循环习题(一)
100到10000各位数和为7
int number = 100;
int num = 0;
while (number <10000) {
if (number>100 &&number<=700) {
if ((number%10) + (number/10)%10 + number/100 == 7) {
++num;
}
}else if (number>1000 && number<=7000) {
if ((number%10) +(number/10)%10 + (number/100)%10 + number/1000 == 7) { //%10取个位数
++num;
}
}
number++;
}
Console.WriteLine ("{0}",num);
for (int number = 100; number <= 7000; number++) {
int a, b, c, d;
a = number % 10;
b = (number / 10) % 10;
c = (number / 100) % 10;
d = (number / 1000) % 10;
if (a+b+c+d == 7) {
Console.WriteLine (number);
}
}
for (int number = 100; number <= 7000; number++) {
int a, b, c, d;
a = number % 10;
b = (number / 10) % 10;
c = (number / 100) % 10;
d = (number / 1000) % 10;
if (a+b+c+d == 7) {
Console.WriteLine (number);
}
}
0到100之间的偶数
int num = 1;
while (num<=100) {
if (num%2 == 0) {
Console.WriteLine (num);
}
num++;
}
1到100的和
int num = 1;
int sum = 0;
while (num <=100) {
sum += num;
num++;
}
Console.WriteLine(sum);
在终端循环输入数字,出现负数停止运行
while (true) {
int n =int.Parse( Console.ReadLine ());
if (n<0) {
break;
}
}
用continue关键字打印1到100之间不是3的倍数
int num = 1;
while (num<=100) {
if (num %3 == 0) {
++num;
continue;
}
Console.WriteLine ("{0}",num);
++num;
}
sum = 1+2+3+...+N 求不超过10000的最大值和N.
int sum = 0;
int N = 0;
do {
++N;
sum += N;
} while (sum<10000);
Console.WriteLine (sum-N);
Console.WriteLine (N-1);
打印出1到100之间不是7的倍数且不包含7的数
for (int i = 0; i < 99; i++) {
if (i%7 != 0 && i%10 ==7 && i/10 !=7 ) {
Console.WriteLine ("{0}",i);
}
}
for (int i = 1; i <=100; i++) {
if (i%7 !=0 && !(i.ToString().Contains("7"))) {
Console.WriteLine ("{0}",i);
}
}
打印 1
1 2
1 2 3
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
Console.Write ("{0}",j);
}
Console.WriteLine ();
// Console.Write ("\n");
}
//打印99乘法表
for (int i = 1; i <=9; i++) {
for (int j = 1; j <=i; j++) {
Console.Write ("{0}*{1}={2} ",j,i,j*i);
}
Console.WriteLine ();
}