Number repeated Learn programming Java

Lesson:

Flow Control


Exercise:

Number repeated


Objetive:

Write a java 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:

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);
	}
}

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