AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Write a java program of digit counter

Digit Counter Suman works with a firm where he has to count the number of digits given to him. Manual counting of digits takes a long time. So being a Graduate Trainee in TCS your job is to help shyam in digitizing the counting work given to him. So if the array is 12,44,85,65,88 then your job is to print the most occurred digit and the digit that never occurred. Digits to be counted will range from 0 to 9 In the above array, most occurred digit is 8 and digits that did not occur at all are 0,3,7,9. Write a java program to achieve the above mentioned problem Input Specification line one size of array (size between 1 and 100) followed by elements of array Output Specification Print the most occurred digit and digit that never occurred in two separate lines.

Sample Input: 4

125 104 44 64

Sample Output:

Most occurred digit : 4

Digits that did not occur : 3,7,8,9

Note: If there are two or more digits that has occurred more, than print the greater of them. (for e.g. if 4,5 are most occurred digit than print 5)


corejava x 353
programming x 168
Posted On : 2018-02-05 17:56:14.0
profile Ananya - anyforum.in Ananya
4100
up-rate
3
down-rate

Answers


import java.util.Scanner;

public class DigitCounter {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
Integer number=0;
int temp =0;
int max=0;
char mostOccuredDigit= ' ';
int[] digits=new int[10];//assume array index represents 0-9 digits

for(int i=0;i<n;i++){
number = input.nextInt();
for(char digit:number.toString().toCharArray()){
int digitNumber=digit-'0';
digits[digitNumber]=digits[digitNumber]+1;
temp=digits[digitNumber];
if(max<temp){
max=temp;
mostOccuredDigit=digit;
}else if(max==temp && digit>mostOccuredDigit){
mostOccuredDigit=digit;
}
}
}
System.out.println("");
System.out.println("Most occurred digit : "+mostOccuredDigit);
System.out.print("Digits that did not occur : ");
boolean isFirst=true;
for(int j=0;j<digits.length;j++){
int count= digits[j];
if(count==0){
System.out.print((!isFirst?",":"")+j);
isFirst=false;
}
}
}
}

Posted On : 2018-02-05 22:50:29
Satisfied : 0 Yes  4 No
profile Garima Gupta - anyforum.in Garima Gupta
596129026817
Reply This Thread
up-rate
0
down-rate
Comments
its showing error @garima....
char mostOccuredDigit='';
profile Ananya - anyforum.in Ananya
4  10  0
Posted On :2018-02-07 21:30:13.0
Leave a Comment
Hi Ananya,
There is space between single quotes. it's ' ' not ''
profile Garima Gupta - anyforum.in Garima Gupta
596  1290  26817
Posted On :2018-02-09 21:11:49.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in