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...

  • (Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers...

    (Print distinct numbers) C++ Write a program that reads in 10 numbers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once). The numbers are displayed in the order of their input and separated by exactly one space. (Hint: Read a number and store it to an array if it is new. If the number is already in the array, discard it. After the input, the array contains the distinct numbers.) Sample Run Enter...

  • Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program...

    Hello Sir, I need help with Java. Here is the problem below. 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, and convertMillis(555550000) returns the string 154:19:10. Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1...

  • 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:...

  • In JAVA: Write a method that reverses the array passed the argument and returns this array....

    In JAVA: Write a method that reverses the array passed the argument and returns this array. Write a test program that prompts the user to enter ten numbers, invokes the method to reverse the numbers and display the numbers. Write a method that returns a new array by eliminating the duplicate values in the array using following method header: a. Public static int[] eliminateDuplicates(int list) b. Write a test program that reads in ten integers and invoke the method, the...

  • 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.

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