Signup/Sign In

Multiply two numbers and round up to two decimal places

learn C language tutorial

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.

Multiply two numbers and round 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:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Use another variable that will store the product of these two numbers.
  5. Print the result using the format specifier.
  6. 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:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Call a function to perform the multiplication operation.
  5. Use another variable that will store the result of these two numbers.
  6. Print the result using the format specifier.
  7. 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:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Call a function to perform the multiplication operation.
  5. Use another variable that will store the product of these two numbers.
  6. Return the result.
  7. Print the result in the main method with format specifier.
  8. 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:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Without using any other variable directly calculate the product.
  5. Print the result using the format specifier.
  6. 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:

  1. Start
  2. Declare two variables.
  3. Initialize the two variables.
  4. Call a function to perform the multiplication operation.
  5. Without using any other variable directly calculate the product.
  6. Print the result using the format specifier.
  7. 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



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight