Q6

Question 6

Write a program to create an array of 10 integers. Accept values from the user in that array. Input another number from the user and find out how many numbers are equal to the number passed, how many are greater and how many are less than the number passed.

Code Insights

Created java_1.java a new java class file and imported java.util.Scanner package to take user input, in the main method object "s" of Scanner class takes desired input and then with the use of if else blocks and counter variables the desired output is displayed.

Browse Source Code

Code

/**
 *Solution for Ans. 6
 * @author Jatin Kamboj 8647
 */
import java.util.Scanner;
public class java_1
{
    public static void main(String[] args)
    {
        int n=10, comp;
        Scanner s = new Scanner(System.in);
        System.out.println("--- This program will print " +
                "the numbers equal, greater and lesser " +
                "than a given input number out of another" +
                " 10 input integers ---");
        System.out.println("\nEnter the number to " +
                "compare with: ");
        comp = s.nextInt();

        System.out.print("Enter 10 elements that " +
                "are to be compared.\n");
        int[] a = new int[n];
        System.out.println("Enter elements of array: ");

        for(int i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
        int greater = 0;
        int lesser = 0;
        int equal = 0;
        for(int i = 0; i < n; i++)
        {
            if (a[i]==comp){
                equal += 1;
            } else if(a[i] > comp){
                greater += 1;
            }else{
                lesser += 1;
            }

        }
        System.out.println("Count of numbers greater" +
                " than " + comp + ": " + greater);
        System.out.println("Count of numbers equal" +
                " to " + comp + ": " + equal);
        System.out.println("Count of numbers lesser" +
                " than " + comp + ": " + lesser);
    }
}
Download java_1.java

Output

Image output for java-1.java
Live Demo java_1.java

Last updated