Digit prime number printing within a range..showing wrong output!?

hi all!! :icon_cheesygrin:

This is my first post here so please correct me if i did anything wrong!

ohk so i wrote a program to print all digit prime numbers.
[INFO: digit prime numbers are numbers which are itself prime and their sum of digits is also prime. for eg. 41 is prime and 4+1=5 is also prime so it is a digit prime number.]

Now the program i have written compiles without error but shows a few warnings and doesn't show the desired output.Can't figure out the problem. :(

please help! :icon_neutral:
#include "stdafx.h"
#include "stdio.h"

int isprime(int);
int isdigitprime(int);
int i;
int _tmain(int argc, _TCHAR* argv[])
{
	int u,l;
	printf("Enter lower limit: \n");
	scanf_s("%d",&l);
	printf("Enter upper limit: \n");
	scanf_s("%d",&u);
	for(i=l;i<u;i++)
	{
		if((isprime(i)==1)&&(isdigitprime(i)==1)) //prints only if the number and its sum both are prime
		{
		printf("%d\n",i);
		}
	}
	return 0;
}
int isprime(int x) //checks for prime
{
	for(i=2;i<x-1;i++)
	{
		if((i%x)==0)
			return 0;
	}
	if((x==1)||(i==x))
	return 1;
}
int isdigitprime(int x)
{
	int d,s=0,k=x;
	while(k>0) //adds up the digits
	{
		d=k%10;
		s+=d;
		k=k/10;
	}
if(isprime(s)==1) // checks if the sum of digits is prime or not
	return 1;
}

Comments

  • Sorry the code was posted wrong and i dont know how to edit it! So, i'm posting it here again :icon_redface:
    #include "stdafx.h"
    #include "stdio.h"
    
    int isprime(int);
    void isdigitprime(int);
    int i;
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int u,l;
    	printf("Enter lower limit: \n");
    	scanf_s("%d",&l);
    	printf("Enter upper limit: \n");
    	scanf_s("%d",&u);
    	for(i=l;i<u;i++)
    	{
    		isdigitprime(i);
    	}
    	return 0;
    }
    int isprime(int x) //checks for prime
    {
    	for(i=2;i<x-1;i++)
    	{
    		if((x%i)==0)
    			return 0;
    	}
    		return 1;
    }
    void isdigitprime(int x)
    {
    	int d,s=0,k=x;
    	while(k>0) //adds up the digits
    	{
    		d=k%10;
    		s+=d;
    		k=k/10;
    	}
    if(isprime(s)==1) // checks if the sum of digits is prime or not
    	printf("The sum of digits is %d and the number %d is digit prime ",s,x);
    }
    
  • Move int i out of global space above main, and make it local in both main() and in every other function that uses it.

    The program doesn't call isprime(), so prime numbers are not found. Call isprime() first, and then in another for loop, call isdigitprime.

    This is a C version that calls isprime() with the local int i.
    //#include "stdafx.h"
    #include <stdio.h>
    
    int isprime(int);
    void isdigitprime(int);
    //int i;
    int main(int argc, char* argv[])
    {
    	int i,u,l;
    	printf("Enter lower limit: \n");
    	scanf("%d",&l);
    	printf("Enter upper limit: \n");
    	scanf(" %d",&u);
       
    	for(i=l;i<u;i++)
    	{
    		if(isprime(i))
             printf("%5d",i);
    	}
    	return 0;
    }
    int isprime(int x) //checks for prime
    {
       int i;
    	for(i=2;i<x-1;i++)
    	{
    		if((x%i)==0)
    			return 0;
    	}
    		return 1;
    }
    void isdigitprime(int x)
    {
    	int d,s=0,k=x;
    	while(k>0) //adds up the digits
    	{
    		d=k%10;
    		s+=d;
    		k=k/10;
    	}
    if(isprime(s)==1) // checks if the sum of digits is prime or not
    	printf("The sum of digits is %d and the number %d is digit prime ",s,x);
    }
    
    

    Give that a shot, and add in the loop for isdigitprime().
  • Adak wrote:
    Move int i out of global space above main, and make it local in both main() and in every other function that uses it.

    Thanks a lot!! making int i a local variable instead of global solved the issue!
    Adak wrote:
    The program doesn't call isprime(), so prime numbers are not found. Call isprime() first, and then in another for loop, call isdigitprime.

    yeah..i figured that i was just checking that if the code was running without the isprime() function so i deleted it.. :):):)
  • You're welcome.

    I don't understand - you deleted the isprime() function? How do you then know what numbers are prime, so you can send them to the isdigitprime() function (to see if the prime numbers digits, add up to a prime number themselves)?
Sign In or Register to comment.