Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

20 Jul 2012

c program for adding two numbers / integers

Simple c program for adding two numbers and its explanation:

PROGRAM:
#include<stdio.h>
int main()
{
     int x;
     int y;
     int result;
     printf("\n Enter the first number to be added: ");
     scanf("%d",&x);
     printf("\n Enter the second number to be added: ");
     scanf("%d",&y);
     result = x + y;
     printf("\n The sum of two numbers is: %d\n",result);
     return 0;
}

Sample Output:
( using GNU GCC Compiler with Code Blocks IDE )

   Enter the first number to be added: 10
   Enter the second number to be added: 15
   The sum of two numbers is: 25
 --------------------------------------------------------
EXPLANATION for the above program :    

 1) The #include <stdio.h> includes the file stdio.h into your code so it knows what the

      functions such as  printf() mean.

 2) main() is the main function in which you write most of your code .  int is the default 

      return type of main function.

 3) int x; , int y; , and int result. the int stands for integer or a number
     (no decimal points you need a float for that) the X and Y are the numbers you are 
      adding together and result is the result of the numbers.

 4) printf("\nEnter the first number to be added: ");
     scanf("%d",&x); the Printf() prints the text, Enter the first number to be added: , 

     onto the screen and the         
     scanf("%d",&x); looks for the number input from the keyboard for x.

 5) printf("\n Enter the second number to be added: ");
      scanf("%d",&y); do the same as the last 2 lines except it looks for the 
       value for y instead of x.

 6) result = x + y; that adds the values of X and Y and stores the result in the 

      variable result.

 7) printf(" The sum of two numbers: %d\n",result);  that prints the result on the screen  

      return(0); tells the OS that the program ended  successfully.


For c# program : 

C# PROGRAM TO ADD TWO NUMBERS / INTEGERS 


1 comment: