Is digital root of N a prime? | Practice | GeeksforGeeks

Given a number N, you need to find if its digital root is prime or not. DigitalRoot of a number is the repetitive sum of its digits until we get a single-digit number.
Eg.DigitalRoot(191)=1+9+1=>11=>1+1=>2
Example 1:
Input:
N = 89
Output:
0
Explanation:
DigitalRoot(89)=>17=>1+7=>8; not a prime.
Example 2:
Input:
N = 12
Output:
1
Explanation:
DigitalRoot(12)=>1+2=>3; a prime number.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function digitalRoot() which takes an integer N as an input parameter and return 1 if its digital root is a prime number otherwise return 0.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 104
0
raghvendrapratap76992 months ago
 
class Solution{public:   int digitalRoot(int n)   {       int sum=0,x,sum2=0,z;       while(n!=0)       {           x=n%10;           sum=sum+x;           n=n/10;       }       if(sum<10)       {           if(sum==2 || sum==3)           return true;           if(sum==5 || sum==7)           return true;       }              else       {           while(sum!=0)           {              z=sum%10;              sum2=sum2+z;              sum=sum/10;           }           if(sum2==2 || sum2==3)           return true;            if(sum2==5 || sum2==7 )           return true;           if (sum2==11)           return true;                  }       return false;   }      };
0
pranjal pandey10 months ago
pranjal pandey
https://uploads.disquscdn.c...
0
pranjal pandey
This comment was deleted.
0
pranjal pandey10 months ago
pranjal pandey
IF ANYONE GET O(1) solution PLEASE TELL.
bool isPrime(int m){       if(m==1)return false;       if(m==2)return true;       for(int i=2;i*i<m;i++){ if(m%i="=0){" return="" false;="" break;}="" }="" return="" true;="" }="" int="" digital_root(int="" n){="" if(n<10)return="" n;="" int="" x="n%10;" return="" digital_root(x+n="" 10);="" }="" int="" digitalroot(int="" n)="" {="" int="" m="digital_Root(N);" return="" isprime(m);="" }="">
0
pranjal pandey
This comment was deleted.
0
Yash Patil2 years ago
Yash Patil
public static void main (String[] args) {Scanner sc1 =new Scanner(System.in);int test=sc1.nextInt();while(test!=0){    int num=sc1.nextInt();    int temp=num;    int k=1,sum=0;    while(temp!=0)    {        sum+=(temp%10);        temp/=10;    }    if((sum*k)%9==0)    {        System.out.println(0);    }    else    {        if(isPrime(((sum*k)%9)))        {            System.out.println(1);        }        else        {            System.out.println(0);        }    }    test--;}}public static boolean isPrime(int num){    if(num==0||num==1)    {        return false;    }    for(int i=2;i*i<=num;i++)    {        if(num%i==0)        {            return false;        }    }    return true;}
0
Shashwat Sharma2 years ago
Shashwat Sharma
0.01 sec....#include <iostream>using namespace std;
int main() {//codeint t;  cin>>t;while(t-->0){    int sum=0,cnt=0,n;  cin>>n;    while(n>0 || sum>9)    {        if(n==0)        {            n=sum;            sum=0;        }        sum+=n%10;        n/=10;    }    for(int i=1;i<=sum;i++)    {        if(sum%i==0)          cnt++;    }    if(cnt==2)       cout<<"1"<<endl; else="" cout<<"0"<<endl;="" }="" return="" 0;="" }="">
0
Govind Kumar2 years ago
Govind Kumar
0
Ayush Mishra2 years ago
Ayush Mishra
/*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {Scanner sc= new Scanner(System.in);int n=sc.nextInt(); while (n-->0){    sc.nextLine();    int a= sc.nextInt();    int sum=0,x=0;    while (a!=0)    {        x=a % 10;        sum=sum+x;        a/=10;        if (sum>=10 && a==0)        {            a=sum;            sum=0;        }    }    int length=0;    for (int i=2;i<sum;i++) {="" if="" (sum="" %="" i="=0)" {="" length++;="" }="" }="" if="" (length="=0" &&="" sum!="1)" {="" system.out.println("1");="" }="" else="" if(sum="=1" ||="" length="">0)    {        System.out.println("0");    }}}}
0
nandini2 years ago
nandini
please help me.my o/p is correct but then also its giving wrong.https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.