Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


mira
numbers
1 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


Write a program that read 15 integers between 0 and 6 and a method to search, displays and returns the number that has the highest occurrence. Prompt the user if the number is not within this range.


Advertisement
View Answers

October 26, 2010 at 12:00 PM


Hello Friend,

Try the following code:

import java.util.*;

public class NumberExample {
        public static void searchAndDisplay(int[] arr) {
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        ArrayList<Integer> list2 = new ArrayList<Integer>();
        for (int element : arr) {
            int index = list1.indexOf(element);
            if (index != -1) {
                int newCount = list2.get(index) + 1;
                list2.set(index, newCount);
            } else {
                list1.add(element);
                list2.add(1);
            }
        }
        int maxCount = 0;
        int index = -1;
        for (int i = 0; i < list2.size(); i++) {
            if (maxCount < list2.get(i)) {
                maxCount = list2.get(i);
                index = i;
            }
        }
        System.out.println("Number " + list1.get(index)+ " has highest occurrence i.e " + maxCount);
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] arr = new int[15];
        System.out.println("Enter 15 integers (0-6):");
        for (int i = 0; i < arr.length; i++) {
            int num = input.nextInt();
            if (num <= 6 && num >= 0) {
                arr[i] = num;
            } else {
                System.out.print("Enter another number: ");
                num = input.nextInt();
            }
        }
        searchAndDisplay(arr);
    }
}

Thanks



