Menu

[Solved]Java Homework Exercises Question 1 Modify Code Count 0 5 Modify Code Count Even Numbers 0 Q37121244

Java Homework Exercises:

Question 1:

Can you modify the code to count up from 0 to 5?

Can you modify the code to count all even numbers from 0 to20?

public class Countdown {

public static void main(String[] args) {

       

       System.out.println(“Countdown to Launch: “);

        for(int i = 5; i>= 0; i–) {

           System.out.print(i +” “);

        }

       System.out.println(“Blast Off!”);

    }

}

=======================================================

Question 2:

Modify SquareRootWhile.java to use a while loop to repeatedlyprompt users to type a number until

they type a non-negative number, and then computes the squareroot.

Expected output:

Type a non-negative integer: -5

Invalid number, try again: -1

Invalid number, try again: 11

The square root of 11 is 3.166

import java.util.Scanner;

public class SquareRootWhile {

    public static void main(String args[])

    {

    System.out.print(“Type a non-negativeinteger: “);

     Scanner console = newScanner(System.in);

               int number = console.nextInt();  

}

  

}

===========================================================

Question 3:

The program is used to count the number of occurrences of thechar ‘w’ in the string.

– Modify the program to …

• Resolve the syntax error

• Print the count of char ‘w’

– Expected Output:

• Number of w : 3

public class CountChar {

    public static void main(String[] args) {

        String str =”www.oracle.com”;

        int max =str.length();

        int count = 0;

       

//           if (str.charAt(i) != ‘w’)

//               continue;

           count++;

       

       System.out.println(“Counting w : ” + count );

    }

}

=========================================================

Question 4:

Use a while loop and break statement.

• Implement the following:

– Accept 10 numbers from the user.

– Compute the sum of the numbers entered.

– When 0 is entered, the program must exit and display the sumof the numbers.

import java.util.Scanner;

public class ComputeSum {

    public static void main(String[] args) {

      

    }

}

Expert Answer


Answer to Java Homework Exercises: Question 1: Can you modify the code to count up from 0 to 5? Can you modify the code to count a… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *