Responsive Ads Here

Tuesday, November 25, 2014

Prime number

4 way / 4 type

 

Simple way

#include<stdio.h>
int main()
{
    int n,i,count=0;
    printf("enter number=");
    scanf("%d",&n);
 
    for(i=2;i<n;i++)
    {
        if(n%i==0)
        {
            count++;
            break;
        }
    }
    if (count!=0)
        printf("not");
    else
        printf("%d is a prime number",n);
}

 


Perfect way



#include<stdio.h>
 
int main(){
 
    int num,i,count=0,f;
    printf("Enter a number: ");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++)
    {
        if(num%i==0)
         count++;
    }
   if(count==0 && num!= 1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}

 


Minimum to maximum range



#include<stdio.h>
 
int main(){
 
    int num,i,count,min,max;
 
     printf("Enter min range: ");
     scanf("%d",&min);
 
    printf("Enter max range: ");
    scanf("%d",&max);
 
    for(num = min;num<=max;num++){
 
         count = 0;
 
         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
 
         if(count==0 && num!= 1)
             printf("%d ",num);
    }
 
   return 0;
}

 


Maximum range



#include<stdio.h>
nt main()
 
   int num,i,count,n;
   printf("Enter max range: ");
   scanf("%d",&n);
   for(num = 1;num<=n;num++)
       {
        count = 0;
        for(i=2;i<=num/2;i++)
        {
            if(num%i==0)
            {
                count++;
                break;
            }
       }
        if(count==0 && num!= 1)
            printf("%d ",num);
   }
  return 0;
 

No comments:

Post a Comment

php4

<?php    // Start the session  session_start();  ?>  <!DOCTYPE html>  <html>  <head>       <link rel=&q...