Wednesday, September 9, 2015

For repetition statement in java

Java also provides the for repetition

statement, which specifies the counter-controlled-repetition details in a single line of
 
code.

a program receives 10 numbers and calculates their average using for statement.


import java.util.Scanner;


public class Apples {

public static void main(String[] args){



int counter = 0;

float sum = 0;

float studentGrade =0;

float average = 0;



Scanner input = new Scanner(System.in);

// entering a 10 numbers to get their average

System.out.println("enter 10 grades");



for (counter = 0 ; counter < 10 ; counter++ ){



studentGrade = input.nextInt();

sum += studentGrade;




}

average = sum / counter;

System.out.printf("the average is %s", average);


}

}


 

1 comment:

  1. Another nice class , I am enjoying the classes .Looking forward to the next one .

    ReplyDelete