Friday, July 10, 2015

prime number by function

#include <stdio.h>

void prime(int num);

int main(){
    int n;
    printf("Please enter a number:");
    scanf("%d",&n);
    prime(n);
    return 0;
}
void prime(int num){

    int i;
for(i=2; i<=num-1; i++){
    if(num%i==0){
        printf("%d is not a prime number",num);
        break;
    }
    }
    if(num==i)
    printf("%d is a prime number",num);
}



///another way

#include<stdio.h>
int prime(int a);

int main()
{
    int num,i,n;
    num=prime(n);
    for(i=2; i<=num-1; i++)
    {

    if(num%i==0){
        printf("%d is not a prime number",num);
        break;
    }
    }
    if(num==i)
        printf("%d is a prime number",num);

    return 0;
}
int prime(int a){

    printf("Please enter a number:");
    scanf("%d",&a);
    return a;

}

No comments:

Post a Comment