Thursday, November 27, 2014

c program to display all prime numbers in a given array

/*.................................................................................................................................................
File : rec14.c
program : Find the prime numbers in a given array
Author : sujithbaby440@gmail.com
Date :15-11-14
...................................................................................................................................................*/
#include<stdio.h>
int IsPrime(int N);
main()
{
int N,i,x[30],temp,num;
system("clear");
printf("The program to display all prime numbers in a given array\n\n");
printf("Enter the limit of the array\n");
scanf("%d",&N);
printf("Enter values in to array\n");
for(i=1;i<=N;i++){
scanf("%d",&x[i]);
}
printf("The prime numbers are:\n");
for(i=1;i<=N;i++){
temp=IsPrime(x[i]);
if(temp==1){
printf("%d\n",x[i]);
}
}
}
int IsPrime(int N)
{
int r,i,temp1;
i=2;
temp1=0;
while(i<N){
r=N%i;
if(r==0){
return 0;
}
i++;
}
return 1;
}


OUTPUT 1
The program to display all prime numbers in a given array
Enter the limit of the array
6
Enter values in to array
1 5 7 11 8 2
The prime numbers are:
1
5
7
11
2
[141740@localhost ~]$

OUTPUT 2
The program to display all prime numbers in a given array
Enter the limit of the array
5
Enter values in to array
7 9 6 1 2
The prime numbers are:
7
1
2
[141740@localhost ~]$

No comments:

Post a Comment