Write a program that will print all highly prime numbers from the input interval <a,b>. Prime number is highly prime if deletion of every digit from right is a prime.
Example:
239 is highly prime because 239,23,2 are primes.

Could someone point out what are logic errors in the following program:

#include <stdio.h>

int isPrime(int n);
int isHighlyPrime(int n);
void printHighlyPrimeNumbers(int a,int b);

// check if n is a prime
int isPrime(int n)
{
   int i=2;
   while(i < n)
   {
      if((n % i) == 0)
        return 0;
      i=2*i-1;
   }
   return 1;
}

// check if n is highly prime prime
int isHighlyPrime(int n)
{
   int temp=n;
   int i;
   int digitCount=0;//number of digits in n

   while(temp != 0)
   {
       temp=temp/10;
       digitCount++;
   }
   temp=n;

   //if number of digits is 1, only check if prime
   if(digitCount == 1)
   {
       if(isPrime(temp))
          return 1;
   }

   //else check if n,n/10,(n/10)/10,... are primes
   else
   {
       for(i=1; i<=digitCount-1 ; i++)
       {
          if(isPrime(temp))
              temp=temp/10;
          else break;
       }
       return 1;
   }
   return 0;
}

//print highly primes from a to b
void printHighlyPrimeNumbers(int a,int b)
{
   int i;
   for(i=a; i<=b; i++)
   {
      if(isHighlyPrime(i))
            printf("%d ",i);
   }
}

int main()
{
    int a,b;
    do
    {
        printf("a = ");
        scanf("%d",&a);
    }while(a<1);
    do
    {
        printf("b = ");
        scanf("%d",&b);
    }while(b<1);

    //swap if a>b
    if(a > b)
    {
        int temp=a;
        a=b;
        b=temp;
    }

    printHighlyPrimeNumbers(a,b);

    return 0;
}

I have figured out the problem, here is the working program:

#include <stdio.h>

int isPrime(int p);
int isHighlyPrime(int n);
void printHighlyPrimeNumbers(int a,int b);

// check if p is a prime
int isPrime(int p)
{
    int i = 2;
    while(i<p){
        if(!(p % i)) return 0;
        i = 2*i-1;
    }
    return 1;
}

// check if n is highly prime
int isHighlyPrime(int n)
{
   int temp=n;
   int i;
   int digitCount=0;//number of digits in n

   while(temp != 0)
   {
       temp=temp/10;
       digitCount++;
   }
   temp=n;

   //if number of digits is 1, only check if prime
   if(digitCount == 1)
   {
       if(isPrime(temp))
          return 1;
   }

   //else check if n,n/10,(n/10)/10,... are primes
   else
   {
       while(temp>0)
       {
           if(isPrime(temp) == 0)
           {
               return 0;
           }
           temp =temp/10;
       }
       return 1;
   }
   return 0;
}

//print highly primes from a to b
void printHighlyPrimeNumbers(int a,int b)
{
   int i;
   for(i=a; i<=b; i++)
   {
      if(isHighlyPrime(i))
            printf("%d ",i);
   }
}

int main()
{
    int a,b;
    do
    {
        printf("a = ");
        scanf("%d",&a);
    }while(a<1);
    do
    {
        printf("b = ");
        scanf("%d",&b);
    }while(b<1);

    //swap if a>b
    if(a > b)
    {
        int temp=a;
        a=b;
        b=temp;
    }

    printHighlyPrimeNumbers(a,b);

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.