0% found this document useful (0 votes)
1 views6 pages

Lesson-2 4 A

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Lesson-2 4 A

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Loops

1. Make a program using loop to solve for the average grade of


ten students.
import java.util.Scanner;
public class
{
public static void main(String[] args)
{
Scanner myScanner=new Scanner(System.in);
int grade,total,n;
double average;
n=1;
total=0;
while (n<=10)
{
System.out.print("Enter grade: ");
grade=myScanner.nextInt();
total=total+grade;
n=n+1;
}
average=total/10;
System.out.println("The average grade is" + average + ".");
}
}
2. Make a program using loop to solve for the average of
arbitrary number of sales.
import java.util.Scanner;
public class
{
public static void main(String[] args)
{
Scanner myScanner=new Scanner(System.in);
int sale,total,n;
double average;
n=0;
total=0;
System.out.print("Enter sale (-1 to quit): ");
sale=myScanner.nextInt();
while (sale!=-1)
{
total=total+sale;
n=n+1;
System.out.print("Enter sale (-1 to quit): ");
sale=myScanner.nextInt();
}
average=total/n;
System.out.println("The average sale is " + average + ".");
}
}

You might also like