72 Programming and Data Structures
4.10 Write a program to calculate average of three real numbers.
# include <std io.h>
# in c lu d e <co n io.h >
m a in ()
{
flo a t a ,b ,c ,d ;
c l r s c r ( ) ;
printf ("
ENTER THREE FLOAT NUMBERS An");
scanf ("n %/%/%/", &a,&b,&c);
d=a+b+c;
printf ("
Average of Given Numbers: % fr,d/3);
i
OUTPUT:
ENTER THREE FLOAT NUMBERS : 2.5 3.5 4.5
Average of Given Numbers : 3.5
Explanation In the above program three float values are assigned to the variables a , b & c . Sum of
these variables is assigned to d. Dividing the sum by 3, average is calculated and displayed.
4.11 Write a program to print message with inputted name.
# in clu d e < s td io .h >
# in c lu d e < co n io.h >
m a in ()
{
ch a r yournam e[ 1 0 ] ;
c l r s c r ( ) ;
printf ("n Enter Your Name
scanf ("%s”, yourname);
printf ("
Wei Come %s to 'Cf Programming Course”, yourname);
I
OUTPUT:
Enter Your Name: Ajay
Wei Come Ajay to 'C' Programming Course
Explanation In the above program a character array is declared. The f i r s t p r i n t f O statement
prompts the user to enter his/her name. The s ca n f () statement reads string through the keyboard.
The second p r i n t f () statement displays message with the entered name.
Note F or c h a r a c te r a r r a y r e f e r c h a p te r 7] A rr a y s .