Question

Java program should ask the user for the maximum number to print out to and then...

Java program should ask the user for the maximum number to print out to and then print each evennumber starting from 0 to the maximum. You may want to use % operator which gives you the remainder. Some results of the operator is as follows:

• 0 % 2 = 0

• 1 % 2 = 1

• 2 % 2 = 0

• 3 % 2 = 1

• 4 % 2 = 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class EvenNumbers {

      public static void main(String[] args) {

            // TODO Auto-generated method stub

            //Scanner function to get the input from keyboard

            Scanner scan = new Scanner(System.in);

            //Ask the user to enter Maximum number

            System.out.print("Maximum number to print: ");

            int max= scan.nextInt();

            //printing the maximum number

            System.out.println("Maximum number: "+max);

            System.out.println("Even number upto "+max);

            //Looping from 0 to maximum number to print the numbers upto maximum

            for (int i=0; i<=max; i++){

                  if(i%2 ==0){

                        //If the remainder is 0 then the number is even

                        //If the number is even it is printed otherwise it is not printed

                        System.out.print(i+" ");

                        //If you want to print number per line you can use System.out.println in the above statement

                  }

            }

      }

}

Output:

Add a comment
Know the answer?
Add Answer to:
Java program should ask the user for the maximum number to print out to and then...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This program should ask the user for the maximum number to print out to and then...

    This program should ask the user for the maximum number to print out to and then print each evennumber starting from 0 to the maximum. You may want to use % operator which gives you the remainder. Some results of the operator is as follows: • 0 % 2 = 0 • 1 % 2 = 1 • 2 % 2 = 0 • 3 % 2 = 1 • 4 % 2 = 0

  • IN PERL Write a program that will ask the user for a number and will print...

    IN PERL Write a program that will ask the user for a number and will print all the integers in order starting from 1 until the number. Use the <> operator to ask for the user's input. For this program you will write a countdown timer. Your time will count down from 1 minute (60 seconds) to 0. Once the timer gets to 0 seconds the system will display a message that reads: RING RING RING! -using do while-

  • Simple calculator program. The program should ask the user for a number, an operator, and then...

    Simple calculator program. The program should ask the user for a number, an operator, and then a second number. The operator should be either '+', '-', 'x', or '/'. The program should then report the answer to the problem. Use a switch statement, not an if statement. Note that "operator" is a reserved word in C++, so you cannot use it as a variable name. Use double variables and setprecision(2).

  • In Java Write a program that will ask the user for integers and print if the...

    In Java Write a program that will ask the user for integers and print if the integer is positive, negative or zero. The program should continue asking for integer until the user enters a negative value.

  • Java Submit a program in a .java file. The program should print a multiplication table for...

    Java Submit a program in a .java file. The program should print a multiplication table for the numbers 1-9. This requires a nested for loop. The outer loop moves from row to row, while the inner loop prints the row. Use tabs to separate the output, such as in the following statement:      System.out.println(“1\t2\t3\t4\t5\t6\t7\t8\t9”); Output should look similar to the following. 1      2     3     4     5     6     7     8     9 ------------------------------------------------------------------ 1       2     3     4     5     6        7     8     9...

  • Write a java program that will print if n numbers that the user will input are...

    Write a java program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times asking for an integer...

  • Write a program that will ask the user for a decimal number such as 18 and...

    Write a program that will ask the user for a decimal number such as 18 and prints the 16 bit binary equivalent of the number: 0000 0000 0001 0010 NOTE: Your output should be a binary string formatted by nibbles (spaces every four bits): 0000 0000 0010 1110 1010 0111 1. Ask the user for a number and store it in an int variable called “number”. 2. Find out how many bits you want to use to represent this number....

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • This is a Java text only program. This program will ask if the user wants to...

    This is a Java text only program. This program will ask if the user wants to create a music playlist. If user says he or she would like to create this playlist, then the program should first ask for a name for the playlist and how many songs will be in the playlist. The user should be informed that playlist should have a minimum of 3 songs and a maximum of 10 songs. Next, the program will begin a loop...

  • This program will ask the user to enter a number of players, then ask the user...

    This program will ask the user to enter a number of players, then ask the user for each player's name and score. Once all of the players have been entered, the program will display a bar chart with each of their scores scaled as a percentage of the maximum score entered. See below for details on exactly how this should work - an example transcript of how the program should work is shown below (remember that values entered by the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT