Count the number of elements in given array greater than a given number x in java.

import java.util.Scanner;

import java.util.Arrays;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter size of array=");

int n = sc.nextInt();

int[] arr=new int[n];

System.out.println("enter elements=");

for(int i=0; i<n; i++){

      arr[i]=sc.nextInt();

}

      System.out.println("enter x=");

      int x=sc.nextInt();

      int c=0;

      for(int j=0; j<n; j++){

            if(arr[j]>x)

            c++;

      }

      System.out.println("total no. of elements greater then x is ="+c);

   }

}

output->

enter size of array=
5
enter elements=
12
45
78
56
23
enter x=
30
total no. of elements greater then x is =3

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.