Menu

Add Topic

Forum

waiting answer December 27, 2020

Write a c program to add two numbers?

Answers
December 26, 2020

Program: C program to add two numbers

     #include<stdio.h>
 
     int main() {
        int num1, num2, sum;
     
        printf("\nEnter two numbers: ");
        scanf("%d %d", &num1, &num2);
     
        sum = num1 + num2;
     
        printf("Sum: %d", sum);
     
        return(0);
     }

Output

           Enter two numbers: 4 5
           Sum: 9

0 0
December 26, 2020

Program: addition of two numbers in c

     #include <stdio.h>
     int main() {    

         int number1, number2, add;
        
         printf("Enter two integer numbers: ");
         scanf("%d %d", &number1, &number2);

         // calculating sum
         add = number1 + number2;      
        
         printf("%d + %d = %d", number1, number2, add);
         return 0;
     }

Output

            Enter two integer numbers: 10 10
            10 + 10 = 20

0 0
December 26, 2020

In this example, the user is asked to enter two integer numbers and then the program displays the sum of these two numbers.

     #include <stdio.h>
     int main()
     {
        int number1, number2, sum;
        printf("Enter the first number: ");
        scanf("%d", &number1);
        printf("Enter the second number: ");
        scanf("%d", &number2);

        sum = number1 + number2;
        printf("Sum of the two numbers are: %d", sum);
        return 0;
     }

Output

            Enter the first number: 10
            Enter the second number: 10
            Sum of the two numbers are: 20

0 0
December 26, 2020

C program to find the sum of two numbers using a user-defined function. The function adds two numbers and returns the result of the addition.

     #include <stdio.h>
     int sum2number(int num1, int num2){
        return num1+num2;
     }
     int main()
     {
        int number1, number2, sum;
        printf("Enter the first number: ");
        scanf("%d", &number1);
        printf("Enter the second number: ");
        scanf("%d", &number2);
        //Calling the function
        sum = sum2number(number1, number2);
        printf("The Sum of the entered numbers are: %d", sum);
        return 0;
     }

Output

           Enter the first number: 10
           Enter the second number: 10
           The Sum of the entered numbers are: 20

0 0
December 26, 2020

You can also use the bitwise operator to find the sum of two numbers.

    int sum(int num1, int num2) 
    {      
        while (num2 != 0) 
        { 
            int carry = num1 & num2;   
            num1 = num1 ^ num2;  
            num2 = carry << 1; 
        } 
        return a; 
    }

0 0
December 26, 2020

In this example, I will show you how we can add two numbers without using the + operator. 

    #include<stdio.h>
    int main()
    {
        int a,b;
        a=10;
        b=20;
        int sum;
        sum=a-(-b);
        printf ("Sum of two numbers are %d",sum);
        return 0;
    }

0 0

Please Login to Post the answer

Leave an Answer