Internet Technologies
1.0.0
1.0.0
  • Introduction
  • Contents
  • Practical List
  • HTML & CSS
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
  • JAVA Programs
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
  • JAVASCRIPT
    • Q13
    • Q14
    • Q15
  • JDBC
    • Q16
    • Q17
  • JSP
    • Side Note
    • Q18
    • Q19
    • Q20
    • Q21
    • Q22
    • Q23
  • End
Powered by GitBook
On this page
  • Code
  • Output
  1. JAVA Programs

Q6

Question 6

PreviousQ5NextQ7

Last updated 4 years ago

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.

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);
    }
}

Output

Try or Test The Corresponding Code Here

The above notebook runs on cloud, make sure to run the first cell of the notebook to set-up JDK and JRE on the cloud machine.

Browse Source Code
Google Colaboratory
Live Demo java_1.java
Logo
1KB
java_1.java
Download java_1.java
Image output for java-1.java