Addition of two numbers

Simple Program Example

Write the program of addition of two numbers.

addition.c (Name of program)

I.                  Code of Program: 

                 Write the program of addition of two numbers without store the result.

1.    /* this program display the addition of two number on console.

2.    Written by www.abccode.in

3.    stdio.h provide standard input output functions.

4.    conio.h provide console input output functions. */

5.      #include<stdio.h>

6.      #include<conio.h>

7.      void main()

8.      {

9.                   int a, b;  // a and b are the two integers declared here.

10.                     a=10; // Variable a assigning with 10 value.

11.                     b=20; // Variable b assigning with 10 value.

12.                     printf("Two number addition is = %d",(a + b));

13.                     getch();

14.           }

Explanation of Program Code:

Line 1 to 4: First four lines are comment lines.

Line 5: Library file stdio.h provide standard input output functions.

Line 6: Library file conio.h provide console input output functions.

Line 7: main function heading line. Start the execution of program from this line. 

Line 8: “{“ this indicate the beginning of main function.

Line 9: “a” and “b” are variable names that are used to store integer number. We must use integer data type to store the number value without floating point. int used to declare the integer variable.

Variable must be declared before use in program code. In the later chapter we will discuss the data type in detail. Variable declared without value.  

Line 10: Variable a assigning with 10 value.

Line 11: Variable b assigning with 20 value.

Line 12: Standard output function printf can be used to print message with addition of two integer number. This function contain two arguments. First argument “%d” tells the compiler that the values of second argument. These arguments are separated by comma. Second argument (a + b) calculate the addition of variables.

Line 13: The getch function is the console output function in the conio.h library file. That pause the output console until a key is pressed.  

Line 14: “}“ this indicate the end of main function.

Output of Program:

Two number addition is = 30

Use code and try to run:

/* this program display the addition of two number on console.

Written by www.abccode.in */

// stdio.h provide standard input output functions.

// conio.h provide console input output functions.

  #include<stdio.h>

  #include<conio.h>

  void main()

  {

                    int a, b;  // a and b are the two integers declared here.

                    a=10; // Variable a assigning with 10 value.

                    b=20; // Variable b assigning with 10 value.

                    printf("Two number addition is = %d",(a + b));

                    getch();

}

II.              Code of Program:

                Write the program of addition of two numbers with store the result.

1.    /* this program display the addition of two number on console.

2.    Written by www.abccode.in

3.    stdio.h provide standard input output functions.

4.    conio.h provide console input output functions. */

5.      #include<stdio.h>

6.      #include<conio.h>

7.      void main()

8.      {

9.                 int a, b, c;// a and b are the two integers declared here.

10.                   a=10; // Variable a assigning with 10 value.

11.                   b=20; // Variable b assigning with 10 value.

12.                   c= a + b;

13.                   printf("Two number addition is = %d",c);

14.                   getch();

15.           }

Explanation of Program Code:

In this program code, “c” is variable that used to store the addition of “a” and “b” variable. Programmer must store results of any expression only if result want to use them in the next program code.

In this program, Line number 12 has expression c = a+b;   Variable c store the addition of variable a and b. Display the result using c instead of (a + b).

Output of Program:

Two number addition is = 30

Use code and try to run:

/* this program display the addition of two number on console.

Written by www.abccode.in */

// stdio.h provide standard input output functions.

// conio.h provide console input output functions.

  #include<stdio.h>

  #include<conio.h>

  void main()

  {

                int a, b, c;  // a and b are the two integers declared here.

                a=10; // Variable a assigning with 10 value.

                b=20; // Variable b assigning with 10 value.

                c = a + b;

                printf("Two number addition is = %d",c);

                getch();

}

Comments