‘C’ function to check if a number is prime.

C File =>


/* Write a ‘C’ function to check if a number is prime. Use this function to 
display all prime numbers between 100 and 500. */

#include<stdio.h>
int main() 
{
       int i;
 
  // function to check if number is prime
       int isprime(int n)
      {
           int flag=1,i;
           for(i=2;i<n-1;i++)
           {
               if(n%i==0)
              {
                  flag=0;
                  break;
               }
           }
          return flag;
       }
  
       printf("\n Prime numbers between 100 and 500 :\n");
 
      for(i=100;i<500;i++)
      {
          if(isprime(i)==1)
               printf("%d\t",i);
      }
 
      return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.