Java Data Types Assignment?
Object [ ] = { “Java”, 1.23F, true, false, 23, ‘a’, ‘b’, 23.4F, 45.6F}; Write the Java Program to sum only the float variables or any data type.
public class Main {
public static void main(String[] args) {
Object[] data = new Object[]{ "Java", 1.23F, true, false, 23, 'a', 'b', 23.4F, 45.6F};
float sum = 0;
for (int i = 0; i < data.length; i++) {
Object obj = data[i];
if (obj.getClass() == Float.class)
sum += ((Float) obj);
}
System.out.println("Sum of floats = " + sum);
}
}
Comments
Leave a comment