In: Computer Science
For java program.
for Loop
do Loop
roll = rand.nextInt(6) + 1;
System.out.println(" Roll = " + roll);
ctr++;
Here is a sample output display:
The rolls are:
Roll = 3
Roll = 1
Roll = 4
Roll = 6
Roll = 4
Add a loop structure using the while loop and then add a for loop. The program should include 3 loops that do the same thing. What is the “best” loop structure to use for this problem and why?
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
package c13;
import java.util.Scanner;
public class AddNumbers {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
int sum=0;
int count=0;
int input;
while(true) {
System.out.print("Enter number : ");
input =
sc.nextInt();
if(input%2!=0)
{
break;
}
count++;
sum
+=input;
}
System.out.println("Total number of
iteration : "+(count+1));
System.out.println("Total number
entered : "+(count));
System.out.println("Total of the
numbers is : "+sum);
}
}
2)
package c13;
public class Main {
public static void main(String[] args) {
for(int i=13;i<=93;i+=10)
System.out.println(i);
}
}
3)
package c13;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int roll;
int ctr=1;
Random rand = new Random();
do {
roll =
rand.nextInt(6) + 1;
System.out.println(" Roll = " + roll);
ctr++;
}while(ctr<=5);
}
}