Call us at 0700-922-6559 or Click here

write a program to add two numbers

Below is the program to add two numbers with explanation.

#include <stdio.h>

int main() {    

    int number1, number2, sum;

    printf(“Enter two integers: “);

    scanf(“%d %d”, &number1, &number2);

    // calculate the sum

    sum = number1 + number2;      

    printf(“%d + %d = %d”, number1, number2, sum);

    return 0;



Enter two integers: 5 8

5 + 8 = 13

let’s break down the C program step by step:

  1. #include <stdio.h>: This line includes the standard input-output library, allowing you to use functions like printf and scanf for input and output operations.
  2. int main(): This line marks the beginning of the main function, which is the entry point of a C program. It returns an integer (int) value to the operating system when the program finishes running. In this case, it takes no arguments, indicated by the empty parentheses ().
  3. {: The curly brace marks the beginning of the main function’s body, where the actual code of the program resides.
  4. int number1, number2, sum;: Here, you declare three integer variables: number1, number2, and sum. These variables will be used to store two integers entered by the user and the result of their sum.
  5. printf(“Enter two integers: “);: This line uses the printf function to display the message “Enter two integers: ” on the console, prompting the user to input two integers.
  6. scanf(“%d %d”, &number1, &number2);: This line uses the scanf function to read input from the user. It expects two integer values separated by a space and stores them in number1 and number2. The & operator is used to provide the memory addresses of these variables so that scanf can write the input values to them.
  7. sum = number1 + number2;: Here, you calculate the sum of the two integers entered by the user and store the result in the sum variable.
  8. printf(“%d + %d = %d”, number1, number2, sum);: This line uses printf to display the sum of the two integers along with the numbers themselves. It prints the values of number1 and number2 and their sum, separated by “+” and “=”, respectively.
  9. return 0;: This line indicates the end of the main function and returns an exit status of 0 to the operating system, which typically signifies successful execution of the program.
  10. }: The closing curly brace marks the end of the main function’s body and, thus, the end of the program.

When you run this program:

It prompts you to enter two integers.

You input two integers separated by a space (in this case, “5” and “8”) and press Enter.

The program calculates the sum of these two integers (5 + 8 = 13).

It then displays the result along with the input values as “5 + 8 = 13.”

So, in your provided example, when you entered “5 8,” the program displayed “5 + 8 = 13” as the output.

If you want learn C programming, then join our course.

Leave a Reply

Your email address will not be published. Required fields are marked *