Subscribe

RSS Feed (xml)

Friday, September 30, 2011

Write a C program to check prime number or not without using recursion

#include<stdio.h>

int isPrime(int);

int main(){

    int num,prime;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    prime = isPrime(num);

   if(prime==1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);

   return 0;
}

int isPrime(int num){

    int i=2;

    while(i<=num/2){
         if(num%i==0)
             return 0;
         else
             i++;
    }

    return 1;
}

4 comments:

  1. ok i have note down this in my program.
    that is good.

    ReplyDelete
  2. good explanation
    http://ccppcoding.blogspot.in/

    ReplyDelete
  3. Bro your doing an awesome jobs ..
    For more Tricky C programs Visit ►► Lightning Code

    ReplyDelete
  4. Great post. Very useful information for beginners. I really thankful for this post. c programming classes in pune

    ReplyDelete