Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Count occurrences
- Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops).
- Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
- Input
- On the first line you will be given n.
- Output
- On the only output line you should print the count of 7s.
- Constraints
- n>=0
- Sample tests
- Input
- 717
- Output
- 2
- Input
- 7
- Output
- 1
- import java.util.Scanner;
- public class CountOccurrences {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- System.out.println(result(input, '7'));
- }
- public static int result(String n, char c ) {
- int count = 0;
- if (n.length() > 0) {
- if (n.charAt(0) == c) {
- count++;
- }
- count += result(n.substring(1), c);
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment