Programming for Problem Solving (3110003)

BE | Semester-1   Winter-2019 | 07-01-2020

Q4) (c)

Build a function to check number is prime or not. If number is prime then function return value 1 otherwise return 0. 

Program

#include <stido.h>
int isprime(int);
void main() {
    int n, c=0;
    printf("\nEnter n: ");
    scanf("%d", &n);

    c = isprime(n);
    if(c > 0) 
        printf("\nNumber %d is not prime.\n", n);
    else 
        printf("\nNumber %d is prime.\n", n);
}        

int isprime(int n) {
    int i, c=0;
    for(i=2; i<n; i++) {
        if(n % i == 0) {
            c++;
            break;
        }
    }
    return c;
}

Output:

Enter n: 6
Number 6 is not prime.


Enter n: 7
Number 7 is prime.