3 – Loops Answers

1) Use a for loop to print the 5 times table up to 12 x 5

for(int i = 1; i<13; i++)
{
    System.out.println(i*5);
}

2) Use a for loop to print the 7 times table up to 12 x 7 in the form “3 x 7 = 21”

for(int i = 1; i<13; i++)
{
    System.out.format("%d x 7 = %d %n",i,i*5);
}

3) Use a for loop to print the following sequence: 0.5, 0.4, 0.3, 0.2, 0.1, 0

for(int i = 5; i>=0; i--)
{
    System.out.println((double)i/10);
}

4) Use a for loop to print the following sequence: 0.03, 0.02, 0.01, 0, -0.01, -0.02, -0.03

for(int i = 3; i>=-3; i--)
{
    System.out.println((double)i/100);
}

5) Use a for loop to print five random numbers between 1 and 10

for(int i = 1; i<11; i++)
{
    System.out.println((int)(Math.random()*5+1));
}

6) Use a for loop to print the first ten square numbers: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100

for(int i = 1; i<11; i++)
{
    System.out.println(i*i);
}

7) Use a for loop to print the first ten triangle numbers: 1, 3, 6, 10, 15, 21, 28, 36,45, 55

int triangle = 0;
for(int i = 1; i<11; i++)
{
    triangle += i;
    System.out.println(triangle);
}

8) Use a while loop to print the numbers from 1 to 10 inclusive

int count = 1;
while (count < 11)
{
    System.out.println(count);
    count++;
}

9) Use a while loop to print the sequence 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1

double number = 9;
    while (number > 0)
    {
    System.out.println(number/10);
    number--;
    }
}

10) Use a while loop to print the 4 times table up to 12 x 4.

int count = 1;
while (count < 13)
{
    System.out.println(count*4);
    count++;
}

11) Use a while loop to print the 9 times table up to 12 x 9 in the form “1 x 9 = 9, 2 x 9=18… ”

int count = 1;
while (count < 13)
{
    System.out.format("%d x 9 =  %d%n",count,count*9);
    count++;
}

12) Prompt the user to enter a number. Keep a running total of the numbers entered. Loop until the user enters -1, exit the loop and print the total
(Compare this with the answer to 13)

Scanner scan = new Scanner(System.in);
int total = 0;
int number;
do
{
    System.out.println("Enter a number.  -1 to exit.");
    number = scan.nextInt();
    total+=number;
} while (number != -1);
System.out.println("Total is " + (total+1));

13) Prompt the user to enter a number. Keep a running total of the numbers entered. Loop until the user enters -1, exit the loop and print the average
(Compare this with the answer to 12)

Scanner scan = new Scanner(System.in);
int total = 0;
int number = 0;
int count = -1;
while(number != -1)
{
    total+=number;
    count++;
    System.out.println("Enter a number, -1 to quit");
    number = scan.nextInt();
}
System.out.println(total + " " + number + " " + count);
double average = (double)total/(double)count;
System.out.println("Average is " + average);

14) Write a program to test if 91 is prime. Use a while loop to divide 91 by the numbers from 2 to 10. Output True if none of the numbers 91%(number) = 0 for any of the numbers, output False otherwise.

int primep = 13;
int count = 2;
boolean flag = true;
while (count <11)
{
    if(primep % count == 0) flag = false;
    count++;
}
System.out.println(flag);

15) Write a program to test if any number is prime. Use a while loop to divide the input by the numbers from 2 to sqrt(input). Output “Prime” if the number is prime, “Not Prime” otherwise.

Scanner scan = new Scanner(System.in);
    System.out.println("Enter number to be tested");
    int primep = scan.nextInt();
    int count = 2;
    boolean flag = true;
    while (count < Math.sqrt(primep))
    {
    if(primep % count == 0) flag = false;
    count++;
    }
    if(flag)
    {
    System.out.println("Prime");
    }
    else
    {
    System.out.println("Not Prime");
    }

16) Use a while loop to print the sequence 1, -2, 3, -4, 5, -6, 7, -8

int count = 1;
while(count < 9)
{
    System.out.println(-(Math.pow(-1,count)*count));
    count++;
}

17) Use a while loop to calculate pi using the Liebniz formula pi/4 = 1 – 1/3 + 1/5 – 1/7 + 1/9 – … How many iterations do you need to get 3.141?

int count = 1;
double total = 1;
while(count < 10000)
{
    total = total + ((Math.pow(-1,count)/(2*count + 1)));
    System.out.println(4*total);
    count++;
}

Leave a Comment