Wednesday 4 November 2015

SAMPLE C PROGRAM 2

Lets look at the following program now.

 OUTPUT







We have already seen the explanation about LINES 1, 2, 3, 4, 6, 8 and 12. In this session , we will discuss about the rest of the code.


LINE 5   : int n1, n2, sum;
This is a declaration statement
n1, n2 and sum are the names of variables.
A variable is a memory location where a value can be stored for use by the program
The above declaration specifies that the variables n1, n2 and sum are of type int which means that it can hold only integer values
LINE 7   : scanf("%d",&n1);
This is used to obtain the value from the user
The scanf function has 2 arguments : "%d" and &n1
First argument indicates the type of data that should be input by the user
%d indicates that the data should be an integer (d – decimal integer)
Second argument begins with an ampersand (&) – called the address operator in C – followed by the variable name
So &n1 means value entered through the keyboard is assigned to the variable n1
LINE 10 : sum=n1+n2;
This statement calculates the sum of variables n1 and n2 and assigns the result to the variable sum using the assignment operator (=)
LINE 11 : printf("Sum = %d",sum); 
This is used to print the string "Sum = "followed by the numerical value of the variable sum on the screen

No comments:

Post a Comment