tags:

views:

341

answers:

8

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
+8  A: 

Just check whether the number modulus 10 equals 9:

if (number % 10 == 9) …
Konrad Rudolph
+5  A: 

Check out the modulus operator.

Johannes Hoff
+1  A: 
if ( ( num % 10 ) == 9 )  
{  
  // I end in 9  
}
Evän Vrooksövich
+4  A: 

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
}
Robert Cartaino
+1  A: 
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.

Pete Kirkham
I can't upvote this on principle, but I gotta say sometimes I think these are the best kind of answers to homework questions. If someone isn't willing to figure it out themselves on something as relatively simple as this, they're probably not really meant for computer science. I'd rather that they figure that out while in class instead of while working on a critical project with me.
Toji
could this be code-golf?
bguiz
I gave the modulus answer because the OP requested a single loop – but I have to admit that a single loop + modulus here isn’t as clear a solution as two nested loops. Why didn’t you just post this solution?
Konrad Rudolph
A: 
int i = 0;
while (i < 100)
{
  i+=1; 
  while(i % 10 != 0)
  {
    Console.Write(i);
    i+=1;
  }
  Console.Write(Environment.NewLine);
}
Rik
There is no way these two while loops will ever stop.
Mez
Damnit, forgot the increment.
Rik
A: 
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;
    }
}
mjd
+2  A: 
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

Ryan Cook