Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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);
                Console.ReadLine();

               
            }
        }
    }
}


Thanks in advance!
Posted
Updated 4-Jul-13 0:55am
v2
Comments
Joezer BH 4-Jul-13 6:59am    
[Warning] Home Work detected :blinkingRedLight:

I think the code could be as below. But I'm wondering what's it for?
C#
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);
    }
}
 
Share this answer
 
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);
C#
if (i==4)
   print(@"(you have been reached 4th position of ascending order)");
else
  Console.WriteLine(i);


Good luck,
Edo
 
Share this answer
 
v2
Change you code to this:


C#
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);

      Console.ReadLine();
}


Then add the method:

C#
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?[^]
 
Share this answer
 
v3
C#
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++;
}
Console.ReadLine();
 
Share this answer
 
Make sure that the 4th position was reached by using a comparison with 4 in an if statement.
C#
i++;
if(i == 4)
{
    //output the "special" message
}
else
{
    // output only the number without any "special" message
}


Regards,

— Manfred
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900