12,243,303 members (49,599 online)
Rate this:
See more:
Hi all,

I have a few doubts in how to modify my program written in C#.
This program gives below output in ascending order.

The output:
```1 2 3 4 5 6 7 8 9 10
```

My question is :

I need to display the message when the 4th position in the output is reached.

The message should be like this ("You have reached 4th position of ascending order").

After getting the 4 th postions the code again will start execute the remaining order, which means 5th,6th,7th,8th,9th and 10th.

The expected output is:
```1

2

3

(" you have been reached 4th position of ascending order")

5

6

7

8

9

10
```

Please find my code below and tell me were I should alter it. Please let me know! Advice is very appreciable.

My code is:
```using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while(i<10)
{

i = i + 1;
Console.WriteLine(i);

}
}
}
}
```

Posted 4-Jul-13 1:48am
stellus1.2K
Edited 4-Jul-13 1:55am
v2
Maimonides 4-Jul-13 6:59am

Rate this:

## Solution 2

I think the code could be as below. But I'm wondering what's it for?
```for (int i = 1; i <= 10; i++)
{
if (i == 4)
{
Console.WriteLine(" you have been reached 4th position of ascending order");
}
else
{
Console.WriteLine(i);
}
}```
Rate this:

## Solution 3

This is indeed a very complex question, I would imagine that perhaps if you'd use IF THEN ELSE[^] you just might be able to pull this off.

Something like:

Console.WriteLine(i);
```if (i==4)
print(@"(you have been reached 4th position of ascending order)");
else
Console.WriteLine(i);```

Good luck,
Edo
v2
Rate this:

## Solution 4

Change you code to this:

```int i = 0;
int n = 4;
while(i<10)
{

i = i + 1;
if(i = n)
Console.WriteLine("You have reached the {0}{1} position of ascending order", n, GetEnding(n));
else
Console.WriteLine(i);

}```

```private string GetEnding(int n)
{
var ones = n % 10;
switch(ones)
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";

}
```

That should do what you want, it should also make it that if you change n it continues to work. (i believe).

An alternative to the GetEnding method can be found in this thread:

Is there an easy way in .NET to get “st”, “nd”, “rd” and “th” endings for numbers?[^]
v3
Rate this:

## Solution 5

```int i=1;
While (i<=10)
{
if (i == 4)
{
Console.WriteLine(" you have been reached 4th position of ascending order");
}
else
{
Console.WriteLine(i);
}
i++;
}
```
Rate this:

## Solution 1

Make sure that the 4th position was reached by using a comparison with 4 in an if statement.
```i++;
if(i == 4)
{
//output the "special" message
}
else
{
// output only the number without any "special" message
}
```

Regards,

— Manfred
v3

Top Experts
Last 24hrsThis month
 OriginalGriff 360 Richard MacCutchan 240 George Jonsson 90 Dave Kreskowiak 66 Matt T Heffron 63
 OriginalGriff 185 BillWoodruff 40 George Jonsson 40 Member 12323765 20 SagSD 10