Thursday 4 October 2012

Function016

Write a function to determine whether the input number is prime or not.

#include <stdio.h>
#include <conio.h>
int prime ( int );
void main ( )
{                              
        int      n;
        clrscr ( );
        printf("\n\n\n\t\t Enter a number..:"); scanf("%d",&n);
                  
        if ( prime ( n ) )
        {              
                    printf("\n\t\t The number is a prime number.");
        }                                
        else                 
        {                                        
                    printf("\n\t\t The number is not a prime number.");
        }
        getch ( );
}               
                        
int prime ( int     n )
{                                 
        int    i;
        for ( i = 2; i < n; i++ )
        {                                    
                    if ( n % i == 0 )
                    {                       
                                 return 0;
                    }
        }
        return 1;
}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...