Question

Java Programming. Write your own source code with comments.

75.jpg

(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 numbers. Here is the sample run of the program: Enter ten numbers: 1 2 3 2 1 6 3 4 5 2 The number of distinct number is 6 The distinct numbers are: 1 2 3 6 4 5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.io.*;
import java.util.Scanner;
class Distinctnumbers
{
public static void main(String[] args)
{
Scanner input=new Scanner (System.in); // Creating Scanner Object for user input
System.out.print("Enter ten Numbers: "); //prompt the user to enter 10 numbers
int[] values = new int[20]; //create an array to hold the numbers
int count = 0; //initalize count value to zero
for (int i=0; i<10; i++) //Here we are creating loop to find which numbers are distinct
{
int number = input.nextInt(); // Here we are taking input from the user
boolean distinct = true;
  
for (int j=0; j<count; j++)
{
if (number == values[j])
{
distinct = false;
break;
} // End of if
} // End of for loop
if (distinct) values[count++] = number;
} // End of for loop
System.out.print("The Distinct Numbers are: "); //print out "The Distinct Numbers are"
for (int i=0; i<count; i++) //Here we creating loop to print out each distinct number
{
System.out.print(values[i]+ " "); // Here each distinct number is seperated by space
}
} // End of main
} // End of class

Output:

Administrator: Windows Command Processor F:くjava〉javac Distinctnumbers.java F:〈java〉java Dis t İnc t numbers Enter ten Number

Add a comment
Know the answer?
Add Answer to:
Java Programming. Write your own source code with comments. (Print distinct numbers) Write a program that...
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 for netbeans Question 2: (Print distinct numbers) Write a program that reads in ten numbers...

    java for netbeans Question 2: (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 numbers. Here is the...

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

  • Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your...

    Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your own code. 7.18 (Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubblesort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is not in order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their...

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

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

  • 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 write c programming for this code below. Write a program to print a pattern of...

    please write c programming for this code below. Write a program to print a pattern of numbers separated by spaces for the given number of rows. At the time of execution, the program should print the message on the console as: Enter number of rows For example, if the user gives the input as: Enter number of rows : 4 then the program should print the result as: 232 34543 4567654

  • write a program to print the largest of any three distinct numbers? ( in java)

    write a program to print the largest of any three distinct numbers? ( in java)

  • Can you write with comments please. Thank you. Write a Java Program that asks the user...

    Can you write with comments please. Thank you. Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

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