hey friends, here is a code that I made to find the prime numbers in an array.. What the program is supposed to do is check whether the number is PRIME or not.. The position of the element is specified and the program finds and check the element in that position.. Unfortunately it is not working properly.. no error while compiling yet there are some other logic errors I guess.. please help me sort it out

I tried to debug.. But it didn't help .

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[10],n,pos,i;
clrscr();
cout<<"Enter the no of elements in the array \t";
cin>>n;
cout<<"Enter the elements of the array \t";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the postion of the number to be checked\t";
cin>>pos;
for(i=2;i<=a[pos+1]/2;i++)
if (a[pos+1]%i==0)
{
cout<<pos<<"th term is not a prime number\t";
getch();
exit(0);
}
cout<<pos<<"th term is a prime number\t";
getch(); return;
}

I added a watch on "g" which is the variable which contains the value of a[pos +1] .The value of g is not changing.. It is some random value like 1336...
the problem is with assigning the value of a[pos+1] to the variable "g"..

:-/

pos+1 ??
should it be pos-1?

For say if array is int arr= {5,4,8,7}
and you are checking at position 4 i.e 7,
then you should do arr[3] and no arr[5] since
arr[3]=7
also, arr[5] is not equal to 7 but it contains some junk values!!

so just change pos+1 to pos-1 and then chekc

My god..! You are a genius..! Why didn't I think of that..! phew..! Thank you so much dear friend.! I cant thank you enough..! The problem is eventually solved..!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.