Sunteți pe pagina 1din 8

Lab-02

CF-Zero Semester

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
Objective
Understand the working of different format specifiers, Learn to declare different variables, initialize them, use them,
and print values using format specifiers.

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

CF-Lab-02 prepared by Nauman Shamim

Declaring variables

The general format to declare any variable in c is as follows


type

identifier

value

Example

int
float

x
y

100

2.5

Example-1 simple declaration;

#include<stdio.h>
#include<conio.h>
int main ()
{
/* variable definition: */
int
a, b;
int
c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
getch();
return 0;
}
Example-2 Declaring and Initializing

#include<stdio.h>
#include<conio.h>
int main ()
{
int a=10,b=20;
printf(Values of a and b are \n);
printf(a=%d,b=%d \n,a,b);
//Re-Initializing

CF-Lab-02 prepared by Nauman Shamim

;
;

a=30;
b=40;
printf(New values of a and b are \n);
printf(a=%d,b=%d\n,a,b);
return 0;
getch();}
Task: Try to write the output of the following program, note down your answer, execute the program and compare your
answer with program output
#include<stdio.h>
#include<conio.h>
int main ()
{
int a=10,b=20,c=30;
printf(Value of A = %d, value of B=%d,b,a);
printf(\nB+C=%d,a+b);
a=b;
b=c;
c=a+b;
printf(\nThe values of a,b and c are\n a= %d \nb=%d \nc=%d,a,b,c);
getch();
return 0;}

TASK-03

SOLVING PROBLEMS

In the following section a number of example problems will be introduced along with their solution, read the problems and try to
understand the solution. If you can understand the concept try to solve the problems given in this section
Example-3 Sample Problem
Write a program to calculate the area of rectangle whose length and widths are 20 and 30 respectively
Solution

#include<stdio.h>
#include<conio.h>
int main()
{
int length,width,area;
length=20;
width=30;
area=length*width;
printf(Area of rectangle is =%d,area);
getch();
return 0;
}
CF-Lab-02 prepared by Nauman Shamim

Extension Problem
Allow the user to specify the length and width of the rectangle.
#include<stdio.h>
#include<conio.h>
int main()
{
int length,width,area;
printf(Enter Length = );
scanf(%d,length);
printf(Enter Width = );
scanf(%d,width);
area=length*width;
printf(Area of rectangle is =%d,area);
getch();
return 0; }
Solve Yourself
Problem-01
Write a program to solve the following problem.
a) What will be sale price of an item after 2 years if its value decreases 10% each year.
b) What will the 10% of 20% of 45678
Type conversion
Example-4
#include<stdio.h>
#include<conio.h>
int main()
{
int a=4,b=37;
float result;
result=b/a;
printf(Result

of 37 / 5 = %f,result);

getch();
return 0;
}
Q: Is your output correct ? If not why ?
Second version of example-4
#include<stdio.h>
#include<conio.h>
int main()
{
float a=4.0,b=37.0;
CF-Lab-02 prepared by Nauman Shamim

float result;
result=b/a;
printf(Result

of 37 / 5 = %f,result);

getch();
return 0;
}
Q Is your answer correct this time ?,Do you understand the type conversion problem involved ?
Find the answer on last page
Task: 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

Increment and Decrement Operators


Execute the example programs to understand the working of unary operators, if you understand the concept try to solve
the tasks that follow
Example-5 Sample Program
The following program demonstrate use of increment and decrement operators
#include <conio.h>
#include <stdio.h>
int main()
{
int a,b,c;
a=10;
b=20;
c=30;
printf(Starting value of a,b,c are\n);
printf(a=%d,b=%d,c=%d\n,a,b,c);
printf(after a++,b++,c++ value are\n);
a++;
b++;
c++;
printf(a=%d, b=%d ,c=%d \n,a,b,c);
getch();
return 0;
}

CF-Lab-02 prepared by Nauman Shamim

Example-5B Modified Sample Program


#include <conio.h>
#include <stdio.h>
int main()
{
int a,b,c;
a=10;
b=20;
c=30;
printf(Starting value of a,b,c are\n);
printf(a=%d,b=%d,c=%d\n,a,b,c);
a=b+(c++);
printf(after a=b+(c++) values are \n);
printf(a= %d ,b =%d ,c =%d \n,a,b,c);
getch();
return 0;
}
Tasks
1)

Write program for the following expressions, where a,b,c are integer variables, evaluate the expression and display the final
result (final value of variable a)
Initial values of a,b,c are 10,20,30
a)
b)
c)
d)

2)

a=b*c;
b*=b+c;
c=a++;
a=++b+c++;

Enhance the code and get the values of a,b and c from the user

Example-6 Working with other data types


The following program demonstrates how other data types are declared and are printed

#include <conio.h>
#include <stdio.h>
int main()
{
char block_name = Y;
long int block_volume = 987654 ;
float block_density= 120.25 ;
double block_mass=98765410.65;
printfBlock name = %c \n, block_name);
printf(Block volume = %ld meter cube \n ,block_volume);
printf(Block density = %f (grams/cm)\n ,block_density);
printf(Block mass %f (kgs),block_mass);
getch();
return 0;
}
CF-Lab-02 prepared by Nauman Shamim

Example-7 using sizeof()

sizeof() is a library function, the function returns the number of bytes allocated to any variable in the memory, the function
can also return the number of bytes that are specified for various data types. See the example to understand the concept
#include <conio.h>
#include <stdio.h>
int main()
{
int a=20;
float b=1.8.3;
int size_a, size_b;
size_a=sizeof(a);
size_b=sizeof(b);
printf(size of variable a is %d bytes \n ,size_a);
printf(size of variable b is %d bytes ,size_b);
getch();
return 0;
}

Example-08 Print number of bytes specified for various data types


#include <conio.h>
#include <stdio.h>
int main()
{
printf(size of int data type is %d bytes \n ,sizeof(int));
printf(size of float data type is %d bytes ,sizeof(float));
getch();
return 0;
}
Task-1: Using sizeof function print size of all data types (use lecture slides to verify your output)
Task-2: use sizeof function to determine the number of bytes used by the program in example 6;

TASK -4

Try to write programs as specified


Tasks

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 find the area of a circle whose radius is r , the program should take the value of r from the user
using scanf function and print the area of the circle.

CF-Lab-02 prepared by Nauman Shamim

Activity -02
Write a program to solve the following equation for any values of a and b
2

(a+b) = (a +b +2ab);

Type Conversion
Line 1)
Line 2)
Line 3)
Line 4)

int a=4,b=37;
float result;
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

S-ar putea să vă placă și