Input and Output in C 71
4.9 Write a program to perform addition of two numbers.
# include <stdlo.h>
# include <conio.h>
mainO
{
in t a ,b ,c ;
printf (“n ENTER TWO VALUESn");
scanf (“ %d %d ", &a, &b);
cxa+b;
printf ("tt Sum is~%d",c);
}
OUTFUJ;
ENTER TWO VALUES 58
SUM IS = 13
Explanation In the above program variables a , b & c are declared. Values of a and b are read
through the keyboard using s c a n f () statement. Addition of variables a and b is performed and
assigned to variable c.
4.9 Write a program to find the square of the given number.
# in clu d e < std io .h >
# in clu d e < conio.h>
m ain()
{
in t a ,c ;
printf (" ENTER ANY NUMBERn");
scanf (" %d ", &a);
c=a*a;
printf (" SQUARE OF GIVEN NUMBER = %d",c);
I
OUTPUT:
ENTER ANY NUMBER 5
SQUARE OF GIVEN NUMBER 25
Explanation The above program is same as the previous one. The only difference is that instead of
addition, product of a and b is calculated.
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 .
Input and Output in C 73
4.12 Write a program to input a single character and display it.
# include <stdio.h>
# include <conio.h>
mainO
{
ch ar ch;
c l r s c r () ;
printf ("Enter any character
scanf ("%c",&ch);
printf ("n Your Entered Character is : %c ",ch);
I
OUTPUT: Enter any character: C
Your Entered Character is : C
Explanation In the above program a character is entered and stored in variable ch. The p rin t f ()
statement displays the entered character.
4.13 Write a program to swap the values of two variables without using third variable.
# include <stdio.h>
# include <conio.h>
void main ()
{
in t a=7, b=4;
c lrs c r () j
printf (" A= %d B= %d,a,b);
a=a+b;
b=a-b;
a-a-b;
printf (" Now A= %d B= %d,a,b);
I
OUTPUT:
A=7 B=4
Now A= 4 B=7
Explanation In the above program no third variable is used as a mediator for swapping the values.
The steps given below illustrates the working of the program.
A) In the first statement variable ' a ' contains sum of a+b i.e. 11.
B) In the second statement variable ' b ' contains a-b i.e. 11-4 =7.
C) In the third statement variable x a # contains a-b i.e. 11-7=4
Thus, the two values are intei-changed.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.133.49