C Program to Add two numbers given by the user
In C language, to read the inputs we use the scanf() function, and then to print the result we use the printf() function. The %d used in scanf() and printf() functions is the format specifier that is used for int datatype in C.
In this example, the user will provide two numbers and our task is to find the summation of those two numbers.
Input: Enter the numbers: 5 6
Output: Addition of these two numbers is : 11
This can be done in the following ways:
Method 1: Display the sum directly using a third variable
Method 2: Display the sum by calling another function with a void return type
Method 3: Display the sum by calling another function with int return type
Method 4: Display the sum directly without using a third variable
Method 5: Display the sum by calling a function without using a third variable.
Let us look at each of these methods separately.
Program 1: Add two Numbers Given By the User
In this method, a third variable is used to store the summation of the two numbers.
Algorithm:
- Start
- Declare two variables.
- Initialize the two variables.
- Use another variable that will store the result of these two numbers.
- Print the result.
- Stop.
Below is the code for the same
Here, the user is asked to enter two numbers. Then, the summation of these two numbers is stored in another variable and the resultant is displayed. All of these tasks are done in the main method itself.
#include <stdio.h>
int main()
{
int x,y,z; //Variable Declaration
printf("Enter the first number \n");
scanf("%d ",&x); //Initialize the first number
printf("Enter the second number \n");
scanf("%d ",&y); //Initialize the second number
z=x+y; //Add the numbers and store the result in another variable
printf("The sum of two numbers i.e., %d and %d is %d",x,y,z); //Print the resultant
return 0;
}
Enter the first number 5
Enter the second number 8
The sum of two numbers i.e., 5 and 8 is 13
Program 2: Add two Numbers Given By the User
In this method, we will perform the addition operation in another method by using a third variable. This third variable will store the result and display it.
Algorithm:
- Start
- Declare two variables.
- Initialize the two variables.
- Call a function to perform an addition operation.
- Use another variable that will store the result of these two numbers.
- Print the result.
- Stop.
Below is the code for the same
Here, the user is asked to enter two numbers. Then, a function is called and the numbers are passed as arguments. Another variable is declared in that function which stores the result after performing the addition operation. Finally, the result is displayed in that function.
#include <stdio.h>
void add(int x,int y); //Function Declaration
int main()
{
int x,y; //Variable Declaration
printf("Enter the first number \n");
scanf("%d ",&x); //Initialize the first number
printf("Enter the second number \n");
scanf("%d ",&y); //Initialize the second number
add(x,y); //Function Call
return 0;
}
void add(int x,int y) //Function Definition
{
int z;
z=x+y; //Add the numbers and store the result in another variable
printf("The sum of two numbers i.e., %d and %d is %d",x,y,z); //Print the resultant
}
Enter the first number 4
Enter the second number 5
The sum of two numbers i.e., 4 and 5 is 9
Program 3: Add two Numbers Given By the User
In this method, we will perform the addition operation in another method by using a third variable. This third variable will store the result and the function then will return the result. Finally, the result is displayed in the main method.
Algorithm:
- Start
- Declare two variables.
- Initialize the two variables.
- Call a function to perform an addition operation.
- Use another variable that will store the result of these two numbers.
- Return the result.
- Print the result in the main method.
- Stop.
Below is the code for the same
Here, the user is asked to enter two numbers. Then, a function is called and the numbers are passed as arguments. Another variable is declared in that function which stores the result after performing the addition operation. Now, the result is returned to the main method. Finally, the result is displayed.
#include <stdio.h>
int add(int x, int y); //Function Declaration
int main()
{
int x,y,z; //Variable Declaration
printf("Enter the first number \n");
scanf("%d ",&x); //Initialize the first number
printf("Enter the second number \n");
scanf("%d ",&y); //Initialize the second number
z = add(x,y); //Function Call
printf("The sum of two numbers i.e., %d and %d is %d",x,y,z); //Print the resultant
return 0;
}
int add(int x, int y) //Function Definition
{
return x+y; //Returns the summation of both the variables
}
Enter the first number 5
Enter the second number 3
The sum of two numbers i.e., 5 and 3 is 8
Program 4: Add two Numbers Given By the User
In this method, a third variable is used to store the summation of the two numbers.
Algorithm:
- Start
- Declare two variables.
- Initialize the two variables.
- Without using any other variable directly calculate the sum.
- Print the result.
- Stop.
Below is the code for the same
Here, the user is asked to enter two numbers. Then, the summation of these two numbers is displayed. All of these tasks are done in the main method itself.
#include <stdio.h>
int main()
{
int x,y; //Variable Declaration
printf("Enter the first number \n");
scanf("%d ",&x); //Initialize the first number
printf("Enter the second number \n");
scanf("%d ",&y); //Initialize the second number
//Directly display the sum without using a third variable
printf("The sum of two numbers i.e., %d and %d is %d",x,y,x+y); //Print the resultant
return 0;
}
Enter the first number 9
Enter the second number 6
The sum of two numbers i.e., 9 and 6 is 15
Program 5: Add two Numbers Given By the User
In this method, we will perform the addition operation in another method by using a third variable. This third variable will store the result and display it.
Algorithm:
- Start
- Declare two variables.
- Initialize the two variables.
- Call a function to perform an addition operation.
- Without using any other variable directly calculate the sum.
- Print the result.
- Stop.
Below is the code for the same
Here, the user is asked to enter two numbers. Then, a function is called and the numbers are passed as arguments. In that function, the addition operation of the two numbers is performed directly and the value is displayed.
#include <stdio.h>
void add(int x,int y); //Function Declaration
int main()
{
int x,y; //Variable Declaration
printf("Enter the first number \n");
scanf("%d ",&x); //Initialize the first number
printf("Enter the second number \n");
scanf("%d ",&y); //Initialize the second number
add(x,y); //Function Call
return 0;
}
void add(int x,int y) //Function Definition
{
//Directly display the sum without using a third variable
printf("The sum of two numbers i.e., %d and %d is %d",x,y,x+y); //Print the resultant
}
Enter the first number 5
Enter the second number 3
The sum of two numbers i.e., 5 and 3 is 8