FaltuTech.Club : Fane of Advanced Logical Thriving Utopian Technical Club

Calculate Arithmetic Mean of Given Numbers in Java

Calculate Arithmetic Mean of Given Numbers in Java



class test{
    public static void main(String ... arg){
	int length = arg.length;
	int sum = 0;
	for(String i : arg)
		sum+=Integer.parseInt(i);

	System.out.println("Arithmetic Mean of your numbers is : " + sum/length);
    }
}


Discussion

In above code sample we have not enclosed sum/length into Parentheses but in some programs we have enclosed arithmetic calculations into parentheses. It is because /,*,% are not overloaded while + is overloaded and that’s why (-) will also be trouble in situations where (+) precedes (-) in expression. While if (-) precedes (+) there would be not any trouble.