Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi.. i want to print 5 digit number in reverse order..

ex.. 49999,49998,49997,49996.....so on

one more think number of print depend on user input
i.e if user input 3 then it display only 3 print as 49999,49998,49997



any idea?....thanks in advance...

What I have tried:

C#
for(int i =50000; i >=printCopies; i--)
{
   reslt += i.ToString("00000") + System.Environment.NewLine;
}
Posted
Updated 14-Jul-20 23:12pm
v2
Comments
Patrice T 14-Jul-20 16:41pm    
What is the problem with your code?
Sam_gaik 14-Jul-20 17:01pm    
It does not work as expected..
Patrice T 14-Jul-20 17:12pm    
Elaborate.

Easiest way to understand is like this:
C#
StringBuilder sb = new StringBuilder();
for (int i = 0; i < userInputLimit; i++)
   {
   sb.AppendLine((50000 - i).ToString());
   }
string result = sb.ToString();
Using a StringBuilder is better than concatenating strings as it "expands" automatically instead of creating a new string every time you add a new bit to the old one.
 
Share this answer
 
Comments
Sam_gaik 15-Jul-20 1:53am    
It works...thnks
OriginalGriff 15-Jul-20 2:04am    
You're welcome!

Am I missing something here? Do you just want to print a line of comma separated decreasing values like this:


C#
 int copies = 3;
 int initialValue = 5000;
 string separator = string.Empty;
 for (int i=0;i <copies;i++)
 {
     Console.Write(separator);
     Console.Write(initialValue - i);
     if (separator.Length==0) separator = ",";
 }
Console.WriteLine();

If you want an IEnumerable so that you can use Linq to select the output. Try this


C#
  IEnumerable <int > numbers = Enumerable.Range(initialValue-copies+1, copies).Reverse();
  IEnumerable <string > numberStrings = Enumerable.Range(initialValue , copies).Select((x,i) = > (x-(i*2)).ToString());
 //print  one number per line
           foreach (int num in numbers)
            {
                Console.WriteLine(num);
            }
//use IEnumerable <string > to join the strings together to give a comma separated string value
            Console.WriteLine(string.Join(',', numberStrings));
 
Share this answer
 
Comments
Sam_gaik 15-Jul-20 12:00pm    
Thanks for your suggestion... !
That looks to be on the right track.

My only question/recommendation from what you have shown is in regards to reslt :
It should NOT be a String variable, because it is immutable. If this is the case; you should be replacing it with a StringBuilder, which is designed for this.
C#
StringBuilder sbResult = new StringBuilder();

for(int i =50000; i >=printCopies; i--)
{
	sbResult.AppendLine( i.ToString("00000") );
}

reslt = sbResult.ToString();
References:
MS Docs: Strings - C# Programming Guide[^]
MS Docs: StringBuilder Class (System.Text)[^]
 
Share this answer
 

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