In: Computer Science
Write a Java program using jGRASP directions are as follows:
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.
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);
}
}
}