You are here

Addition of two numbers in C

Addition of two numbers in C: This C language program performs the basic arithmetic operation of addition of two numbers and then prints their sum on the screen. For example, if a user will input two numbers as; '5', '6' then '11' (5 + 6) will be printed on the screen. In the expression (c = a + b) overflow may occur if the sum of a and b is larger than the maximum value which can be stored in the variable c. Similarly, you can write a C program which performs subtraction, multiplication, and division of two numbers.

Addition program in C

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.    int a, b, c;
  6.    
  7.    printf("Enter two numbers to add\n");
  8.    scanf("%d%d", &a, &b);
  9.    
  10.    c = a + b;
  11.    
  12.    printf("Sum of the numbers = %d\n", c);
  13.    
  14.    return 0;
  15. }

Output of C addition program:
Addition C program output.

Download Add numbers program executable.

In the expression (c = a + b) overflow may occur if the sum of a and b is larger than the maximum value which can be stored in the variable c. Similarly, you can write a C program which performs subtraction, multiplication, and division of two numbers.

Addition without using third variable

  1. #include<stdio.h>
  2.  
  3. main()
  4. {
  5.    int a = 1, b = 2;
  6.    
  7.    /* Storing result of addition in variable a */
  8.    
  9.    a = a + b;
  10.          
  11.    printf("Sum of a and b = %d\n", a);
  12.        
  13.    return 0;
  14. }

This method isn't recommended because the original value of the variable 'a' is lost and if we require it further in the program, then we will not have it.

C program to add two numbers repeatedly

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int a, b, c;
  6.    char ch;
  7.  
  8.    while (1) {
  9.       printf("Inut two integers\n");
  10.       scanf("%d%d", &a, &b);
  11.       getchar();
  12.      
  13.       c = a + b;
  14.  
  15.       printf("(%d) + (%d) = (%d)\n", a, b, c);
  16.  
  17.       printf("Do you wish to add more numbers (y/n)\n");
  18.       scanf("%c", &ch);
  19.  
  20.       if (ch == 'y' || ch == 'Y')
  21.          continue;
  22.       else
  23.           break;
  24.    }
  25.  
  26.    return 0;
  27. }

Output of program:

  1. Inut two integers
  2. 2 6
  3. (2) + (6) = (8)
  4. Do you wish to add more numbers (y/n)
  5. y
  6. Inut two integers
  7. 2 -6
  8. (2) + (-6) = (-4)
  9. Do you wish to add more numbers (y/n)
  10. y
  11. Inut two integers
  12. -5 3
  13. (-5) + (3) = (-2)
  14. Do you wish to add more numbers (y/n)
  15. y
  16. Inut two integers
  17. -5 -6
  18. (-5) + (-6) = (-11)
  19. Do you wish to add more numbers (y/n)
  20. n

C program for addition of two numbers using a function

We can calculate sum of two integers using a function.

  1. #include<stdio.h>
  2.  
  3. long addition(long, long);
  4.  
  5. main()
  6. {
  7.    long first, second, sum;
  8.  
  9.    scanf("%ld%ld", &first, &second);
  10.  
  11.    sum = addition(first, second);
  12.  
  13.    printf("%ld\n", sum);
  14.  
  15.    return 0;
  16. }
  17.  
  18. long addition(long a, long b)
  19. {
  20.    long result;
  21.  
  22.    result = a + b;
  23.  
  24.    return result;
  25. }

We have used long data type as it can handle large numbers if you want to add very large numbers which don't fit in long range then use an array, a string or other suitable data structure.