Sunday, 8 December 2013

Unknown

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



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);
    }
}

Unknown

About Blog No Baap -

Since 2016 BlogNoBaap has been bringing you the very best in all types of web resources. Posted daily, and delivered straight to your inbox each morning.

Subscribe to this Blog via Email :