Important Programs CH 10
Important Programs CH 10
Write a program that display a message and values of Write a program that adds two floating point numbers
integer and character variables. and show the sum on the screen.
Void main ( ) Void main ( )
{ {
int a = 20 ; char ch = ‘a’ ; float a , b , sum ;
printf(“Value of integer variable a = %d” , a ); printf(“Enter value of a = ”);
printf(“Value of character variable ch = %c” , ch ); scanf(“%f”, & a);
} printf(“Enter value of b = ”);
scanf(“%f”, & b);
sum = a + b ;
printf(“Sum of a & b = %f” , sum );
}
Q#3 Q#4
Write a program to calculate and print the area of Write a program that displays average marks using field
square with the given height and width. width of 5 characters and precision of 2 characters.
Void main ( ) Void main ( )
{ {
int h , w , sqr ; float avg = 75.659874 ;
printf(“Enter value of Height = ”); printf(“Average Marks = %5.2f” , avg );
scanf(“%d”, & h); }
printf(“Enter value of Width = ”);
scanf(“%d”, & w);
sqr = h * w ;
printf(“Area of Square = %d” , sqr );
}
Q#5 Q#6
Write a program that displays the floating point value Write a program that displays value of character
152.3333 using field width of 10 characters and variable grade.
precision of 3 characters justified to left side.
Void main ( ) Void main ( )
{ {
float a = 152.3333 ; char grade = ‘A’ ;
printf(“% - 10.3 f” , a ); printf(“Value of Grade = %c” , grade );
} }
Q#7 Q#8
Write a program that displays the value stored in a Write a program that displays the name of a person
string variable name. right justified and address and address left justified by
using characters spaces for each.
Void main( ) Void main( )
{ {
char name[20] = “Saba Nadeem”; char name[20] = “Saba Nadeem”;
printf("My name is = %s", name); char adrs[20] = “st # 1, Abc, Rwp”;
} printf("My name is = % 25 s", name);
printf("My Address is = % -25 s", adrs);
}
Q#9 Q # 10
Write a program that displays the following output Write a program to show following output using one
using single printf() statement. printf() statement.
* 1 2 3 4 5
** 6 7 8 9 10
***
****
Write a program to show following output using one Write a program to convert distance from kilometers
printf() statement. into meters.
Pakistan is my country
Islamabad is its capital
Void main ( ) Void main ( )
{ {
printf(“ Pakistan is my country \n Islamabad is its int km , m ;
capital “); printf(“Enter value in Kilometer = ”);
} scanf(“%d”, & km);
m = km * 1000 ;
printf(“Distance in Meter = %d” , m );
}
Q # 13 Q # 14
Write a program that inputs name, age and address Write a program that input base and height from the
from the user and display it on the screen. user and calculate area of a triangle by using the
formula: Area = ½ * base * height
Void main( ) Void main ( )
{ {
char name[20] , address[20] ; float b , h , area ;
int age; printf(“Enter value of Base = ”);
printf("Enter your name = "); scanf(“%f”, & b);
gets(name); printf(“Enter value of Height = ”);
printf("Enter your Address = "); scanf(“%f”, & h);
gets(address); area = (b * h) / 2 ;
printf("Enter your Age = "); printf(“Area of Triangle = %f” , area );
scanf(“%d”, &age); }
printf("Name = %s", name);
printf("Age = %d", age);
printf("Address = %s", address);
}
Q # 15 Q # 16
Write a program that gets temperature from the user in Write a program that gets a three digit number from
Celsius and convert it into Fahrenheit using formula: the user and displays it in reverse order. For example,
F = 9/5 * C + 32 the user enters 123, and program displays 321.
Q # 15 Q # 16
Write a program that input 4 numbers and calculate the Write a program that converts a person’s height from
sum, average and product of all the numbers. inches to centimeters using the formula 2.54 height.
Write a program that takes two numbers from the user Write a program that calculates the area of trianlge by
in two different variables. Now swap their values and using the following formula:
display swapped values. area = sqrt (s * (s – a) * (s – b) * (s – c)) ;
where s = (a + b + c) / 2 ;
Void main ( ) Void main ( )
{ {
int m , n , temp; int a , b, c ;
printf(“Enter value of m = ”); printf(“Enter value of a = ”);
scanf(“%d”, & m); scanf(“%d”, & a);
printf(“Enter value of n = ”); printf(“Enter value of b = ”);
scanf(“%d”, & n); scanf(“%d”, & b);
temp = m ; printf(“Enter value of c = ”);
m = n; scanf(“%d”, & c);
n = temp ; float s = (a + b + c) / 2 ;
printf(“Value After Swapping” ); float a = sqrt (s * (s – a) * (s – b) * (s – c)) ;
printf(“Value of m = %d”, m); printf(“Area of Triangle = %f”, a);
printf(“Value of n = %d”, n);
} }
Q # 21 Q # 22
Write a program that reads the value of radius and Write a program that reads the value of radius and
display surface are and volume of sphere by using display circumference of circle and use value of π by
following formula: using “define” preprocessor directive.
Surface area = 4 π r2
Volume = 4/3 π R3 Circumference = 2 π r
Void main ( ) # define pi 3.14 ;
{
int r ; Void main ( )
printf(“Enter value of Radius = ”); {
scanf(“%d”, & r); int r ;
float sa = 4 * 3.14 * (r * r) ; printf(“Enter value of Radius = ”);
float vol = (4 / 3) * 3.14 * (r * r *r) ; scanf(“%d”, & r);
printf(“Surface Area of Sphere = %f”, sa); float cf = 2 * pi * r ;
printf(“Volume of Sphere = %f”, vol); printf(“Circumference = %f”, cf);
} }
Q # 23 Q # 24
Write a program that reads the value of character Write a program that reads the value of character
variable by using getch() function and then display it on variable by using and then display its ascii value on
screen. screen.
Void main ( ) Void main ( )
{ {
char ch ; char ch ;
printf(“Enter value of Character = ”); int ascii ;
ch = getch ( ) ; printf(“Enter value of Character = ”);
printf(“Value of Character = %c”, ch); scanf(“%c”, &ch );
} ascii = ch;
printf(“ASCII Value of Character = %d ”, ascii);
}
Q # 25 Q # 26
Write a program that reads a string from the user by Write a program that displays memory in bytes of
using gets() function and then display it on screen. different variables of different data types by using
sizeof() operator.
Void main ( ) Void main ( )
{ {
char str [50] ; int a ;
float b ;
printf(“Enter String = ”); double c ;
gets ( str ) ; char ch ;