Sie sind auf Seite 1von 4

 

Lab-02
BSME

Instructions
1. Use Borland C for the following programs   
2
  . Create a folder CF_Lab2 in C or any other drive 
 3. Create new project for each program, alternately modify the same file 
4. For any help contact lab staff or faculty member available in the lab 
5. If you want a copy of your programs, at the end of the lab session copy the lab folder from local drive to 
your Z‐drive 
 
 

For the following code, try to predict the output of the program, write it down, execute the program and then match the out of your
program with your predicted output.

#include <conio.h>
#include <stdio.h>
void main(void)
{
clrscr();
printf("This is year %d\n",2011);
printf("The sun will rise at %d:%d in the morning today \n",6,30);
printf("If we multiply %d with %d the answer will be %d \n",12,3,36);
printf("My friend %s came to visit me yesterday at %d-%d p.m.\n", "Ahmed",5,30);
printf("\n\t%d\n\t%d+\n\t----\n\t%d",10,20,30); getch();
}

Your Predicted Output (make this table on your notebook)

Line-1
Line-2
Line-3
Line-4
Line-5
Line-6
Line-7
Line-8

Task-02

 Declare two variables of type integer and float respectively initialize them with 25 and 65.25, print their values
 Declare a character variable and initialize it with letter a, print the value of this variable
 Print the sizes of variables created above using sizeof

 
CF‐Lab‐02 prepared by Nauman Shamim 
Task-03
Go through the following example and understand the way input for various data types is obtained, use this understanding to solve
the problem given after the exanple

Getting input for int, float and char type variables

#include<stdio.h>
#include<conio.h>
void main(void)
int obt_marks, tot_marks
float percentage
char grade
printf(“Enter Total Marks:”);
scanf(“%d”,&tot_marks);
printf(“Enter Obtained Marks:”);
scanf(“%d”,&obt_marks);
percentage=(obt_marks/tot_marks)*100;
grade=’A’;

printf(“\nYou obtained %f percent marks”,percentage);

printf(“\nYour letter grade is =%c”,grade);

getch();

Problem

Write a program that converts temperature from Fahrenheit to Celsius, the conversion formula is °C= (°F - 32) x 5/9. You
should follow these steps to solve this problem

1. Declare variables temp_f, temp_c to represent temperature in Fahrenheit and Celsius , (decide the type of these variables
yourself)
2. Print a message using printf such as “Enter the temperature in Fahrenheit scale: “
3. Use scanf to get the value entered by user and store it in temp_f
4. Write the conversion formula and store the result in temp_c
5. Print a message and the result using printf

TASK ‐4  Try to write programs as specified 
The % (mod) operator returns the remainder of a division , see the example below to understand the concept

#include<stdio.h>
#include<conio.h>

void main(void){
int x=10,y=3;
int res;
printf(“\n The remainder of 5/3 is = %d”,5%3);
printf(“\n The remainder of %d/%d is = %d”,x,y,x%y);
res=x%2;
printf(“\n The remainder of %d/%d is = %d”,x,2,res);
getch();
}

 
CF‐Lab‐02 prepared by Nauman Shamim 
Activity‐01 

a) Write a program that gets time in seconds from user and convert it into days, hours , minutes and seconds
b) Write a program that gets time in terms of days such as 0.5 day, 0.3 day, 2 days or 2.25 days, the program should
convert the given days into hours , minutes and seconds

Hint: You may need to use the % operator

Activity‐02 

Try to predict the value x in following statements, write programs to verify your answers. If your predicted answer do not
match with output of your program see explanation at the end of this document.

1. x=10/3 where x is an integer variable


2. x=25/6 where x is a float variable
3. x=25/6.0 where x is a float variable

Activity ‐03 

 A car is travelling with a velocity of 30 km/hour , how much time will it take to reach its destination which is 600 km
from starting point, represent the time in hours, minutes and seconds;
 Modify the above program such that it can find the time for any given velocity V and distance S
 Write a program to solve the following equation for any values of a and b
(a+b)2= (a2+b2+2ab);
   

 
CF‐Lab‐02 prepared by Nauman Shamim 
 
Explanation 
Line 1)
Line 2) int a=4,b=37;
Line 3) float result;
Line 4) result=b/a;

At line 3 , the division b / a is an integer division, as in c/c++ if an operation involve integers only, the answer will must be
an integer, in our case fraction produced during division will be converted to integer and later will be stored as float in the
variable result;

We know that b/a = 37/4 = 9.25 but as the result is to be stored as it is an integer division the answer will be an integer i.e.
9 only, as this value has to be stored in a float variable this value will be promoted (converted) to float first i.e. 9 will be
converted to 9.0 and then stored in result.

In case of result=10/3 , the value stored in result will be 3.0 not 3 or 3.3333… for same reasons

In case of result=10/3.0 or result=10.0/3 second value will be first promoted to float type i.e. in 10/3.0 10 will be converted
to 10.0 and the division will be 10.0/3.0 which a division of floating point numbers, the answer will also be a float point
number 3.333…. this value will be stored in variable result.

 
CF‐Lab‐02 prepared by Nauman Shamim 

Das könnte Ihnen auch gefallen