Saturday, December 7, 2013

Java Program to find the occurences of a digit in a number

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Occurences {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the number ");
        int first = Integer.parseInt(reader.readLine());
        System.out.println(" Enter the digit to be searched ");
        int second = Integer.parseInt(reader.readLine());
        int count = 0, m;
        while (first > 0) {
            m = first % 10;
            if (m == second)
                count++;
            first = first / 10;
        }

        System.out.println("Number Of Occurences " + count);
    }
}

No comments:

Post a Comment