Program to accept value of apple sales for each day of the week (using array of type float) and then, calculate the average sale of the week

This is the simple program in that we have created an array of sales and then stored the 7 values. After that we have display the total sale and average sale of the week.


PROGRAM
class AppleSales {

 public static void main(String[] args) throws IOException {
  
  double avg;
  float sum=0;
  float sales[]=new float[7];
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
  for(int i=1;i<=7;i++) {
   System.out.println ("Enter sales for day"+i+" of week =");
   sales[i-1] = Float.parseFloat(br.readLine());
   sum=sum+sales[i-1];
  }
  
  System.out.println ("Sum = "+sum);
  avg=sum/7;
  System.out.println ("Average sale of week="+avg);
 }
}
OUTPUT
C:\>javac AppleSales.java
C:\>java AppleSales
Enter sales for day1 of week =
120.6
Enter sales for day2 of week =
142.5
Enter sales for day3 of week =
452.4
Enter sales for day4 of week =
500.0
Enter sales for day5 of week =
532.0
Enter sales for day6 of week =
745.2
Enter sales for day7 of week =
320.9
Sum = 2813.5999
Average sale of week=401.9428405761719

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative