In: Computer Science
Create a Java application NumberLoops to practice loops. The draft has one loop to calculate a product and the final will add another loop to print integers in a required format. There is no starter code for the problem.
Use a Scanner to input an integer then compute and display the product of all positive integers that are less than the input number and multiple of 3. If no numbers satisfy the conditions, then print out 1 for the product.
Use a double for the product to avoid overflow and print out it without any decimal digits using the format specifier “%.0f”. Numbers 1 and 3 are allowed for the draft and will not be considered as magic numbers.
After printing out the product, enter another integer then print out all non-negative integers less than the input integer, starting with 0. Print one space after each integer and one additional space before an integer if it is less than 10. Print 10 integers per row, except the last row which may have less than 10 integers. In addition to 1 and 3, numbers 9 and 10 are allowed for the final and will not be considered as magic numbers.
The remainder operator (%) will help you here to decide when to call println() to go to the next line. Use only one loop. Do not use nested loops.
Sample run Enter an integer: 36
The product is 7071141369600.
Enter another integer: 36
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35
CODE CHECK:
import java.util.Scanner;
public class NumberLoops
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = scan.nextInt();
int i;
double product=1;
for(i=1;i<36;i++)
{
if(i%3==0){
product=product*i;
}
}
System.out.printf("The product is
"+"%.0f",product,".");
System.out.printf("\n");
System.out.print("Enter another integer: ");
int num2 = scan.nextInt();
System.out.print("0 ");
for(i=1;i<num2;i++)
{
if (i%10 == 0){
System.out.print("\n");}
System.out.print(i+" ");
}
}
}
OUTPUT:-