Example(s):Palindrome, prime, armstrong, "linear search", reverse etc.
Example(s):1575, 1632, 1539 (Only one at a time)
Problem Statement - Average of three integers to float output
Write a java program to calculate the average of three fixed integers 10,15 and 18 . The output can be a fractional floating point value.
Average of 10, 15, 18 = 14.333333
int a=10,b=15,c=18; //Three fixed values have been initialised in integer variables a,b,c
Since the output can be a floating point value so avg has been declared as a float value. In the expression denominator has been taken as 3.0f so that floating point based calculation occurs. a+b+c has been put in brackets so that summation occurs before division.
System.out.println(“Average of “+a+”, “+b+”, “+c+” = “+avg); The average is neatly printed.
In java fractional division calculation of integers can achieved by converting either of numerator or denominator into a float or double value as the case may be. In this example following alternate expression using typecasting of numerator to float would have also worked. float avg=(float) (a+b+c)/3; Here the denominator remains an integer while numerator becomes a float value.
Proper use of brackets and type of numerator or denominator is essential to get correct value in this problem.