Lucky Seven Problem in Java

In this Java tutorial, we will learn how to solve lucky seven problem in Java. We will also take a look at the code snippet with the output.

Problem Statement – Solve Lucky Seven problem in Java

In the problem, we are provided with a number. We have to find the total count of digit 7 in the number.

Example: The number is 7487 so, the total count of digit 7 in the number is 2.

Integer to String Conversion in Java

  Syntax :- Integer.toString( number ) ;

from this expression number will be converted into String .

String to Character Array Conversion in Java

Syntax:- String.toCharArray () ;

from this expression String will be converted to character array.

Also, learn,

Algorithm to solve lucky seven problem in Java

  1. Scan a number n .
  2.  Convert the number to String using Integer.toString() .
  3. Convert String to character array using String.toCharArray() .
  4. Now, check for each character in CharArray formed if it is ‘7’ increment count.
  5. Finally, print the count.

Java Code to count the total occurrence of digit 7 in a given number

import java.util.Scanner;

   public class Codespeedy

     public static void count ( int n )
       String str = Integer.toString(n);
         char [] s = str.toCharArray();
       int count=0;
         for( int i=0 ; i<s.length ; i++ )
             if ( s[i]=='7')
       public static void main ( String[] args )
      Scanner scan = new Scanner (;
       int n;
       n = scan.nextInt();






In the given number 7 occurs 3 times so, the output is 3.

Also learn,

Leave a Reply

Your email address will not be published. Required fields are marked *