Related Tutorials/Questions & Answers:
automorphic numbers
automorphic numbers  how to find automorphic number in java   Hi Friend, Pleas visit the following link: Automorphic numbers Thanks
Advertisements
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents
Numbers pyramid
Numbers pyramid  Hi sir, Can you please tell me how to output this using nested for loops? 1 2, 1 1, 2, 3 4, 3, 2, 1 1, 2, 3, 4, 5 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7 8, 7, 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 9, 8
Rational Numbers
Rational Numbers   Write and fully test a class that represents rational numbers. A rational number can be represented as the ratio of two integer values, a and b, where b is not zero. The class has attributes for the numerator
Listing all even numbers between two numbers
Listing all even numbers between two numbers  Hi, How to write code to list all the even numbers between two given numbers? Thanks   Hi, You function will take 2 numbers and the find the even numbers between
how to add to numbers in java
how to add to numbers in java  how to add to numbers in java
Hexadecimal numbers multiplication
Hexadecimal numbers multiplication  Sir, I have to multiply 128 bit hexadecimal numbers. Do u have any logic for this?? The numbers are like ab7564fa342b5412c34d9e67ab341b58
Addition of two numbers
Addition of two numbers  addition of two numbers
defining numbers in Java Script
defining numbers in Java Script  Explain about defining numbers in Java Script
multiply of 100 digits numbers
multiply of 100 digits numbers  multiplying 100 digits numbers to eachother
numbers divisible by 5 and 6
numbers divisible by 5 and 6  Find the first ten numbers which are greater than Long.MAX_VALUE divisible by 5 and 6
prime numbers - Java Beginners
prime numbers  Write a java program to find the prime numbers between n and m
Adding two numbers
Adding two numbers  Accepting value ffrom the keyboard and adding two numbers
Generate Random Numbers
Generate Random Numbers  hi,,, Please give me answer with example How do you generate random numbers within a given limit with actionscript... This function generate random numbers with in given limit
random numbers - Java Beginners
random numbers  write a program to accept 50 numbers and display 5 numbers randomly  Hi Friend, Try the following code: import...); System.out.println("Enter 10 numbers: "); for(int i=0;i<10;i
odd numbers with loop
odd numbers with loop  get the odd numbers till 100 with for,while loop   Java find odd numbers: class OddNumbers { public static void main(String[] args) { for(int i=1;i<=100;i
recursion numbers - Java Beginners
recursion numbers  I need to use recursion to test all values from 0 to 20 and see if they are contain in a 1-D array with values: 2,4,6,8,10,12,14,16,18,20. The results of all numbers from 0-20 will be printed
Sum of positive and negative numbers
; import java.util.*; class Numbers { public static void main(String...=0,sum2=0; int num[]=new int[10]; System.out.println("Enter 10 numbers...]; } } System.out.println("Sum of positive numbers: "+sum2); System.out.println
Application for print pyramid of numbers
Application for print pyramid of numbers  What is the code for printing the numbers like 1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
Sum of first n numbers
Sum of first n numbers  i want a simple java program which will show the sum of first n numbers....   import java.util.*; public class...; } System.out.println("Sum of Numbers from 1 to "+n+" : "+sum
random numbers - Java Beginners
to display the random numbers, but not twice or more. I mean i need a number to be display once. This code allows some numbers to be displayed more than once. Hi... Scanner(System.in); System.out.println("Enter 10 numbers: "); for(int i=0;i<10;i
Prime Numbers - IDE Questions
Prime Numbers  Create a program that calculates the prime numbers... numbers in rows of 10. Hint: Use a counter to count how many values are printed... = 0; System.out.println("Prime Numbers are:" ); for (int i = 1; i <
Perfect Numbers - Java Beginners
numbers. Use methods   Hi Friend, Try the following code: public
Random numbers - Development process
Random numbers  hi, How to generate unique random numbers between range like(10 to 50)  Hi friend, class RandomNumber { public static void main(String[] args) { //set the variable aNumber
print numbers in traingle shape
print numbers in traingle shape  1 22 333 4444 i want output like this please help me   class Pyramid { public static void main(String[] args) { for(int i=1;i<=4;i++){ for(int j=1;j<
Divide 2 numbers
Divide 2 numbers  Write a java program to divide 2 numbers. Avoid division by zeor by catching the exception.   class Divide { public static void main(String[] args) { try{ int num1=8
finding the prime numbers
finding the prime numbers  Hi, I am a beginner to java and I have problem with the code in finding the prime numbers, can someone tell me about the problem. Thanks.   Want to know about how to find out the given
Prime Numbers from range
Prime Numbers from range  how to find prime numbers in a given range which are palindromes??   Hi Friend, Try the following code: import java.util.*; public class PrimeAndPalindrome { static final int MAXNUMBER
Applet for add two numbers
Applet for add two numbers  what is the java applet code for add two numbers?   import java.awt.Graphics; import javax.swing.*; public...); add(text2); label3 = new Label("Sum of Two Numbers
prime numbers application
prime numbers application  how to add a comment indicating that the method will determine if a number is a prime number. In the next line add the method header for this method which will be called isPrime. This method returns
permutstion of numbers in java
permutstion of numbers in java  Is it possible to enter the number in a char so that permutation of a number can be acheived which will be in the form of string????? here is the coding i did...it worked really well when i
Java program - convert words into numbers?
Java program - convert words into numbers?   convert words into numbers?   had no answer sir
convert numbers to strings using JavaScript
convert numbers to strings using JavaScript  How to convert numbers to strings using JavaScript
Generating random numbers in a range with Java
Generating random numbers in a range with Java  Generating random numbers in a range with Java
How to put text file numbers to array and check the position of numbers?
How to put text file numbers to array and check the position of numbers?   I have numbers in text file data.txt 12 9 8 3 1
Convert Roman Numbers to Decimal Numbers
Convert Roman Numbers to Decimal Numbers In the previous section, we have illustrated the conversion of Decimal Numbers to Roman. Here we are doing vice-versa i.e, In this sd For this, we have allowed the user to enter Roman Number like
Generate array of random numbers
Generate array of random numbers You can generate a random number either...() but it is better to use Random class. If you want to generate a series of random numbers.... In this section, we are going to accept 10 numbers and display 5 numbers from them
Need to have numbers printed like this
Need to have numbers printed like this   1 2 0 2 3 0 0 0 3 4 0 0
EVEN NUMBERS - Java Interview Questions
EVEN NUMBERS  i want program of even numbers?i want source code plz reply?  Hi Friend, Try the following code: class EvenNumbers... counter = 0; System.out.println("Even Numbers are:" ); for (int i
Printing numbers up to N into a file
Printing numbers up to N into a file  I'd like to print the first N integers, that is, "1, 2, 3, 4, ..., N-1, N", say N equals 1000, or 10000 or whatever. I'd also like to have the result stored as a file instead of having
print 100 numbers using loops
print 100 numbers using loops  how to print from 1 to 100 using for loop ?   Hi Friend, You can use the following code: class Loop...); } } } Thanks   class Numbers { public satic void main
generate random numbers and display the largest
generate random numbers and display the largest  Hi, I am using netbeans so if someone could help me with an answer in that form i'd appreciate it. Write a method to find the largest value in an array. Write a program that takes
adding of two numbers in designing of frame
adding of two numbers in designing of frame  hello sir, now i'm create two textfield for mark1&mark2 from db.how to add these two numbers in another one text field.how to write a coding... if u have another one idea pls
Java Find Automorphic numbers
Java Find Automorphic numbers In this section, you will learn how to find the automorphic numbers between 1 and 1000. Automorphic numbers are the numbers... of number 6 at the end. Here we are going to find the automorphic numbers between 1
add tow numbers with out using arthamatic operaters
add tow numbers with out using arthamatic operaters  add tow numbers with out using arthamatic operaters
Add two big numbers
Add two big numbers       In this section, you will learn how to add two big numbers. For adding two numbers implement two big decimal numbers then apply the Sum() method
Write a program to list all even numbers between two numbers
Write a program to list all even numbers between two numbers       Java Even Numbers - Even Numbers Example in Java: Here you will learn to write a program for listing out
Textbox allows only numbers in java wicket
Textbox allows only numbers in java wicket  Please provide me wicket code for text box that allows only numbers to type. Thank you
 

 

 

DMCA.com