user3296356
user3296356

Reputation: 31

Printing 100 to 1 ?How?

print 10 number on each line , Below Is The Code Of What I Did But Still Not Successful , Help

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;

            for (i =100; i>=1; i--)
            {
                       Console.Write(i);

            }
            Console.ReadLine();

        }
    }
}

Upvotes: 2

Views: 671

Answers (8)

Raging Bull
Raging Bull

Reputation: 18747

Try this:

for (i = 100; i >=1; i--)
{
    if(i%10==0)                 //if 10 numbers are printed
        Console.WriteLine();    //then line break
    Console.Write(i+" ");       //print the number with a space character
}
Console.ReadLine();

It will print 10 number in each line.

Result:

100 99 98 97 96 95 94 93 92 91 

90 89 88 87 86 85 84 83 82 81 

80 79 78 77 76 75 74 73 72 71 

70 69 68 67 66 65 64 63 62 61 

60 59 58 57 56 55 54 53 52 51 

50 49 48 47 46 45 44 43 42 41 

40 39 38 37 36 35 34 33 32 31 

30 29 28 27 26 25 24 23 22 21 

20 19 18 17 16 15 14 13 12 11 

10 9 8 7 6 5 4 3 2 1 

See result in ideone.

Upvotes: 2

w.b
w.b

Reputation: 11228

Enumerable.Range(0, 10).Reverse().ToList().ForEach(n =>
    {
        Enumerable.Range(n * 10, 10).Select(i => i + 1).Reverse().ToList().ForEach(i => Console.Write(i + " "));
        Console.WriteLine();
    });

// output: 100 99 98 97 96 95 94 93 92 91
//         90 89 88 87 86 85 84 83 82 81
//         80 79 78 77 76 75 74 73 72 71
//         70 69 68 67 66 65 64 63 62 61
//         60 59 58 57 56 55 54 53 52 51
//         50 49 48 47 46 45 44 43 42 41
//         40 39 38 37 36 35 34 33 32 31
//         30 29 28 27 26 25 24 23 22 21
//         20 19 18 17 16 15 14 13 12 11
//         10 9 8 7 6 5 4 3 2 1

Upvotes: 1

d.moncada
d.moncada

Reputation: 17402

You can also use a StringBuilder to build the 10 numbers per line, but concatenating like so should yield sufficient results (since it's only 100 numbers)

foreach (var number in Enumerable.Range(1, 100).ToArray().Reverse())
{
    Console.Write((number % 10 == 0) ? "\n" : (number.ToString() + " "));
}

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59262

using System;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;

            for (i = 100; i > 0; i--)
            {
                if(i%10==0)Console.WriteLine();
                Console.Write(i);
            }
            Console.ReadLine();

        }
    }
}

Updated Demo:http://ideone.com/st4i8n

Upvotes: 1

jdphenix
jdphenix

Reputation: 15435

This might do what you need:

  Enumerable.Range(1, 100).Reverse()
    .ToList()
    .ForEach(i => Console.Write(i % 10 == 1 ? i + "\r\n" : i + ", "));

Which will output:

100, 99, 98, 97, 96, 95, 94, 93, 92, 91
90, 89, 88, 87, 86, 85, 84, 83, 82, 81
80, 79, 78, 77, 76, 75, 74, 73, 72, 71
70, 69, 68, 67, 66, 65, 64, 63, 62, 61
60, 59, 58, 57, 56, 55, 54, 53, 52, 51
50, 49, 48, 47, 46, 45, 44, 43, 42, 41
40, 39, 38, 37, 36, 35, 34, 33, 32, 31
30, 29, 28, 27, 26, 25, 24, 23, 22, 21
20, 19, 18, 17, 16, 15, 14, 13, 12, 11
10, 9, 8, 7, 6, 5, 4, 3, 2, 1

Upvotes: 1

pravprab
pravprab

Reputation: 2293

static void Main(string[] args)
{
    for (int i = 100; i >0; i--)
    {
        if (i % 10 == 0)
            Console.WriteLine();
        Console.Write(i);                  
    }
    Console.ReadLine();
}

Upvotes: 1

user3400330
user3400330

Reputation:

Try this

static void Main(string[] args)
       {
            int i=100;

             for (i; i <=100; i--)
             {
                Console.Write(i);
                 if(i==0)
                  {
                   break;
                  }

               }
             Console.ReadLine();   

        }

Edit

Must use

if(i==0){break;}

otherwise this for loop does not end.

Upvotes: 1

Umar Iqbal
Umar Iqbal

Reputation: 689

HundredToOne()
{
    for(int i=100;i>0;i--)
    {       
     if(i%10 == 0)
      Console.WriteLine("\n");

      Console.Write(i);
    }
}

Upvotes: 0

Related Questions