I want to print the numbers from 1 to100 in the format
1-------------9
10------------19
20-------------29
30--------------39
40-----------49
50-----------59
60-----------------69
70---------------79
80---------------89
90-----------------99
I want to print the numbers from 1 to100 in the format
1-------------9
10------------19
20-------------29
30--------------39
40-----------49
50-----------59
60-----------------69
70---------------79
80---------------89
90-----------------99
Just check whether the number modulus 10 equals 9:
if (number % 10 == 9) …
The modulus operator (%) will give you the remainder of a division. When you divide a number by 10, the "remainder" will give you the last digit.
int lastDigit = number % 10; // this is the remainder of (number/10)
if (lastDigit == 9)
{
// whatever you want to do here
}
if ( x == 0x09 || x == 0x13 || x == 0x1d || x == 0x27 || x == 0x31 || x == 0x3b || x == 0x45 || x == 0x4f || x == 0x59 || x == 0x63 )
or
if ( strchr( "\x09\x13\x1d\x27\x31\x3b\x45\x4f\x59\x63", x ) )
For those with no sense of the ridiculous, why are these more insane than x%10 == 9
? in x%10==9
you have introduced two magic numbers rather than several, and turned a structured problem with no conditional behaviour (print rows, each row having columns) into a single loop with conditional behaviour.
int i = 0;
while (i < 100)
{
i+=1;
while(i % 10 != 0)
{
Console.Write(i);
i+=1;
}
Console.Write(Environment.NewLine);
}
for (i=1;i<=100;i++)
{
if (i%10==0)
{
if (i==10)
cout<<i-9<<"--------"<<i-1<<endl;
else
cout<<i-10<<"--------"<<i-1<<endl;
}
}
for (var i = 9; i < 100; i += 10)
Console.WriteLine(((i==9)?1:i-9) + "----------" + i);
Should print pretty much exactly what you asked.
for (var i = 9; i < 100; i += 10)
Console.WriteLine(i);
Will print just your result