Skip to content

Latest commit

 

History

History
86 lines (65 loc) · 2.89 KB

File metadata and controls

86 lines (65 loc) · 2.89 KB

C 程序:两个浮点数相乘

原文: https://beginnersbook.com/2017/09/c-program-to-multiply-two-floating-point-numbers/

程序将两个浮点数相乘并将乘积显示为输出。

示例 1:显示两个浮点数乘积的程序

在该程序中,要求用户输入两个浮点数,程序将输入的值存储到变量num1num2中。然后程序将输入的数字相乘并将乘积显示为输出。

#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 编程概念的知识:

  1. C - 函数
  2. 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 程序

  1. C 程序:相加两个数字
  2. C 程序:打印用户输入的整数
  3. C 中的Hello World程序
  4. C 程序:查找给定范围内的回文数
  5. C 程序:将小写字符串转换为大写