Tuesday, May 8, 2012

Expression Evaluation : Addition

Hello guys!!! How are you ?? Think all of you are well by the grace of Almighty. Today we will learn how to add a number with another number in C. Let's look at a program :














Output: Summation: 15

First of all we write a header file  #include<stdio.h>. Then write main function. In main function, from "int a" to "int sum" part is called declaration. Then from "a=5" to "sum=a+b" part is called initialization. Then we print the result. In this program, first of all we take three integer  type variables called "a", "b" and "sum". Then we initialize  5 into "a" and 10 into "b". In the variable "sum" we initialize "a+b". Finally we print the value of "sum". Here "\n" is used for new line.

We can initialize all same type variable & declare variable and initialize at the same time. we can also print the value of  "a+b" without it's value in another variable. If we do it, then this code is look like following:










Output: Summation: 15


If we want to scan the two integer numbers then the code is looked like the following:












Output will depend on input. If inputs are 5 & 10, output will :
Enter Two Numbers: 5 10
Summation: 15


In scanf we scan the address of two variables "a" & "b". In printf generally we print the value of variable.

If we want output more clear, then we can code the following way:













If inputs are "5" & "10", the output will be:

Enter Two Numbers: 5 10
Summation: 5 + 10 = 15


Thank you all...:)

0 comments:

Post a Comment