Program is not giving desired output (PRINT SERIES)

include
using namespace std;

int ans(int x,int y){
int v;

for(int u=1;u<x;u++){
    v=3u+2;
    if(v%y!=0){
        cout<<v<<endl;
    }

}

return 0;

}
int main() {
int n1;
int n2;
cin>>n1;
cout<<endl;
cin>>n2;

ans(n1,n2);

return 0;

}

hello @vanshit02

u r iterating only n1 time.
so in this n1 terms some of them will be divisible by n2.
so overall ur code will print less than n1 terms.

we never know after how many iteration we will find n1 terms of the series that are not divisible n2.
thats why use while loop .

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.