w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Print a number four times in separate rows


C# Sharp Basic: Exercise-12 with Solution

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}

C# Sharp Exercises: Print a number four times in separate rows

Sample Solution:

C# Sharp Code:

using System;
public class Exercise12
{
    public static void Main()
    {
        int num; 
  
        Console.WriteLine("Enter a digit: ");
        num = Convert.ToInt32( Console.ReadLine() );
  
        // Part A: "num num num num" using Write
        Console.Write( num );
        Console.Write(" ");
        Console.Write( num );
        Console.Write(" ");
        Console.Write( num );
        Console.Write(" ");
        Console.Write( num );
        Console.WriteLine();
  
        // Part B: "numnumnumnum" using Write
        Console.Write( num );
        Console.Write( num );
        Console.Write( num );
        Console.WriteLine( num );
        Console.WriteLine();
  
        // Part C: "num num num num" using {0}
        Console.WriteLine("{0} {0} {0} {0}", num);
  
        // Part D: "numnumnumnum" using {0}
        Console.WriteLine("{0}{0}{0}{0}", num);
   }
}

Sample Output:

Enter a digit:                                                                                                
2                                                                                                             
2 2 2 2                                                                                                       
2222                                                                                             
2 2 2 2                                                                                                       
2222

Flowchart:

Flowchart: C# Sharp Exercises - Print a number four times in separate rows

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program that takes an age (for example 20) as input and prints something as "You look older than 20.
Next: Write a C# program that takes a number as input and then displays a rectangle of 3 columns wide and 5 rows tall using that digit.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming