do...while循环语句输出0-9,并用do while循环语句实现水仙花数;for循环语句输出0-9,并用该语句实现逢7过;用嵌套循环实现九九乘法表。

do while循环语句

满足循环条件,执行循环语句;在屏幕上输出0-9这10个数字。

#include<iostream>
using namespace std;
int main() {
	/*do while循环语句:满足循环条件,执行循环语句
	* 语法:do{循环语句}while(循环条件)
	* 注意:与while的区别在于do...while会先执行一次循环语句,在判断循环条件
	*/
	//在屏幕上输出0-9这10个数字
	int num = 0;
	do 
	{
		cout << num << endl;
		num++;
	} while (num < 10);

	system("pause");
	return 0;
}

do while语句输出0-9这10个数字

do while循环语句实现水仙花数

#include<iostream>
using namespace std;
int main() {
	/*水仙花数
	* 是指一个三位数,它的每个位上的3次幂之和等于它本身
	* 例如1^3 + 5^3 + 3^3=153
	*/
	
	int num = 100;
	int a, b, c  = 0;
	cout << "水仙花数为:" << endl;
	do 
	{
		a = num % 10;//个位
		b = num / 10 % 10;//十位
		c = num / 100;//百位
		//d = a*a*a + b*b*b + c*c*c;
		if (num == a*a*a + b*b*b + c*c*c)
		{
			cout << num << endl;
		}
		num++;
	} while (num < 1000);
	system("pause");
	return 0;
}

水仙花数

for循环语句

#include<iostream>
using namespace std;
int main() {
	/*for循环:满足循环条件,执行循环语句
	* 语法:for(起始表达式;条件表达式;末尾循环体){循环语句};
	*/
	for (int num = 0; num < 10; num++) 
	{
		cout << num << endl;
	}
	system("pause");
	return 0;
}

逢7过

#include<iostream>
using namespace std;
int main() {
	/* 逢7过:从1开始数到100,
	*/
	for (int num = 1; num < 100; num++)
	{
		if (num % 7 == 0 || num % 10 == 7 || num / 10 == 7)
		{
			cout << "过" <<"(" <<num<<")" << endl;
		}
		else
		{
			cout << num << endl;
		}
	}
	system("pause");
	return 0;
}

逢7过

嵌套循环

#include<iostream>
using namespace std;
int main() {
	/*嵌套循环*/
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10;j++)
		{
			cout << "* ";
		}
		cout << endl;
	}


	system("pause");
	return 0;
}

测试结果

打印九九乘法表

#include<iostream>
using namespace std;
int main() {
	/*嵌套循环*/
	for (int i = 1; i < 10; i++)
	{
		for (int j = 1; j <= i;j++)
		{
			cout << j << "*" << i << "=" << j * i<<"\t";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值