Islam Ismayilov Islam Ismayilov - 3 months ago 11
Java Question

Find smallest even from array. and also average of even and odd numbers?

package lesson5;

import java.util.Scanner;

public class task55 {

private static Scanner sc;

public static void main(String[] args) {
sc = new Scanner(System.in);
int i = 0;
double num[] = new double[5];
double min_even = 0;
double evencount = 0;
double evensum = 0;
int oddcount = 0;
int oddsum = 0;

for (i = 0; i < 5; i++) {
System.out.println("input number");
num[i] = sc.nextInt();
}

System.out.println(" print even num : ");

for (i = 0; i < 5; i++) {
if (num[i] % 2 == 0) {

evencount++;
evensum += num[i];
System.out.println(num[i] + " ;");
if (min_even > num[i])
min_even = num[i];

}
}

System.out.println(" evencount = " + evencount);
System.out.println(" evensum =" + evensum);
System.out.println(" ceven average = " + evensum / evencount);
System.out.println("-------------------------------");
System.out.println(" print odd num :");

for (i = 0; i < 5; i++) {
if (num[i] % 2 != 0) {
oddcount++;
oddsum += num[i];
System.out.println(num[i] + " ;");

}
}

System.out.println(" oddcount " + oddcount);
System.out.println(" oddsum =" + oddsum);
System.out.println(" average odd = " + oddsum / oddcount);

}


}

above mentioned code works. just i couldnot get result- min and max.
please help me to print- min_even and max_even and min_odd and Max_odd.

Answer

Try this.

package lesson5;
import java.util.Scanner;

public class task3 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int [] masarray = new int [10];
    int a,b,c,d,f,g,j,m,n,l;

    int flag_even =0;
    int min_even = 0;
    float sum_even=0;
    float sum_odd=0;
    int total_even=0;
    int total_odd=0;

    System.out.println("input first  number of array");
    a=sc.nextInt();
    masarray [0]= a;

    System.out.println("input second  number of array");
    b=sc.nextInt();
    masarray [1]= b;

    System.out.println("input third  number of array");
    c=sc.nextInt();
    masarray [2]= c;

    System.out.println("input fourth  number of array");
    d=sc.nextInt();
    masarray [3]= d;

    System.out.println("input fiveth  number of array");
    f=sc.nextInt();
    masarray [4]= f;

    System.out.println("input sixth  number of array");
    g=sc.nextInt();
    masarray [5]= g;

    System.out.println("input seventh  number of array");
    j=sc.nextInt();
    masarray [6]= j;

    System.out.println("input eighth  number of array");
    m=sc.nextInt();
    masarray [7]= m;

    System.out.println("input nineth number of array");
    n=sc.nextInt();
    masarray [8]= n;

    System.out.println("input tenth  number of array");
    l=sc.nextInt();
    masarray [9]= l;

    for ( int i =0; i < masarray.length; i++){
        if (masarray[i]%2==0){

            if(flag_even==0)//Assigning first even number found in array to min_even to initialize it
            {
                min_even=masarray[i];
                flag_even=1;//Initialization done
            }

            else if(min_even>masarray[i])
                min_even=masarray[i];

            sum_even+=masarray[i];//calculates sum of all even numbers
            System.out.println("even number = " + masarray[i]); 

            total_even++;//counting total number of even numbers
        }

        else
        {
            sum_odd=masarray[i];//calculates sum of all odd numbers
            total_odd++;//counting total number of odd numbers
        }
    }

    if(flag_even==0)
        System.out.println("No even number present in array!");
    else
    {   
        System.out.println("Minimum Even number in given array is:"+min_even);
        System.out.println("Average of Even numbers in given array is:"+(sum_even/total_even));
    }

    if(total_odd==0)
        System.out.println("No odd number present in array!");
    else
        System.out.println("Average of Odd number in given array is:"+(sum_odd/total_odd));

  }
}
Comments