Print average of n number Using Java

Print average of n number Using Java

Previous Home Next

 

Print average of n number Using Java

 

 //program for find a Average value of any prime no.
package r4r.co.in;

import java.io.*;

public class AverageNo {

    public static void main(String[] args) throws IOException {

        BufferedReader Digit = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the Digit A:");
        String s1 = Digit.readLine();
        int n1 = Integer.parseInt(s1);
        System.out.println("Digit A is:" + n1);

              // Average value of any digit

        try {
            int a = n1, sum = 0, count = 0;
            float b;
            {
                for (int i = 0; i <= a; i++) {
                    if ((i % 2) == 0) {               //Prime no. determine
                        System.out.println("Prime no of A:" + i);
                        sum = sum + i;              // sum of all value of prime no.
                        count++;
                    }
                }

                System.out.println("Sum of all prime no. A:" + sum);
                b = sum / count;                     //Average value of prime no.
                System.out.println("Average of no A:" + b);

            }
        } catch (Exception e) {
            System.out.print(e);
        }
    }
}

Enter the Digit A:
20
Digit A is:20
Prime no of A:0
Prime no of A:2
Prime no of A:4
Prime no of A:6
Prime no of A:8
Prime no of A:10
Prime no of A:12
Prime no of A:14
Prime no of A:16
Prime no of A:18
Prime no of A:20
Sum of all prime no. A:110
Average of no A:10.0
Previous Home Next