Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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);
                Console.ReadLine();
 
               
            }
        }
    }
}
 
Thanks in advance!
Posted 4-Jul-13 0:48am
stellus991
Edited 4-Jul-13 0:55am
v2
Comments
Maimonides at 4-Jul-13 6:59am
   
[Warning] Home Work detected :blinkingRedLight:
Rate this: bad
good
Please Sign up or sign in to vote.

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);
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

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);
 
      Console.ReadLine();
}
 
Then add the method:
 
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?[^]
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

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++;
}
Console.ReadLine();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 375
1 RyanDev 135
2 CHill60 120
3 Richard MacCutchan 115
4 Sergey Alexandrovich Kryukov 110


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid