In: Computer Science
#1 Write a loop that displays every fifth number, 0 through 100.
#2 Write a do- while loop that asks the user to enter two numbers. The number should be added and the sum displayed. The user should be asked if he or she wishes to perform the operation again. if so, the loop should repeat; otherwise it should terminate.
#3 Write a nested loop that displays 10 rows of '#' characters. There should be 15 '#' characters in each row.
#4 Write a while loop that displays the odd numbers between 1 and 15.
#5 Write a program segment with a do-while loop that displays whether a user- entered integer is even or odd. The code should then ask the user if he or she wants to test another number. The loop should repeat so long as the user enters Y or y. Use a logical OR operator in the do-while loop test expression.
//Java code
import java.util.Scanner; public class LoopDemo { public static void main(String[] args) { /** * Write a loop that displays every fifth number, 0 through 100. */ System.out.println("================== Solution1 ======================"); for (int i = 0; i <=100 ; i++) { if(i%5==0) { System.out.print(i+" "); } } System.out.println(); System.out.println("================== Solution2 ======================"); /** * Write a do- while loop that asks the user to * enter two numbers. The number should be added * and the sum displayed. The user should be asked * if he or she wishes to perform the operation again. * if so, the loop should repeat; otherwise it should terminate. */ Scanner input = new Scanner(System.in); int num1,num2; char choice; do { System.out.print("Enter first number: "); num1= input.nextInt(); System.out.print("Enter first number: "); num2 = input.nextInt(); int sum = num1+num2; System.out.println(+num1+" + "+num2 +" = "+sum); System.out.print("Do you want to continue. (y/n): "); choice = input.next().toUpperCase().charAt(0); }while (choice=='Y'); System.out.println("================== Solution3 ======================"); /** * Write a nested loop that displays 10 rows of '#' characters. * There should be 15 '#' characters in each row. */ for (int i = 1; i <=10 ; i++) { for (int j = 0; j <15 ; j++) { System.out.print("#"); } System.out.println(); } System.out.println("================== Solution4 ======================"); /** * Write a while loop that displays the odd numbers between 1 and 15. */ int i=1; while (i<=15) { if(i%2!=0) System.out.print(i+" "); i++; } System.out.println(); System.out.println("================== Solution5 ======================"); /** * Write a program segment with a do-while loop that * displays whether a user- entered integer is even or odd. * The code should then ask the user if he or she wants to test * another number. The loop should repeat so long as the user enters Y or y. * Use a logical OR operator in the do-while loop test expression. */ do { System.out.print("Enter a number: "); int num = input.nextInt(); if(num%2==0) { System.out.print(num+" is even."); } else { System.out.print(num+" is odd."); } System.out.print("\nDo you want to continue. (y/n): "); choice = input.next().charAt(0); }while (choice =='y' || choice=='y'); } }
//Output
//If you need any help regarding this solution ............. please leave a comment .......... thanks