Java program to find average of consecutive N Odd no. and Even no.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class EvenOdd_Avg{
	public static void main(String args[]){
		int n = Integer.parseInt(args[0]);
		int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;
		while(n > 0){
			if(n%2==0){
				cntEven++;
				sumEven = sumEven + n;
			}
			else{
				cntOdd++;
				sumOdd = sumOdd + n;
			}
			n--;
		}
		int evenAvg,oddAvg;
		evenAvg = sumEven/cntEven;
		oddAvg = sumOdd/cntOdd;
		System.out.println("Average of first N Even no is "+evenAvg);
		System.out.println("Average of first N Odd no is "+oddAvg);
	}
}

2 Responses to “Java program to find average of consecutive N Odd no. and Even no.”

  1. Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
    at EvenOdd_Avg.main(EvenOdd_Avg.java:3)
    this error is occur

    Reply
  2. Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); int n=sc.nextInt(); repale instead of int n = Integer.parseInt(args[0]);

    pls explain why Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
    at EvenOdd_Avg.main(EvenOdd_Avg.java:3)
    this error is occur

    Reply

Leave a Reply