0
what it is wrong this codes , this program can`t execute more than 5 number why?
//in c++ finding prime number #include<iostream> using namespace std; int prime(int a) { int i=2,counter=0; while(i<=a/2) { if(a%i==0) counter++; i++; } return counter; } int main() { int i,x[i],c=0; int n; cout<<"Enter the number of numbers:\n"; cin>>n; const int constVar = n; for( i = 0 ; i<constVar ; i++){ cout<<"enter number:\n"<<"["<<i<<"] = "<<'\n'; cin>>x[i]; if( (prime(x[i])==0) && x[i]!=1 ) c++; } cout<<c; return 0; } what it is wrong this codes , this program can`t execute more than 5 number why?
2 RĂ©ponses
+ 1
i before the for loop is garbage number because you didn't initialize i before the for loop, so x[i] is indeterminate.
In fact, you don't need array. Just declare a variable for the input in the for loop and pass it in prime().
0
Thanks a lot, your answer is very helpful.
I find my mistake.