Question

In: Computer Science

Write a Java program using jGRASP directions are as follows: Uses a while loop to print...

Write a Java program using jGRASP directions are as follows:

    • Uses a while loop to print the numbers from 3 - 19.
    • Uses a do-while loop to print the numbers from 42 - 56.
    • Uses a for loop to print the numbers from 87 - 95.
    • Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive.
      • Note: Consider that your user's second number can be lower! (see example below)
      • Note: Also consider that your user might give you two of the same number.
  • Your program MUST:
    • Use a while, do-while, and for loop. The fourth loop is your choice.
    • Actively use in-line comments stating what each section of code does.
    • Use try/catch, BUT only if necessary.
    • Your program must conform to the Java coding standards
    • This is an example of what your program should output:
  • I'm going to print out the numbers 3-19 using a while loop:
    3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    
    I'm going to print out the numbers 42-56 using a do-while loop:
    42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    
    I'm going to print out the numbers 87-95 using a for loop:
    87 88 89 90 91 92 93 94 95
    
    Now I'll print whatever numbers you'd like!
    Give me a starting number:
    2
    Give me an ending number:
    -4
    I see that your ending number is lower. Okay, I'll count down!
    2 1 0 -1 -2 -3 -4
     ----jGRASP: operation complete.

Solutions

Expert Solution

Please find below code and don't forget to give a Like

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       int firstnum=3,secondnum=19;
       int dofirst=42,dolast=56;
       int forfirst=87,forlast=95;
       int first=2,last=-2;
       System.out.println("This is while loop");
       while(firstnum<=secondnum){
      
       System.out.print(firstnum+" ");
       firstnum=firstnum+1;
       }
       System.out.println();
       System.out.println("This is do while loop");
       do{
      
       System.out.print(dofirst+" ");
       dofirst=dofirst+1;
      
       }
       while(dofirst<=dolast);
       System.out.println();
       System.out.println("This is for loop");
for(int i=forfirst;i<=forlast;i++){

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

if(last<first){
System.out.println();
       System.out.println("This is negative number for loop");
for(int k=first;k>=last;k--){
System.out.print(k+" ");
}
}
   }
}

To take the input from user, we use Scanner

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
       Scanner input= new Scanner(System.in);
System.out.println("Enter first number");
int firstnum= input.nextInt();
System.out.println("Enter second number");
int secondnum= input.nextInt();
for(int i=firstnum;i<=secondnum;i++){
System.out.println(i);
}
   }
}


Related Solutions

Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
Write a program that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while loop To Understand what the Fibonacci series is: The Fibonacci sequence is a series of numbers where a number is the sum of the previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Q2: Write a Java program to find the Factorial of a number using a while loop. To...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT