Wednesday 18 April 2012

Adding Integers

If you just type anything like 2+2 in the printf statement, then there is no use.
User should input the data of his own choice, the Compiler should scan it and display on the user screen.


So, for this, type:
 


Here, int stands for integer. It is a data type. Learn Data Types in my next post.
int a, b, c;
a, b and c are the variables here. (You can use variable)


Now comes scanf


scanf is used to scan the input of the user and assign it to the variable 


%d is format specifier used for integer. Learn Format Specifier in my future post.


Now you should give instructions to the compiler to add.


So write c = a+b


This tells the compiler that c is the addition of a and b.


Save the program and compile.


Now, run the program and it asks you:


Enter the value of a:


(Enter any integer and press enter)


Again  enter any integer for the value of b and press Enter.


The result will be displayed.


Any problem, use comment box.

0 comments: