1、for循环嵌套----最基础题目:求阶乘的和

 int sum = ;
int n = int.Parse(Console.ReadLine());
for (int i = ; i < n; i++)
{
int sum1 = ;//定义变量sum1,每次循环都赋予其初始值1,求阶乘专用
for (int j = ; j <= i+; j++)
{
sum1 = sum1 * j;
}
sum = sum + sum1;//将每次阶乘的和相加
}
Console.WriteLine(sum);

2、for循环的穷举

例:100元买2元的铅笔,5元的铅笔盒,10元的文件夹,15元的彩笔,刚好花光,每样物品必须有一种,一共有多少种可能性?

int count = ;
for (int qb = ;qb<;qb++)
{
for (int he = ; he < ; he++)
{
for (int jia = ; jia < ; jia++)
{
for (int cai = ; cai < ;cai++ )
{
if (qb * + he * + jia*+cai*== )
{
count++;
Console.WriteLine("铅笔:{0},铅笔盒:{1},文件夹:{2},彩笔:{3}", qb, he,jia,cai);
}
}
}
}
}
Console.WriteLine(count);

3、用 for 循环的嵌套打印一个菱形

效果图:

             Console.WriteLine("请输入边长:");
int ii = int.Parse(Console.ReadLine());
Console.WriteLine("打印出来的菱形为:");
//打印上面的三角形
for (int g = ; g < ii; g++)
{ for (int n = ii - g; n > ; n--)
{
Console.Write(" ");
}
for (int m = ; m <= g; m++)
{
Console.Write(" #");
} for (int p = ; p <= g; p++)
{
Console.Write(" #");
}
Console.WriteLine(" #");
}
//打印下面的三角形
for (int j = ; j < ii - ; j++)
{
for (int a = ; a <= j + ; a++)
{
Console.Write(" ");
}
for (int b = ii - j; b > ; b--)
{
Console.Write(" #");
}
for (int c = ii - j; c > ; c--)
{
Console.Write(" #");
}
Console.WriteLine(" #");
}