Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The exercise is:

Write a C# program to that takes a number as input and display it four times in a row (separated by blank spaces), and then four times in the next row, with no separation. You should do it two times: Use Console. Write and then use {0}. Go to the editor
Test Data:
Enter a digit: 25
Expected Output:
25 25 25 25
25252525
25 25 25 25
25252525

I have already created a solution but want to use a loop method. Here is the first one I did (that worked):

C#
using System;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Enter a number: ");
		int x =Convert.ToInt32(Console.ReadLine());
	        		
		Console.WriteLine("The output is: ");
		Console.WriteLine("{0} {0} {0} {0}", x);
		Console.WriteLine("{0}{0}{0}{0}", x);
		Console.WriteLine("{0} {0} {0} {0}", x);
		Console.WriteLine("{0}{0}{0}{0}", x);
		
		Console.ReadLine();
		
	}
}


What I have tried:

And here is my incomplete code:

C#
public static void Main()
	{
		Console.WriteLine("Enter a number: ");
		int x =Convert.ToInt32(Console.ReadLine());
	    int i = 0;
		Console.WriteLine("The output is: ");
		
		do 
		{
		  Console.Write(x+ " ");
		  i++;
			
		}
		while (i<=3);
			Console.Write("\n{0}{0}{0}{0}", x);
		
			
	}

The output of this is just: (25, for example, is the user input)
Enter a number: 25
The output is: 
25 25 25 25 
25252525

Thank you so much!
Posted
Updated 24-Aug-20 17:39pm
v2
Comments
F-ES Sitecore 24-Aug-20 9:03am    
For starters google how to use "while" and compare the examples with yours. Your while loop is actually doing nothing at all, you should be able to work out why from the documentation. Also look at the "for" loop as that is better for looping a set number of times.
CHill60 24-Aug-20 9:38am    
Er - the while loop is looping 4 times for i = 0,1,2,3. I agree a 'for' loop would be better
F-ES Sitecore 24-Aug-20 11:06am    
Nothing is changing i and the while statement has a semi-colon after it meaning it just does the while bit, not the line of code after. See me after class :D
Sandeep Mewara 24-Aug-20 11:19am    
It's the line indentation there creating confusion.
while with semicolon does it job along with do. Line next to while is intended once only.

Do I also need to catch up post class? :P
CHill60 24-Aug-20 11:46am    
:laugh:

The concept you are possibly looking for is Nested Loops[^] i.e. you want an outer loop for 2 iterations.

As @f-es-sitecore says, a for-loop is better when you know up front how many times you want to do the loop.

I personally would probably do something like - pseudo code here only
C#
//Set up a for-loop for 2 iterations
//output the number four times with spaces
//output the number four times without spaces
Console.Write("\n{0}{0}{0}{0}", x);
//End of the for-loop
Note that you have already done the "without spaces" bit properly - but it does need to be inside the loop not outside it
 
Share this answer
 
Comments
BillWoodruff 25-Aug-20 20:19pm    
+5 a fine example of teaching, rather than writing the OP's code for them.
CHill60 26-Aug-20 3:58am    
Thank you - it's how I learn best so I do try to pass it on / pay it forward
There would be multiple ways. Looking at your query and code, I am assuming you are way basic and have just started coding. So, one of the easiest way for you to achieve your result in your code only would be:
C#
noOfTimes = 2;
for(int j=0; j< noOfTimes; j++)
{
    do 
    {
      Console.Write(x+ " ");
      i++;	
    }
    while (i<=3);
    
    Console.Write("\n{0}{0}{0}{0}", x);
}

Read about loops in C#: for, while, do-while.

References:
for statement - C# reference | Microsoft Docs[^]
while - C# Reference | Microsoft Docs[^]
do - C# Reference | Microsoft Docs[^]

Try the same problem statement post reading above and see if you achieve the same in better way.
 
Share this answer
 
Comments
JJTY 24-Aug-20 9:41am    
@Sandeep Yes! You are right haha, this is just my 2nd-day studying c#, I feel bad huhu XD. I see now what I am missing. Thank you so so much, I will study about loops more. Thank you all for your comments. Sandeep Mewara and F-ES Sitecore!
I got it!

using System;
					
public class Program
{
	public static void Main()
	{
		int i=0;
		int j=0;
				
		for (j=0; j<2;j++)
		{
			for (i=0; i<=4;i++)
			{
				Console.Write("{0,-3}","25");
			}
				Console.WriteLine("\n{0}{0}{0}{0}","25");			
		}
					
	}
}	


Thank you for all your help! I really appreciate it <3
 
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