Write a C Program to Add Two Integers

Write a C program to add Two Integers

A C program to add two integers is a fundamental example often used to introduce beginners to the basics of programming. This program demonstrates the basic structure of a C program and the use of variables and arithmetic operations.

Explanation:

  1. Include Header File: The #include <stdio.h> directive is used to include the standard input-output library, which is necessary for functions like printf and scanf.
  2. Main Function: The main() function is the entry point of every C program. The execution of the program starts from the main() function.
  3. Variables: Declare variables to store the two integers and the result of the addition.int num1, num2, sum;

4.Input: Use scanf to get input from the user for the two integers.

printf(“Enter the first integer: “); scanf(“%d”, &num1); printf(“Enter the second integer: “); scanf(“%d”, &num2);

5. Addition: Perform the addition operation.

sum = num1 + num2;

6.Output: Display the result using printf.

printf(“Sum: %d\n”, sum);

Add Two Integers User Input:

#include <stdio.h>
int main(){
int mum1, mum2, Sum of integers;
printf("Enter first integer: ");//entered by user//
scanf("%d", &num1);//entered by user//
printf("Enter second integer: ");
scanf("%d", &nuum2);
Sum of integers = num1 + num2;//using sum adding two integers//
printf("Sum of %d and %d is %d", first integer, second integer, Sum of integers);
return 0;
}

Output:

Enter first integer: 2
Enter second integer: 4
Sum=6 

Conclusion:

In conclusion, this simple C program serves as a foundation for understanding basic programming concepts such as variable declaration, user input, arithmetic operations, and output display. It provides a hands-on experience for beginners to grasp the structure and syntax of a C program. As you progress in your programming journey, you’ll encounter more complex tasks and concepts, building upon the foundational knowledge demonstrated in this introductory program.

Scroll to Top