Write a C Program to Add Two Numbers.
In this Article, you will learn how to add two numbers entered by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
// Declare variables to store the two numbers
int a, b;
// Prompt the user to enter the first number
printf("Enter the first number: ");
scanf("%d", &a);
// Prompt the user to enter the second number
printf("Enter the second number: ");
scanf("%d", &b);
// Calculate the sum of the two numbers
int c= a+ b;
// Display the result
printf("The sum of %d and %d is %d.\n", a, b, c);
}
This program uses a integer integer variables a and b to
store the user's input. It prompts the user to enter their name using
printf and reads the user's input using scanf. The
"%d" format specifier tells scanf to read an integer from
the user and store it in the variable number.
After successfully reading the input, the program displays the entered
integer back to the user using printf. The message "Enter the first number:" is shown, followed by the value of the variable number.
The sum of a and b is calculated and stored in a new
variable called c.
Finally, the program displays the result by printing the message "The sum of {a} and {b} is {c}." using the printf() function.
When you run this program, it will take two integer inputs from the user, add
them together, and then display the sum on the screen. For example:
Output:
Flow Chat of program:
Draw a flowchart to find sum of two number.
Algorithm
Write an algorithm to find out sum of two numbers.
Step 1 : input two numbers : a,b
Step 2 : calculate c = a+b
Step 3 : print "The sum of %d and %d is %d.", a, b, c
Step 4 : Stop