#include
main()
{
int sum;
sum=10+20;
printf("%d\n",sum);
}
This program stores the result of addition of two nos. 10 and 20 into a variable sum.Here sum has been declared of int data type which means possible values of the sum are integers.
Fifth line of the above program assigns the addition of 10 & 2o to a variable sum.Equal to (=) acts as an assignment operator that assigns the value of right hand expression to left hand variable.
The printf statement will print 30 as output on the output screen.The reason of this output is use of "%d" within double quotes and the name of corresponding variable is specified after the comma.
The above written program adds 10 and 20. If we want to add 40 and 50,we have to change the program.If we want to add any two nos. based on user's choice,then the above program can be modified as
#include
main()
{
int num1,num2,sum;
printf("enter two integer values to be added");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("Addition of %d and %d=%d\n",num1,num2,sum);
}
A new instruction scanf has been used in this program, which is used to take inputs from the user.
The output of the program will be as follows :
enter two integer values to be added 30 20
Addition of 30 and 20=50
_
No comments:
Post a Comment