Monday 17 September 2012

search prime number


Definition of prime number:A prime number is one,which is divisible only by one or itself. And its must greater than 1.
And if number is not prime, it is called composite number and vice versa.

Ans.

#include<stdio.h>
#include<stdio.h>
int main()
{
 int x,num;
 printf("Enter number : ");
 scanf("%d",&num);
 x=2;
 while(x<=num-1)
 {
   if(num%x==0)
   {
      printf("Number is not prime!!");
      break;
   }
   x++;
 }
 if(x==num)
    printf("Number is prime!!");
 getch();
 return 0;
}

    output of above program :

Enter number : 7
Number is prime!!

RELATED PROGRAMS
case1:
Generate first n Prime number
/*c program for generate first n prime number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,num,t,div,count;
 printf("How many prime number you want to print: ");
 scanf("%d", &n);
 printf("\n%d\t",2); /*2 is first prime number*/
 count=1;
 num=3;
 while(count<n)
 {
  t=sqrt(num);
  div=2;
  while(div<=t)
  {
    if(num%div==0)
       break;
    div++;
  }
  if(div>t)
  {
     printf("%d\t",num);
     count++;
  }
  num=num+2;
 }
 getch();
 return 0;
}
case2:
Print prime number 1 to 100
Q. Write a C program to accept a specific last number and print all the less then or equal prime number.
or
Q. Write a C program to print the prime number between 1 to n number.
for example: 1 to 10
Note:-
1. 1 is not the prime number.
2. All even number is not the prime number except the number 2.

/*C program to print 1 to 100 prime numbers*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,n,div,p;
 printf("Enter any number: ");
 scanf("%d", &num);
 for(n=2; n<=num; n++)
 {
  for(div=2; div<n; div++)
  {
   if(n%div==0)
   {
     p=0;
     break;
   }
   p=1;
  }
  if(p)
    printf("\t%d",n);
 }
 getch();
 return 0;
}

No comments:

Post a Comment