C++练习1(基础入门版本)

第一题

利用内置的加法操作符"+“来产生俩个数之和,利用”*"产生两个数的积

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宴师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值