prompts the user to enter 10 integers from 0 to 100. Keep these numbers in an array of integers named yourNumber[]. From this array, calculate and display the sum of all these 10 numbers. Next, identify and display the largest and the smallest number. Finally, provide the number of integers in this array which are odd numbers. Take note that all input from user will be handled by the input dialog method from JOptionPane class and all repetitive tasks must be executed using for or while loops.
1
Expert's answer
2012-02-17T08:30:51-0500
import javax.swing.JOptionPane;
public class Main {
private static double yourNumber[]=new double[20];; public static void main(String[] args) { String ans; for(int i=0;i<10;i++){ ans = JOptionPane.showInputDialog(null, "Enter element number "+ (i+1) + " from 0 to 100:"); yourNumber[i] = Double.parseDouble(ans); } String answer=""; for(int i=0;i<10;i++){ if(yourNumber[i]%2==1){ answer+=yourNumber[i]+" "; } } JOptionPane.showMessageDialog(null, "Sum = "+Sum(yourNumber)+"\n"+"Max = "+Max(yourNumber)+"\n"+"Min = "+Min(yourNumber)+"\nOdd number is: "+answer); System.exit(0); } //Sum private static double Sum(double arayofNumber[]){ double sum=0; for(int i=0;i<10;i++){ sum+=arayofNumber[i]; } return sum; } //Function Min - smallest element in the array public static double Min(double arayofNumber[]){ double min=arayofNumber[0]; for(int i=0;i<10;i++){ if(arayofNumber[i]<arayofNumber[0]){ min=arayofNumber[i]; } } return min; } //Function Max - largest element in the array public static double Max(double arayofNumber[]){ double max=arayofNumber[0]; for(int i=0;i<10;i++){ if(arayofNumber[i]>arayofNumber[0]){ max=arayofNumber[i]; } } return max; }
Comments
Leave a comment