C# Exercise①——Nine-Nine Multiplication Table, Narcissus Number

I have been looking at the loop statement in C# for the past two days, and found that no matter what language it is, it is inseparable from the multiplication table of ninety-nine and the number of daffodils. I first tried to study these two examples myself, and then compared them with what Teacher Xiao Yang said. I was surprised to find that there is not much difference. I am still very happy!

 

One, ninety-nine multiplication table

1. Nine to nine multiplication table (rectangular)

for (int j = 1; j <= 9; j++)                        //显示行数
{
    for (int i = 1; i <= 9; i++)                    //显示列数
    {
        Console.Write("{0}*{1}={2}\t",i,j,j*i);     //\t水平制表符,  Write不换行
    }
    Console.WriteLine();   //满足九列就另起一行
}
Console.ReadKey();

 

2. Nine-Nine Multiplication Table (Triangle)

for (int j = 1; j <= 9; j++)         //显示行数
{
    for (int i = 1; i <= j; i++)     //显示列数,随着行数的增加,个数也增加
    {
        Console.Write("{0}*{1}={2}\t", i, j, i * i);
    }
    Console.WriteLine();
}
Console.ReadKey(); 

 

2. The number of daffodils from 100 to 999.

What is the number of daffodils: For example, 153 1*1*1+5*5*5+3*3*3 =153

int bai = 0;                           //百位
int shi = 0;                           //十位
int ge = 0;                            //个位
for (int i = 100; i <= 999; i++)       //循环
{
    bai = i / 100;                     //百位/100
    shi = i % 100 / 10;                //十位取余100/10
    ge = i % 100 % 10;                 //各位取余100取余10

    if (bai*bai*bai+shi*shi*shi+ge*ge*ge==i)    //如果个十百位加起来等于i
    {
        Console.WriteLine(i);                   //输出水仙花数
    }
}
Console.ReadKey();

 

Accumulation lies in the usual bit by bit, except for each point of knowledge, we have to understand each small example, small practice, we have to carefully figure out the principle, so as to find the connection with other knowledge.

 

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/108278831