In this article, we are going to learn multiply and division operations on two numbers using a C programming language. We will learn multiplication on two numbers and print the result on the console. Similarly, we will perform division on two numbers and will learn to find Quotient and Remainder. We will print the results using printf() function.
1. C Program to Multiply Two Numbers entered by user
In this example, we are taking two floating-point numbers or you can say decimal point numbers from the user. We are using the float data type of C language, which can hold an integer or a decimal number.%lf is used in scanf() function to allow users to enter numbers with the decimal point. When we perform a multiplication that result is saved in the product variable which is also floating.
So to print the output with decimal results we are using %.2lf in printf() function to display numbers up to 2 decimal points in the results
#include <stdio.h>
int main()
{
float num1, num2, product;
printf("Please enter two numbers: ");
scanf("%f %f", &num1, &num2);
// Perform multiplication on two numbers
product = num1 * num2;
printf("The Multiplication Product of %.2f * %.2f = %.2lf",num1,num2, product);
return 0;
}
Output
Please enter two numbers: 12.4
10
The Multiplication Product of 12.40 * 10.00 = 124.00
2.C Program to Divide two numbers and find Quotient and Remainder
In this example, we are taking two integers from the user to perform the division. The division of these numbers may result in 0 remainders or it may result in a decimal Quotient so we have taken the quotient as a float to save a decimal result.
The remainder will always be an integer so we have taken it as a simple integer type.
Now we will perform the division after taking the inputs from the user and saving them in our variables. Once we have the results we will print them using printf() function.
C Program to Compute Quotient and Remainder
#include <stdio.h>
int main() {
int num1, num2;
float quotient;
int remainder;
printf("Please enter First numbers: ");
scanf("%d", &num1);
printf("Please enter Second numbers: ");
scanf("%d", &num2);
// method to divide two numbers
quotient = num1/ num2;
// method to calculate remainder
remainder = num1% num2;
printf("The result of division of %d/%d = %f\n", num1,num2, quotient);
printf("The remainder after division of %d/%d is = %d",num1,num2, remainder);
return 0;
}
Output
Please enter First numbers: 20
Please enter Second numbers: 3
The result of division of 20/3 = 6.000000
The remainder after division of 20/3 is = 2