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:

  1. #include<stdio.h>: This line includes the standard input-output library, which contains functions like printf and scanf used for input and output operations.
  2. #include<conio.h>: This line includes the "conio" (console input-output) library. This library provides functions like clrscr and getch for console manipulation. However, it's important to note that the use of conio.h is not part of the standard C library and might not be available on all systems.
  3. 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.
  4. float a, b, prod;: Three variables are declared: a and b, which will store the user's input values, and prod, which will store the product of those values.
  5. clrscr();: This function is used to clear the console screen. It's a function from the conio.h library.
  6. printf("Enter the value of a: ");: This line prints a message to the console, asking the user to enter the value of variable a.
  7. scanf("%f", &a);: This line reads a floating-point number input from the user and stores it in the variable a.
  8. printf("Enter the value of b: ");: This line prints a message to the console, asking the user to enter the value of variable b.
  9. 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 variable b.
  10. prod = a * b;: This line calculates the product of a and b and assigns the result to the prod variable.
  11. printf("Product of %f and %f is %f.",a,b,prod);: This line prints the calculated product along with the values of a and b.
  12. 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.
  13. 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.