Multiply two numbers and round up to two decimal places
In C language, to read the inputs we use the scanf() function, and then to print the result we use the printf() function. The %f used in scanf() and printf() functions is the format specifier that is used for float datatype in C.
In this example, the user will provide two numbers and our task is to find the product of those two numbers and round it up to two decimal places.
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: Multiply Two Numbers and Round it up to Two Decimal Places
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 using the format specifier.
- 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()
{
float num1,num2; //Number Declaration
printf("Enter the first number\n");
scanf("%f",&num1); //Initializing the first number
printf("Enter the second number\n");
scanf("%f",&num2); //Initializing the second number
float num = num1*num2; //Multiplying the two numbers
//Print the product of the two numbers upto 2 decimal places
printf("The product of %f and %f is %0.2f", num1,num2,num);
return 0;
}
Enter the first number
3.124
Enter the second number
2.431
The product of 3.124000 and 2.431000 is 7.59
Program 2: Multiply Two Numbers and Round it up to Two Decimal Places
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 using the format specifier.
- 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(float num1, float num2); //Function Call
/* Creating a user-defined function that will calculate the product of two numbers and will display the result up to two decimal places*/
int main()
{
float num1,num2; //Number Declaration
printf("Enter the first number\n");
scanf("%f",&num1); //Initializing the first number
printf("Enter the second number\n");
scanf("%f",&num2); //Initializing the second number
multiply(num1,num2); //Function Call
return 0;
}
void multiply(float num1, float num2) //Function Definition
{
float num = num1*num2; //Multiplying the two numbers
//Print the product of the two numbers upto 2 decimal places
printf("The product of %f and %f is %0.2f", num1,num2,num);
}
Enter the first number
1.214
Enter the second number
4.256
The product of 1.214000 and 4.256000 is 5.17
Program 3: Multiply Two Numbers and Round it up to Two Decimal Places
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 with format specifier.
- 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>
float multiply(float num1, float num2); //Function Call
/* Creating a user-defined function that will calculate the product of two numbers and will return the result back to the main method.*/
int main()
{
float num1,num2; //Number Declaration
printf("Enter the first number\n");
scanf("%f",&num1); //Initializing the first number
printf("Enter the second number\n");
scanf("%f",&num2); //Initializing the second number
float num= multiply(num1,num2); //Function Call
//Print the product of the two numbers upto 2 decimal places
printf("The product of %f and %f is %0.2f", num1,num2,num);
return 0;
}
float multiply(float num1, float num2) //Function Definition
{
return num1*num2; //Multiplying the two numbers
}
Enter the first number
5.246
Enter the second number
9.123
The product of 5.246000 and 9.123000 is 47.86
Program 4: Multiply Two Numbers and Round it up to Two Decimal Places
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 using the format specifier.
- 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()
{
float num1,num2; //Number Declaration
printf("Enter the first number\n");
scanf("%f",&num1); //Initializing the first number
printf("Enter the second number\n");
scanf("%f",&num2); //Initializing the second number
//Print the product of the two numbers by rounding it up to 2 decimal places
printf("The product of %f and %f is %0.2f", num1,num2,num1*num2);
return 0;
}
Enter the first number
3.125
Enter the second number
2.415
The product of 3.125000 and 2.415000 is 7.55
Program 5: Multiply Two Numbers and Round it up to Two Decimal Places
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 using the format specifier.
- 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(float num1, float num2); //Function Declaration
int main()
{
float num1,num2; //Number Declaration
printf("Enter the first number\n");
scanf("%f",&num1); //Initializing the first number
printf("Enter the second number\n");
scanf("%f",&num2); //Initializing the second number
multiply(num1,num2); //Function Call
return 0;
}
void multiply(float num1, float num2) //Function Definition
{
//Print the product of the two numbers by rounding it up to 2 decimal places
printf("The product of %f and %f is %0.2f", num1,num2,num1*num2);
}
Enter the first number
2.498
Enter the second number
4.328
The product of 2.498000 and 4.328000 is 10.81