C program to find sum of two numbers

Category: C Program

Write a C program to enter two numbers and find their sum by using the scanf function.

Example -
Input: 10
       20
Output: Sum of 10 and 20 is: 30

C program to find sum of two numbers

#include <stdio.h>

void main()
{
    int num1, num2, sum;

    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    sum = num1 + num2;

    printf("Sum of %d and %d is: %d", num1, num2, sum);
}

Output

Enter first number: 10
Enter second number: 20
Sum of 10 and 20 is: 30

We can also take the input for both of the two numbers together by using just single scanf function.

#include <stdio.h>

void main()
{
    int num1, num2, sum;

    printf("Enter two numbers to find sum\n");
    scanf("%d%d", &num1, &num2);

    sum = num1 + num2;

    printf("Sum of %d and %d is: %d", num1, num2, sum);
}

Output

Enter two numbers to find sum
10
20
Sum of 10 and 20 is: 30

Recommended Posts