/* Function to find prime number if so return 1 else return 0 */
#include<stdio.h>
#include<math.h>
int isprime( int num )
{
        int i  ;
        int sq = ( int ) sqrt ( num );
        /* here check should be done for num = 0 and 1 other wise 0   and 1 are printed as primes */
         /*  if ( num <= 1) return 0;  */        
       for (  i = 2 ; i <= sq ; i++ )
        {
                if ( num % i == 0 )
                { 
                     break ;
                }
       }
  if ( i <= sq )  
     return  0;
 else 
    return  1 ;
}
int main( void )
{
     int num , ch;
     while ( 1 )
     {
         printf("Enter a Num : ");
         scanf("%d", &num);
         if ( isprime( num ) )
             printf("Num ( %d ) is prime \n" , num);
        else 
            printf("Num ( %d ) is not prime \n", num);
    
         printf("Press  < Enter > to continue \n");
         if ( (  ch = getchar() )  != '\n' )
                 break ;
    }
  return 0;
}

in many of the previous posts i dint observe the check for 0 and 1.
please let me know whether this method is correct or not.

Recommended Answers

All 6 Replies

> please let me know whether this method is correct or not.
You mean you can't tell?

Does it, or does it not output a list of prime numbers?

I'm sure it could be more efficient, but first you need to make sure it produces the right answers.

> please let me know whether this method is correct or not.
You mean you can't tell?

Does it, or does it not output a list of prime numbers?

I'm sure it could be more efficient, but first you need to make sure it produces the right answers.

Yet Another Version of Findind a Prime number:

#define CHK_NUM_IS_2_3_5( n )  ( n == 2 || n == 3 || n == 5 )
#define REM_2( n )  ( n % 2 != 0 ) 
#define REM_3( n )  ( n % 3 != 0 ) 
#define REM_5( n )  ( n % 5 != 0 ) 
#define CHK_REM_2_3_5( n ) ( REM_2( n ) && REM_3( n ) && (REM_5( n ) )           
                     
#include<stdio.h>

int main()
{
  int num ;
	
  REPEAT : 
    printf( "Enter a Num: " ) ;
    scanf( "%d", &num ) ;
    if( num <= 1 )
            printf(" ( %d ) is not PRIME \n " , num );
    else if  ( CHK_NUM_IS_2_3_5 ( num ) )
           printf(" ( %d ) is PRIME \n " , num );
   else if (CHK_REM_2_3_5 ( num ) )    
             printf(" ( %d ) is PRIME \n " , num );
  else
           printf(" ( %d ) is not PRIME \n " , num );
 printf(" Press < Enter > to continue..\n ");
 getchar();
 if ( getchar() < 0 )
	goto END;
goto REPEAT ;
END :
return 0;
}

i Hope this is the Efficient Version of All.
Any other ways, Plz Welcome.

Yet Another Version of Findind a Prime number:

#define CHK_NUM_IS_2_3_5( n )  ( n == 2 || n == 3 || n == 5 )
#define REM_2( n )  ( n % 2 != 0 ) 
#define REM_3( n )  ( n % 3 != 0 ) 
#define REM_5( n )  ( n % 5 != 0 ) 
#define CHK_REM_2_3_5( n ) ( REM_2( n ) && REM_3( n ) && (REM_5( n ) )           
                     
#include<stdio.h>

int main()
{
  int num ;
	
  REPEAT : 
    printf( "Enter a Num: " ) ;
    scanf( "%d", &num ) ;
    if( num <= 1 )
            printf(" ( %d ) is not PRIME \n " , num );
    else if  ( CHK_NUM_IS_2_3_5 ( num ) )
           printf(" ( %d ) is PRIME \n " , num );
   else if (CHK_REM_2_3_5 ( num ) )    
             printf(" ( %d ) is PRIME \n " , num );
  else
           printf(" ( %d ) is not PRIME \n " , num );
 printf(" Press < Enter > to continue..\n ");
 getchar();
 if ( getchar() < 0 )
	goto END;
goto REPEAT ;
END :
return 0;
}

i Hope this is the Efficient Version of All.
Any other ways, Plz Welcome.

I think your program will flag 49 as a prime number. 49 cannot be divided by 2, 3 or 5.

Yet Another Version of Findind a Prime number:

snipped

i Hope this is the Efficient Version of All.
Any other ways, Plz Welcome.

I hope this is a joke.

Please, do not take this code seriously... :icon_rolleyes:

#define CHK_REM_2_3_5( n ) ( REM_2( n ) && REM_3( n ) && (REM_5( n ) )

@Gaiety
I think there are more divisors other than 2, 3 & 5

Please clear your concept of prime numbers

#define CHK_REM_2_3_5( n ) ( REM_2( n ) && REM_3( n ) && (REM_5( n ) )

@Gaiety
I think there are more divisors other than 2, 3 & 5

Please clear your concept of prime numbers

Thanks for all replies. i dint take that as a serious program .

i just expected that would be.

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.