Question
java for netbeans

Question 2: (Print distinct numbers) Write a program that reads in ten numbers and displays the number of distinct numbers an
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

// Importing packages
import java.util.Scanner;

// Defining main class
public class Main
{
// Defining function to find distnict intergers
static void disnict_Elements(int a[] , int n)
{
// Declaring temperoary array and variable to keep count
int[] t = new int[10];
int c = 0;
  
// Code logic for finding disnict elements
for (int i = 0; i < n ; i++)
{
int j;
for (j = 0; j < i; j++)
if (a[i] == a[j])
{
break;
}
  
if (i == j)
{
t[c]=a[i];
c=c+1;
}
}

// Printing results
System.out.println("The number of distnict number "+c);
System.out.print("The distnict numbers are :");
for (int l=0;l<c;l++)
   {
   System.out.print(t[l]+" ");
   }  
}
  
   public static void main(String[] args)
   {
   int[] a= new int[10];
   Scanner s = new Scanner(System.in);
  
   // Input intergers
   System.out.print("Enter ten numbers :");
   for (int i=0;i<10;i++)
   {
   a[i] = s.nextInt();
   }  
      
       // Function calling
       disnict_Elements(a,10);
}
}

Code Screenshot:


Main.java // Importing packages 2 import java.util.Scanner; 3 4 // Defining main class 5 public class Main 6- // Defining fun

// Printing results System.out.println(The number of distnict number +c); System.out.print(The distnict numbers are :); f

Output:

input Enter ten numbers :1 ܢܝ ܬܪ ܩ ܗ ܢ ܢܝ ܢ ܝܕ The number of distnict number 7 The distnict numbers are : 1 2 3 4 5 6 7

Add a comment
Know the answer?
Add Answer to:
java for netbeans Question 2: (Print distinct numbers) Write a program that reads in ten numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming. Write your own source code with comments. (Print distinct numbers) Write a program that...

    Java Programming. Write your own source code with comments. (Print distinct numbers) Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct...

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...

  • Write a program that reads an unknown number (but no more than 100) of integer values...

    Write a program that reads an unknown number (but no more than 100) of integer values from a file (“ass5_Q4_input.txt”), and displays distinct numbers. (i.e. if a number appears multiple times, it is displayed only once.)

  • C++ program by netBeans java language Exercise #2: Write a java program that prompts the user...

    C++ program by netBeans java language Exercise #2: Write a java program that prompts the user to enter a sentence. The program has to find the print: a. the position of vowels in the sentence. b. the number of vowels in the sentence (A, a, U, u, E, e, 0, o, I, i) c. the number of characters as numbers or special characters (other than English letters a..z, A..Z). Hint: remember to ignore the spaces. Sample input-output: Enter the sentnse:...

  • Please use simple java code and comments no arrays Write a program that displays all the...

    Please use simple java code and comments no arrays Write a program that displays all the numbers from 100 to 1,000. ten per line, that are divisible by 5 and 6. Numbers are separated by exactly one space.

  • 6.2 - Write a program that reads numbers and adds them to a list if they...

    6.2 - Write a program that reads numbers and adds them to a list if they aren't already contained in the list. When the list contains ten numbers, the program displays the contents and quits. Use Python 3 Please. Comments in code if you can

  • Write a program that first reads an integer for the array size, then reads numbers into...

    Write a program that first reads an integer for the array size, then reads numbers into the array, computes their average, and finds out how many numbers are above the average. Make sure to include pointer syntax. Example output: Enter array size: 10 10 random numbers between 1 and 10 generated are: 2 8 5 1 10 5 9 9 3 5 The average is: 5.7 Number of items above the average = 4 C++ Code only.

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Task 1 : Write a Java program that prompts for and reads 10 integers from the...

    Task 1 : Write a Java program that prompts for and reads 10 integers from the user into an integer array of size 10, it then: - calculates the sum and average of positive numbers of the array and displays them. [Note: consider 0 as positive] Note: The program must display the message: "There are no positive values" if the array does not contain any positive value.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT