Count of Prime digits in a Number
Last Updated :
20 Dec, 2022
Given an integer N, the task is to count the number of prime digits in N.
Examples:
Input: N = 12
Output: 1
Explanation:
Digits of the number – {1, 2}
But, only 2 is prime number.
Input: N = 1032
Output: 2
Explanation:
Digits of the number – {1, 0, 3, 2}
3 and 2 are prime number
Approach: The idea is to iterate through all the digits of the number and check whether the digit is a prime or not. Since there are only four possible prime numbers in the range [0, 9] and every digit for sure lies in this range, we only need to check the number of digits equal to either of the elements in the set {2, 3, 5, 7}.
Below is the implementation of this approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int countDigit( int n)
{
int temp = n, count = 0;
while (temp != 0) {
int d = temp % 10;
temp /= 10;
if (d == 2 || d == 3
|| d == 5 || d == 7)
count++;
}
return count;
}
int main()
{
int n = 1234567890;
cout << countDigit(n) << endl;
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG {
static int countDigit( int n)
{
int temp = n, count = 0 ;
while (temp != 0 ) {
int d = temp % 10 ;
temp /= 10 ;
if (d == 2 || d == 3
|| d == 5 || d == 7 )
count++;
}
return count;
}
public static void main (String[] args)
{
int n = 1234567890 ;
System.out.println(countDigit(n)) ;
}
}
|
Python3
def countDigit(n):
temp = n
count = 0
while (temp ! = 0 ):
d = temp % 10
temp / / = 10
if (d = = 2 or d = = 3 or d = = 5 or d = = 7 ):
count + = 1
return count
if __name__ = = '__main__' :
n = 1234567890
print (countDigit(n))
|
C#
using System;
class GFG {
static int countDigit( int n)
{
int temp = n, count = 0;
while (temp != 0) {
int d = temp % 10;
temp /= 10;
if (d == 2 || d == 3
|| d == 5 || d == 7)
count++;
}
return count;
}
public static void Main ( string [] args)
{
int n = 1234567890;
Console.WriteLine(countDigit(n)) ;
}
}
|
Javascript
<script>
function countDigit(n)
{
let temp = n, count = 0;
while (temp != 0) {
let d = temp % 10;
temp = Math.floor(temp / 10);
if (d == 2 || d == 3
|| d == 5 || d == 7)
count++;
}
return count;
}
let n = 1234567890;
document.write(countDigit(n) + "<br>" );
</script>
|
Time Complexity: O(log10N), where N is the length of the number.
Auxiliary Space: O(1)
Method#2:Use Recursion
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit( int n, int count=0)
{
if (n==0)
return count;
int d = n % 10;
n /= 10;
if (d == 2 || d == 3|| d == 5 || d == 7){count++;}
countDigit(n,count);
}
int main()
{
int n = 1234567890;
cout << countDigit(n) << endl;
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG
{
static int countDigit( int n, int count)
{
if (n == 0 )
return count;
int d = n % 10 ;
n /= 10 ;
if (d == 2 || d == 3 || d == 5 || d == 7 ) {
count++;
}
return countDigit(n, count);
}
public static void main(String[] args)
{
int n = 1234567890 ;
System.out.println(countDigit(n, 0 ));
}
}
|
Python3
def countDigit(n, count = 0 ):
if (n = = 0 ):
print (count)
return ;
d = n % 10 ;
n = int (n / 10 );
if (d = = 2 or d = = 3 or d = = 5 or d = = 7 ):
count + = 1
countDigit(n, count);
n = 1234567890 ;
countDigit(n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int countDigit( int n, int count = 0)
{
if (n == 0)
return count;
int d = n % 10;
n /= 10;
if (d == 2 || d == 3 || d == 5 || d == 7) {
count++;
}
return countDigit(n, count);
}
public static void Main( string [] args)
{
int n = 1234567890;
Console.WriteLine(countDigit(n));
}
}
|
Javascript
function countDigit(n, count = 0)
{
if (n == 0)
{
console.log(count);
return ;
}
let d = n % 10;
n = Math.floor(n / 10);
if (d == 2 || d == 3|| d == 5 || d == 7)
{
count++;
}
countDigit(n,count);
}
let n = 1234567890;
countDigit(n)
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...