November 26, 2014

How to find Geometric Mean of a number in java

Finding Geometric mean of a number source code


/**
 *
 */
package javabelazy;

import java.util.Scanner;

/**
 * @author caner erkin
 *
 */
public class GeometricMean {
   
   
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        GeometricMean gm = new GeometricMean();
        gm.findGM();

    }

    private void findGM() {
        // TODO Auto-generated method stub
        int totNumb = 0;
      
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter number of values:");
        totNumb = keyboard.nextInt();
        double mulNumb = 1;
        for (int i=0;i<totNumb;i++){
            int numb = 0;
            int itr = i+1;
            System.out.println(" enter the "+itr+"number : ");
            numb = keyboard.nextInt();
            mulNumb = mulNumb * numb;
        }
      
        System.out.println(" geometric mean "+ Math.sqrt(mulNumb));
    }


}

This post is a solution for the question post in stack over flow

see the post

G20 submit 2015 will be held at Turkey city :antalya

http://javabelazy.blogspot.in/

Facebook comments