Menu Close

Occurrences of the digit 4 Java .. – FTC

Kostya likes the number 4 much. Of course! This number has such a lot of properties, like:

  1. Four is the smallest composite number;
  2. It is also the smallest Smith number;
  3. The smallest non-cyclic group has four elements;
  4. Four is the maximal degree of the equation that can be solved in radicals;
  5. There is four-color theorem that states that any map can be colored in no more than four colors
    in such a way that no two adjacent regions are colored in the same color;
  6. Lagrange’s four-square theorem states that every positive integer can be written as the sum of at most four square numbers;
  7. Four is the maximum number of dimensions of a real division algebra;
  8. In bases 6 and 12, 4 is a 1-automorphic number;
  9. And there are a lot more cool stuff about this number!

Impressed by the power of this number, Kostya has begun to look for occurrences of four anywhere. He has a list of T integers, for each of them he wants to calculate the number of occurrences of the digit 4 in the decimal representation. He is too busy now, so please help him.

Input
The first line of input consists of a single integer T, denoting the number of integers in Kostya’s list.
Then, there are T lines, each of them contain a single integer from the list.

Output
Output T lines. Each of these lines should contain the number of occurrences of the digit 4 in the respective integer from Kostya’s list.

Input:
Enter the TestCase:  1
Enter any number:  4548464943142445

Output:
Count = 8
import java.io.*;
import java.util.*;
public class temp{
  public static void main(String[] args) throws IOException
  {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the TestCases:  ");
		int t=sc.nextInt();
    for(int i=0;i<t;i++)
    {
      System.out.print("Enter any number:  ");
      long n=sc.nextLong();
			long count=0,rem=0;

			while(n!=0)
			{
				rem=n%10;
				n=n/10;

				if(rem==4)
				{
					count++;
				}
			}
      System.out.println("Count = "+count);
    }
  }
}

INPUT_1:
Enter the TestCases:  2

Enter any number:  485485644745
Count = 5

Enter any number:  424244
Count = 4


INPUT_2:
Enter the TestCases:  1

Enter any number:  4548464943142445
Count = 8


INPUT_3:
Enter the TestCases:  2

Enter any number:  4444451404
Count = 7

Enter any number:  1014210152
Count = 1


INPUT_4:
Enter the TestCases:  3

Enter any number:  4584945
Count = 3

Enter any number:  2152365
Count = 0

Enter any number:  48521
Count = 1


INPUT_5:
Enter the TestCases:  4

Enter any number:  141414
Count = 3

Enter any number:  111114
Count = 1

Enter any number:  152461185459443
Count = 4

Enter any number:  456894
Count = 2


INPUT_6:
Enter the TestCases:  5

Enter any number:  246810
Count = 1

Enter any number:  3691215
Count = 0

Enter any number:  4812162024
Count = 2

Enter any number:  51015253035
Count = 0

Enter any number:  1011101111014
Count = 1


ILLUSTRATION

Executed using javac on linux terminal