Implement using functions to check whether the given number is prime and display appropriate messages(No built-in math function)

All QuestionsCategory: C LanguageImplement using functions to check whether the given number is prime and display appropriate messages(No built-in math function)
nikhil.d asked 3 years ago
2 Answers
nikhil.d answered 3 years ago

#include <stdio.h>
#include <stdlib.h>
int Isprime(int n);
int main()
{
    int n;
    printf(“enter a positive integer\n”);
    scanf(“%d”, &n);
    if (Isprime(n))
        printf(“%d is a prime number\n”, n);
    else
        printf(“%d is not a prime number\n”, n);
    return 0;
}
int Isprime(int n)
{
    int i;
    if (n == 1)
    {
        printf(“its a unique number\n”);
        exit(0);
    }
    for (i = 2; i <= n / 2; i++)
    {
        if (n % i == 0)
        {
            return (0);
        }
    }
    return 1;
}

Anonymous answered 3 years ago

Note: Change the double inverted comma ( ” ) in the code to avoid an error.