Number repeated Learn programming C#



Lesson:

Flow Control


Exercise:

Number repeated


Objetive:

Write a C# program that asks the user for a number and a quantity, and displays that number repeated as many times as the user has specified. Here's an example:

Enter a number: 4
Enter a quantity: 5

44444

You must display it three times: first using "while", then "do-while" and finally "for".


Code:

using System;
public class exercise29
{
    public static void Main()
    {
        int num, amount;
        int i;

        Console.Write("Enter a number: ");
        num = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter an amount: ");
        amount = Convert.ToInt32(Console.ReadLine());

        for (i = 0; i < amount; i++)
            Console.Write(num);


        Console.WriteLine();

        i = 0;
        while (i < amount)
        {
            Console.Write(num);
            i++;
        }


        Console.WriteLine();

        i = 0;
        do
        {
            Console.Write(num);
            i++;
        }
        while (i < amount);
    }
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.