练习案例:水仙花数
案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
例如:1^3 + 5^3+ 3^3 = 153
请利用do...while语句,求出所有3位数中的水仙花数
//获取个位 153%10=3 对数字取模于10可以获取到个位
//获取十位 153/10 15%10=5 先整除于10得到二位数在取模于10,得到十位
//获取百位 153/100 就可以得到百位
int num1 = 100;
do
{
int a = 0; //个位
int b = 0; //十位
int c = 0; //百位
a = num1 % 10;
b = num1 / 10 % 10;
c = num1 / 100;
if (a*a*a+b*b*b+c*c*c==num1)
{
cout << num1 << endl;
}
num1++;
} while (num1<1000);
for循环练习九九乘法表
for (int i=1;i<=9;i++)
{
for (int j=1;j<=i;j++)
{
cout << j << "*" << i << "=" << j * i<<" ";
}
cout << endl;
}
while循环练习九九乘法表
int i = 1;
while (i <= 9)
{
int j = 1;
while (j<=i)
{
cout << j << "*" << i <<"="<<j * i << " ";
j++;
}
i++;
cout << endl;
}
do while练习九九乘法表
int i = 1;
do
{
int j = 1;
do
{
cout << j << "*" << i << "=" << j * i << " ";
j++;
} while (j<=i);
i++;
cout << endl;
} while (i<=9);
不明白的地方已经搞明白了,嘻嘻