Table of Contents
Introduction
This program will display the product value of two numbers entered by the user that are of ‘float’ data type, and then the product value will be printed on the screen.
To understand this example, you should know the following C programming concepts:
1. Variables, constants, and Literals in C
2. C data types
3. Programming operators in C
Go through the article named ‘Operators in C’ in the C language series to gain more information about this particular concept.
Program
#include<stdio.h> int main() { float a,b; printf("Enter two values:\n"); scanf("%f %f",&a,&b); float product=a*b; //Calculating the product value printf("Product:%.2f",product); // .2 is used to round of the resultant value return 0; }
Output
Explanation
In the starting, the user is asked to enter two values which are stored in variables ‘a’ and ‘b’ respectively.
float product=a*b; then the product of both the variables is calculated and assigned to a variable named ‘product’ of ‘float’ data type.
Multiplying two ‘float’ values will always give a ‘float’ type value as a result.
printf(“Product: %.2f”,product); the result so obtained is printed on the screen with the use of ‘.2’ in between the format specifier to the round of the resultant value of ‘product’ variable up to 2 decimal places.
0 Comments