Question

In: Computer Science

#1 Write a loop that displays every fifth number, 0 through 100. #2 Write a do-...

#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.

Solutions

Expert Solution

//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


Related Solutions

In C# Please Write an application named Perfect that displays every perfect number from 1 through...
In C# Please Write an application named Perfect that displays every perfect number from 1 through 10,000. A number is perfect if it equals the sum of all the smaller positive integers that divide evenly into it. For example, 6 is perfect because 1, 2, and 3 divide evenly into it and their sum is 6. Starting code. using static System.Console; class Perfect { static void Main() { // Write your main here. } }
Java Write a JavaFX application that displays a button and a number. Every time the button...
Java Write a JavaFX application that displays a button and a number. Every time the button is pushed, change the number to a random value between 1 and 100. Thank you and can you show a picture of the result as well
10. Write a for loop which will print every number evenly divisible by 13 between 1...
10. Write a for loop which will print every number evenly divisible by 13 between 1 and 100, inclusive. Your loop will only increment by 1 each loop so you need an if test to see if each number should be printed. Put each number output on the same line, 1 space apart. 11. Write a while loop which will prompt the user for a number and accept their input and then display this message (assuming number input is 3)...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
1. write the equations of a line through the point (0, 2, -5) and perpendicular to...
1. write the equations of a line through the point (0, 2, -5) and perpendicular to the plane -2x+3y+4z = 18. Use either parametric or symmetric form. 2. Find the acute angle between the planes 2x+4y-z = 12 and x-6y+5z= 20.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT