Hard Prime Numbers

Problem Statement
A prime number is a number, which can be divided evenly only by 1 or
itself, and it must be greater than 1.
For example, 7 can be divided evenly only by 1 or 7, and 7 is greater than
1, so it is a prime number.
Let us call a prime number is hard if its sum of digits (in decimal
convention) is also prime. For example, 23 is a hard prime, but 13 is not.
Write a full program implementing the following functions:
(i)
checkPrime: It takes a number as a parameter and returns 1 if it is
a prime number, else it returns 0.
(ii)
addDigit: which takes a number as parameter and returns its sum
of digit.
(iii) A main function which reads a number N, and prints all the hard
primes and their number (counting them) till N.
#include<stdio.h>
int checkprime(int x);
int adddigit(int x);
int main()
{
  int n,i,y;
  printf("enter the number n\n");
  scanf("%d",&n);
  for(i=1;i<n;i++)
    {
      if(checkprime(i)==1)
    {
      y=adddigit(i);
      if(checkprime(y)==1)
        {
          printf("%d ",i);
        }
    }
    }
  return 0;
}
int checkprime(int n)
{
  int temp=2;
  while(temp<n)
    {
      if(n%temp==0)
    {
      return 0;
    }
      temp++;
    }
  return 1;
}
int adddigit(int n)
{
  int sum=0;
  while(n>=1)
    {
      sum=sum+n%10;
      n=n/10;
    }
  return sum;
}

No comments:

Post a Comment