Here’s a C program to add two numbers:
#include <stdio.h>
int main() {
int num1, num2, sum;
// Prompt the user to enter two numbers
printf("Enter two numbers: ");
// Read the two numbers using scanf
scanf("%d %d", &num1, &num2);
// Calculate the sum
sum = num1 + num2;
// Print the result
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
Explanation
Include the Standard Input-Output header file: #include <stdio.h> is used to include the standard input-output library which allows us to use functions like printf and scanf.
Declare the main function: int main() is the main function where the execution of the program begins.
Declare integer variables: int num1, num2, sum; declares three integer variables num1, num2, and sum.
Read inputs from the user:
- printf(“Enter the first number: “); prompts the user to enter the first number.
- scanf(“%d”, &num1); reads the integer input from the user and stores it in num1.
- printf(“Enter the second number: “); prompts the user to enter the second number.
- scanf(“%d”, &num2); reads the integer input from the user and stores it in num2.
Add the two numbers: sum = num1 + num2; calculates the sum of num1 and num2 and store the result in sum.
Print the result: printf(“The sum of %d and %d is %d\n”, num1, num2, sum); prints the values of num1, num2, and sum in a formatted string.
Return statement: return 0; indicates that the program ended successfully.
Algorithm:
Step 1: Start
Step 2: Declare two integer variables, num1 and num2.
Step 3: Declare an integer variable sum to store the result.
Step 4: Read the values of num1 and num2 from the user.
Step 5: Add num1 and num2, and store the result in sum.
Step 6: Print the value of sum.
Step 7: Stop
Explanation of the Algorithm
Step 1: Start
- Start indicates the beginning of the algorithm. This is where the process of adding two numbers begins.
Step 2: Declare two integer variables, num1 and num2.
- Declare two integer variables, num1 and num2: This step involves defining two variables that will hold the integer values entered by the user. These variables will store the two numbers that we want to add.
Step 3: Declare an integer variable sum to store the result.
- Declare an integer variable sum: In this step, we define another integer variable named sum that will hold the result of the addition of num1 and num2.
Step 4: Read the values of num1 and num2 from the user.
- Read the values of num1 and num2 from the user: This step involves taking input from the user. We prompt the user to enter two integer values which will be stored in the variables num1 and num2.
Step 5: Add num1 and num2 and store the result in sum.
- Add num1 and num2, and store the result in sum: In this step, we perform the addition of the two numbers stored in num1 and num2. The result of this addition is then stored in the variable sum.
Step 6: Print the value of sum.
- Print the value of sum: Here, we output the result of the addition to the user. This step involves displaying the value stored in the variable sum.
Step 7: Stop
- Stop: This step signifies the end of the algorithm. Once the result has been displayed, the process is complete, and the algorithm terminates.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.