1.12: Formats

OBJECTIVE

Write a C# program to ask the user for a number and display ir four times in a row, separated with blank spaces, and then four times in the next row, with no separation. You must do it two times: first using Console.Write and then using {0}. 

RESOLUTION


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace My3Development
{
public class Formats
{
    public static void Main(String[] args)
    {
       int n;
          
       Console.Write(" Enter a digit: ");
       n=Convert.ToInt32(Console.ReadLine());

                // Part A: "n n n n" using Write
       Console.Write( n );
       Console.Write(" ");
       Console.Write( n );
       Console.Write(" ");
       Console.Write( n );
       Console.Write(" ");
       Console.Write( n );
       Console.WriteLine();

        // Part B: "nnnn" using Write
      Console.Write( n );
     Console.Write( n );
     Console.Write( n );
     Console.WriteLine( n );
     Console.WriteLine(); 

       // Part C: "n n n n" using {0}
     Console.WriteLine("{0} {0} {0} {0}", n);

      // Part D: "nnnn" using {0}
     Console.WriteLine("{0}{0}{0}{0}", n);

        Console.ReadKey();
        
        }
    }
 }

DEMO RESULT


         PREVIOUS                                             HOME                                                NEXT

0 comments :

Please Enter best of your Comments