程序将两个浮点数相乘并将乘积显示为输出。
示例 1:显示两个浮点数乘积的程序
在该程序中,要求用户输入两个浮点数,程序将输入的值存储到变量num1
和num2
中。然后程序将输入的数字相乘并将乘积显示为输出。
#include <stdio.h>
int main(){
float num1, num2, product;
printf("Enter first Number: ");
scanf("%f", &num1);
printf("Enter second Number: ");
scanf("%f", &num2);
//Multiply num1 and num2
product = num1 * num2;
// Displaying result up to 3 decimal places.
printf("Product of entered numbers is:%.3f", product);
return 0;
}
输出:
Enter first Number: 12.761
Enter second Number: 89.23
Product of entered numbers is:1138.664
示例 2:使用函数将两个数相乘的程序
在这个程序中,我们创建了一个用户定义的函数product()
,它在函数调用期间将我们传递给它的数字相乘。此函数返回这些数字的乘积。要了解该程序,您应该具备以下 C 编程概念的知识:
#include <stdio.h>
/* Creating a user defined function product that
* multiplies the numbers that are passed as an argument
* to this function. It returns the product of these numbers
*/
float product(float a, float b){
return a*b;
}
int main()
{
float num1, num2, prod;
printf("Enter first Number: ");
scanf("%f", &num1);
printf("Enter second Number: ");
scanf("%f", &num2);
// Calling product function
prod = product(num1, num2);
// Displaying result up to 3 decimal places.
printf("Product of entered numbers is:%.3f", prod);
return 0;
}
输出:
Enter first Number: 12.761
Enter second Number: 89.23
Product of entered numbers is:1138.664
查看这些相关的C 程序: