# Field Manual for Hackers

## Java - Write a program to count the number of occurrence of digit in a number.

1. import java.util.Scanner;
class Calculate
{
public static void main(String Args[])
{
float number;
System.out.println("enter the number");
Scanner sc=new Scanner(System.in);
number=sc.nextFloat();
int c=0;
System.out.println("enter the digit ");
int dig=sc.nextInt();
int num=(int)number;
while(num!=0)
{
int rem;
rem=num%10;
num=num/10;
if(rem==dig)
{
c++;
}

}
System.out.println("occurance of number is"+c);
}
}

2. import java.util.Scanner;

class Test{

public static void main(String ar[])
{

Scanner sc=new Scanner(System.in);
int num,n0=0,n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0,n9=0,check;
System.out.println("Enter Number");
num=sc.nextInt();
while (num > 0)
{
check=num%10;
if(check==0){n0++;}
else if(check==1){n1++;}
else if(check==2){n2++;}
else if(check==3){n3++;}
else if(check==4){n4++;}
else if(check==5){n5++;}
else if(check==6){n6++;}
else if(check==7){n7++;}
else if(check==8){n8++;}
else{n9++;}
num/=10;
}
System.out.println("0 Occurred = "+n0+" Times");
System.out.println("1 Occurred = "+n1+" Times");
System.out.println("2 Occurred = "+n2+" Times");
System.out.println("3 Occurred = "+n3+" Times");
System.out.println("4 Occurred = "+n4+" Times");
System.out.println("5 Occurred = "+n5+" Times");
System.out.println("6 Occurred = "+n6+" Times");
System.out.println("7 Occurred = "+n7+" Times");
System.out.println("8 Occurred = "+n8+" Times");
System.out.println("9 Occurred = "+n9+" Times");
}
}

3. class Test
{

public static void main(String ar[])
{
long number,number1;
int count=0,i=0;
System.out.print("Enter the number: ");
Scanner o= new Scanner(System.in);
number=o.nextInt();

while(i<=9)
{ count=0;
number1=number;
while(number1!=0)
{
if(i==number1%10)
count++;
number1/=10;
}

System.out.println("Occurence of "+i+" is "+count);
i++;
}

}
}

4. import java.util.Scanner;
class Occurance
{
public static void main(String ar[])
{int Total,n;
int a,b,c,d,e,f,g,h,i,j;
a=b=c=d=e=f=g=h=i=j=0;
int Number;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the Number");
Number=scan.nextInt();
while(Number>0)
{
Total=(Number%10);
n=Total;
Number=Number/10;
if(n==0){a++;}
if(n==1){b++;}
if(n==2){c++;}
if(n==3){d++;}
if(n==4){e++;}
if(n==5){f++;}
if(n==6){g++;}
if(n==7){h++;}
if(n==8){i++;}
if(n==9){j++;}
}
System.out.println("0="+a);
System.out.println("1="+b);
System.out.println("2="+c);
System.out.println("3="+d);
System.out.println("4="+e);
System.out.println("5="+f);
System.out.println("6="+g);
System.out.println("7="+h);
System.out.println("8="+i);
System.out.println("9="+j);

}
}

5. \$num1=\$_POST["num"];
for (\$i=0;\$i<=9;\$i++)
{\$count=0;

\$temp=\$num1;
while(\$temp>=1)
{
if(\$i==\$temp%10)
{
\$count++;
}
\$temp/=10;
}
echo "Occurence of \$i= \$count
";

}

6. import java.util.Scanner;
class nofocc
{
public static void main(String ar[])
{
Scanner scan = new Scanner(System.in);
int num , remainder, c0,c1,c2,c3,c4,c5,c6,c7,c8,c9 ;
c0=c1=c2=c3=c4=c5=c6=c7=c8=c9=0;
System.out.println("enter the number");
num = scan.nextInt();
while(num>0)
{
remainder = num%10;
if(remainder == 0)
c0++;
else if(remainder == 1)
c1++;
else if(remainder == 2)
c2++;
else if(remainder == 3)
c3++;
else if(remainder == 4)
c4++;
else if(remainder == 5)
c5++;
else if(remainder == 6)
c6++;
else if(remainder == 7)
c7++;
else if(remainder == 8)
c8++;
else if(remainder == 9)
c9++;
num /=10;
}
System.out.println("No of occurences of digit 0 are : " +c0);
System.out.println("No of occurences of digit 1 are : " +c1);
System.out.println("No of occurences of digit 2 are : " +c2);
System.out.println("No of occurences of digit 3 are : " +c3);
System.out.println("No of occurences of digit 4 are : " +c4);
System.out.println("No of occurences of digit 5 are : " +c5);
System.out.println("No of occurences of digit 6 are : " +c6);
System.out.println("No of occurences of digit 7 are : " +c7);
System.out.println("No of occurences of digit 8 are : " +c8);
System.out.println("No of occurences of digit 9 are : " +c9);
}
}

7. import java.util.Scanner;
class Digit{
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
int number,rem,num,i=0;
System.out.println("enter the number");
number=obj.nextInt();
System.out.println("enter the digit whose occurrence is to be find out");
num=obj.nextInt();
while(number!=0){
rem=number%10;
number=number/10;
if(rem==num)
i++;
}
System.out.println("the number of occurrence of the given digit is "+i);
}

}

8. import java.util.Scanner;

class Occur{
public static void main(String ar[]){
int number,count=0,occur,rem;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the number:");
number=scan.nextInt();

System.out.println("Enter the digit whose occurrence is to be found:");
occur=scan.nextInt();

while(number>0){
rem=number%10;
number=number/10;

if(rem==occur)
count++;
}
System.out.println("The number of occurrence of digit in the given number:"+count);
}
}

9. package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class OccerenceOfDigit extends Activity {

int num,digit,temp,rem,count=0;
Button btdigit;
TextView tvDigit;
EditText etnum,etdigit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.occerence_of_digit);

btdigit=findViewById(R.id.btdigit);
etdigit=findViewById(R.id.etdigit);
etnum=findViewById(R.id.etnum);
tvDigit=findViewById(R.id.tvdigit);

btdigit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num=Integer.parseInt(etnum.getText().toString());
digit=Integer.parseInt((etdigit.getText().toString()));

temp=num;

while (num>0){
rem=num%10;
if (rem==digit) {
count++;
num=num/10;
}

}
tvDigit.setText("Total occurrence of digit is"+count);
}
});
}
}

10. import java.util.Scanner;
class Occur1{
public static void main(String ar[]){
Scanner s=new Scanner(System.in);
int n,d=0,d0=0,d1=0,d2=0,d3=0,d4=0,d5=0,d6=0,d7=0,d8=0,d9=0;
System.out.println("enter the number");
n=s.nextInt();
while(n>0)
{d=n%10;
if(d==0) d0++;
else if(d==1) d1++;
else if(d==2) d2++;
else if(d==3) d3++;
else if(d==4) d4++;
else if(d==5) d5++;
else if(d==6) d6++;
else if(d==7) d7++;
else if(d==8) d8++;
else if(d==9) d9++;
n=n/10;}
System.out.println("0"+" "+d0+" "+"times");
System.out.println("1"+" "+d1+" "+"times");
System.out.println("2"+" "+d2+" "+"times");
System.out.println("3"+" "+d3+" "+"times");
System.out.println("4"+" "+d4+" "+"times");
System.out.println("5"+" "+d5+" "+"times");
System.out.println("6"+" "+d6+" "+"times");
System.out.println("7"+" "+d7+" "+"times");
System.out.println("8"+" "+d8+" "+"times");
System.out.println("9"+" "+d9+" "+"times");
}
}