C Program to Multiply Two Numbers Given by User and Display Product
Program
#include<stdio.h>
#include<conio.h>
int main()
{
float a, b, prod;
clrscr(); // Clears the screen
printf("Enter the value of a: ");
scanf("%f", &a);
printf("Enter the value of b: ");
scanf("%f", &b);
prod = a * b;
printf("Product of %f and %f is %f.",a,b,prod);
getch(); // Holds the output
return 0;
}
Output of the above program :
Enter the value of a: 3.3 ↲ Enter the value of b: 5 ↲ Sum of 3.300000 and 5.000000 is 16.500000. Note: ↲ indicates ENTER is pressed.
Code Explanation
The following is an explanation of the provided C code:
This code is a simple C program that calculates the product of two floating-point numbers input by the user. Here's a breakdown of what the code does:
-
#include<stdio.h>: This line includes the standard input-output library, which contains functions like
printf
andscanf
used for input and output operations. -
#include<conio.h>: This line includes the "conio" (console input-output) library. This library provides functions like
clrscr
andgetch
for console manipulation. However, it's important to note that the use ofconio.h
is not part of the standard C library and might not be available on all systems. - int main(): This is the main function where the program's execution starts. It returns an integer value to indicate the program's exit status.
-
float a, b, prod;: Three variables are declared:
a
andb
, which will store the user's input values, andprod
, which will store the product of those values. -
clrscr();: This function is used to clear the console screen. It's a function from the
conio.h
library. -
printf("Enter the value of a: ");: This line prints a message to the console, asking the user to enter the value of variable
a
. -
scanf("%f", &a);: This line reads a floating-point number input from the user and stores it in the variable
a
. -
printf("Enter the value of b: ");: This line prints a message to the console, asking the user to enter the value of variable
b
. -
scanf("%f", &b);: Similar to the previous
scanf
call, this line reads a floating-point number input from the user and stores it in the variableb
. -
prod = a * b;: This line calculates the product of
a
andb
and assigns the result to theprod
variable. -
printf("Product of %f and %f is %f.",a,b,prod);: This line prints the calculated product along with the values of
a
andb
. -
getch();: This function waits for a single character input from the user. It's used here to pause the program's execution and display the output until the user presses a key. This is another function from the
conio.h
library. -
return 0;: The
main
function returns 0 to indicate successful execution of the program.
Keep in mind that the use of conio.h
and its functions might not be compatible with all compilers and systems, especially on modern platforms. If you're aiming for portability and compatibility, you might want to consider using standard C functions for console input and output instead.