第一题
利用内置的加法操作符"+“来产生俩个数之和,利用”*"产生两个数的积
#include<iostream>
using namespace std;
int main(void)
{
int a, b;
cout << "请输入俩个数" << endl;
cin >> a >> b;
cout << "俩个数的和为" << a + b << endl;
cout << "俩个数的积" << a * b << endl;
system("pause");
return 0;
}
第二题
用for循环编程,求从50——100所有的自然数的和,然后用while重写该程序
for形式
#include<iostream>
using namespace std;
int main(void)
{
int sum = 0;
for (int i = 50; i <= 100; i++)
{
sum += i;
}
cout << sum << endl;
system("pause");
return 0;
}
while形式
#include<iostream>
using namespace std;
int main(void)
{
int sum = 0;
int i = 50;
while (i <= 100)
{
sum += i;
i++;
}
cout << sum << endl;
system("pause");
return 0;
}
第三题
输出10——0递减的自然数
#include<iostream>
using namespace std;
int main(void)
{
for (int i = 10; i >= 0; i--)
{
cout << i<< " ";
}
cout << endl;
system("pause");
return 0;
}
第四题
编写程序,输出用户输入的俩个数比较大的那个
#include<iostream>
using namespace std;
int main(void)
{
int a, b;
cout << "请输入要比较的两个数" << endl;
cin >> a >> b;
if (a > b)
{
cout << a << endl;
}
else
cout << b << endl;
system("pause");
return 0;
}
第五题
输入一组数,输出信息说明其中有多少个负数
#include<iostream>
using namespace std;
int main(void)
{
int amount = 0;
int value;
while (cin >> value)
{
if (value <= 0)
{
amount++;
}
//清空屏幕操作,使得循环未结束之前的输出被刷新掉,只留最后一次结果
system("cls");
cout << amount<< endl;
}
system("pause");
return 0;
}
第六题
提示用户输入俩个数并将俩个数范围内的每个数字写到标准输出
#include<iostream>
using namespace std;
int main(void)
{
int a, b;
cout << "请输入俩个数" << endl;
cin >> a >> b;
int lowwer;
int upper;
if (a > b)
{
upper = a;
lowwer = b;
}
else
{
upper = b;
lowwer = a;
}
for (int i = lowwer + 1; i < upper; i++)
{
cout << i <<" ";
}
cout << endl;
system("pause");
return 0;
}