Introduction
Adding two numbers is one of the basic programs beginners learn when starting with C programming. In this article, we'll explain how to write and understand a C program that adds two numbers.
C Program to Add Two Numbers
#include <stdio.h>
int main() {
int num1, num2, sum;
// Input two integers
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// Calculate the sum
sum = num1 + num2;
// Display the result
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
Program Explanation
1. Header File Inclusion: #include <stdio.h> allows the use of standard input/output functions like printf() and scanf().
2. Main Function: The program execution begins with int main().
3. Variable Declaration: int num1, num2, sum; declares three integer variables.
4. Input from the User:
l printf() displays a message prompting the user to enter two integers
l scanf() reads these integers from user input.
5. Calculation: The sum of num1 and num2 is calculated and stored in sum.
6. Output the Result: printf() displays the result.
Sample Output
Enter two integers: 5 7
The sum of 5 and 7 is 12
Conclusion
This simple program demonstrates the basic structure of a C program, including input/output handling and arithmetic operations.
1 Comments
thank you for this website
ReplyDeleteI really appreciate for your words. Thank you very much for your comment.