21xrx.com
2024-11-05 18:27:39 Tuesday
登录
文章检索 我的文章 写文章
C++程序:比较两个整数和三个浮点数的大小
2023-06-23 14:10:16 深夜i     --     --
C++程序 比较 两个整数 三个浮点数 大小

C++程序可以用来比较两个整数和三个浮点数的大小,并输出比较结果。

首先,我们需要定义两个整数和三个浮点数的变量,如下所示:


int num1, num2;

float num3, num4, num5;

然后,我们可以通过用户的输入来给这些变量赋值:


cout << "Enter two integers: ";

cin >> num1 >> num2;

cout << "Enter three floating point numbers: ";

cin >> num3 >> num4 >> num5;

接下来,使用if语句来比较两个整数的大小并输出结果:


if (num1 > num2)

  cout << num1 << " is greater than " << num2 << endl;

else if (num1 < num2)

  cout << num2 << " is greater than " << num1 << endl;

else

  cout << "Both numbers are equal" << endl;

然后,使用if语句来比较三个浮点数的大小并输出结果:


if (num3 > num4 && num3 > num5)

  cout << num3 << " is the greatest." << endl;

else if (num4 > num3 && num4 > num5)

  cout << num4 << " is the greatest." << endl;

else if (num5 > num3 && num5 > num4)

  cout << num5 << " is the greatest." << endl;

else

  cout << "All three numbers are equal" << endl;

最后,完整的程序如下:


#include <iostream>

using namespace std;

int main() {

  int num1, num2;

  float num3, num4, num5;

  cout << "Enter two integers: ";

  cin >> num1 >> num2;

  cout << "Enter three floating point numbers: ";

  cin >> num3 >> num4 >> num5;

  if (num1 > num2)

    cout << num1 << " is greater than " << num2 << endl;

   else if (num1 < num2)

    cout << num2 << " is greater than " << num1 << endl;

   else

    cout << "Both numbers are equal" << endl;

  

  if (num3 > num4 && num3 > num5)

    cout << num3 << " is the greatest." << endl;

   else if (num4 > num3 && num4 > num5)

    cout << num4 << " is the greatest." << endl;

   else if (num5 > num3 && num5 > num4)

    cout << num5 << " is the greatest." << endl;

   else

    cout << "All three numbers are equal" << endl;

  

  return 0;

}

通过这个程序,我们可以比较两个整数和三个浮点数的大小,并输出比较结果。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复