Multiply two numbers entered 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 product of those two numbers.
Input: Enter the numbers: 3 10
Output: The value after multiplication is: 30
This can be done in the following ways:
Method 1: Display the product directly using a third variable
Method 2: Display the product by calling another function with a void return type
Method 3: Display the product by calling another function with int return type
Method 4: Display the product directly without using a third variable
Method 5: Display the product by calling a function without using a third variable.
Let us look at each of these methods separately.
Program 1: Find the Product of Two Numbers
In this method, a third variable is used to store the product of the two numbers.
Algorithm:
- Start
- Declare two variables.
- Initialize the two variables.
- Use another variable that will store the product 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 product 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; //Multiply the numbers and store the result in another variable
printf("The product of two numbers i.e., %d and %d is %d",x,y,z); //Print the result
return 0;
}
Enter the first number
5
Enter the second number
6
The product of two numbers i.e., 5 and 6 is 30
Program 2: Find the Product of Two Numbers
In this method, we will perform the multiplication operation in another function 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 the multiplication 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 multiplication operation. Finally, the result is displayed in that function.
#include <stdio.h>
void multiply(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
multiply(x,y); //Function Call
return 0;
}
void multiply(int x,int y) //Function Definition
{
int z;
z=x*y; //multiply the numbers and store the result in another variable
printf("The product of two numbers i.e., %d and %d is %d",x,y,z); //Print the result
}
Enter the first number
2
Enter the second number
8
The product of two numbers i.e., 2 and 8 is 16
Program 3: Find the Product of Two Numbers
In this method, we will perform the multiplication 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 the multiplication operation.
- Use another variable that will store the product 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 multiplication operation. Now, the result is returned to the main method. Finally, the result is displayed.
#include <stdio.h>
int multiply(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 = multiply(x,y); //Function Call
printf("The product of two numbers i.e., %d and %d is %d",x,y,z); //Print the result
return 0;
}
int multiply(int x, int y) //Function Definition
{
return x*y; //Returns the product of both the variables
}
Enter the first number
4
Enter the second number
8
The product of two numbers i.e., 4 and 8 is 32
Program 4: Find the Product of Two Numbers
In this method, a third variable is used to store the product of the two numbers.
Algorithm:
- Start
- Declare two variables.
- Initialize the two variables.
- Without using any other variable directly calculate the product.
- Print the result.
- Stop.
Below is the code for the same
Here, the user is asked to enter two numbers. Then, the product 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 product without using a third variable
printf("The product of two numbers i.e., %d and %d is %d",x,y,x*y); //Print the resultant
return 0;
}
Enter the first number
2
Enter the second number
4
The product of two numbers i.e., 2 and 4 is 8
Program 5: Find the Product of Two Numbers
In this method, we will perform the multiplication 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 the multiplication operation.
- Without using any other variable directly calculate the product.
- 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 multiplication operation of the two numbers is performed directly and the value is displayed.
#include <stdio.h>
void multiply(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
multiply(x,y); //Function Call
return 0;
}
void multiply(int x,int y) //Function Definition
{
//Directly display the product without using a third variable
printf("The product of two numbers i.e., %d and %d is %d",x,y,x*y); //Print the resultant
}
Enter the first number
7
Enter the second number
7
The product of two numbers i.e., 7 and 7 is 49