Number repeated Learn programming with Java exercises

Lesson:

Flow Control


Exercise:

Number repeated 49


Objetive:

Write a java program which asks for a number and an amount, and shows that number repeated as many times as the user has indicated, as in the following example:

Enter a number: 4
Enter an amount: 5

44444

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


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int num, amount;
		int i;

		System.out.print("Enter a number: ");
		num = Integer.parseInt(new Scanner(System.in).nextLine());
		System.out.print("Enter an amount: ");
		amount = Integer.parseInt(new Scanner(System.in).nextLine());

		for (i = 0; i < amount; i++)
		{
			System.out.print(num);
		}


		System.out.println();

		i = 0;
		while (i < amount)
		{
			System.out.print(num);
			i++;
		}


		System.out.println();

		i = 0;
		do
		{
			System.out.print(num);
			i++;
		} while (i < amount);
	}
}