what it is wrong this codes , this program can`t execute more than 5 number why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

20th May 2021, 12:58 PM
alireza mohammadian
alireza mohammadian - avatar
2 Answers
+ 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().
20th May 2021, 1:25 PM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Thanks a lot, your answer is very helpful. I find my mistake.
20th May 2021, 2:06 PM
alireza mohammadian
alireza mohammadian - avatar