c++ - Runtime error occurs while accessing elements of vector container -
in below mentioned program converting integer vector, number checked divisibility each element of vector. however, while accessing vector elements check divisibility of elements number, runtime error occurs.
#include <iostream> #include <algorithm> #include<vector> using namespace std; int main(){ int testcase; cin>>testcase; //number of testcases inputted while(testcase--) { int number,i=0,count=0; cin >>number; //number inputted int p=number; vector<int> arr(100); while(number){ //converting integer array arr[i]=number%10; number=number/10; cout<<arr[i]; if(p%arr[i]==0) //checking whether each element of array divisible number //this gives runtime error count++; i++; } cout<<endl<<count<<endl; //printing count of elemnts of array divisible number } return 0; }
i have commented out if condition giving runtime error(without if condition, program runs correctly). can please tell me how can corrected? in advance.
in condition
if(p%arr[i]==0)
arr[i]
can equal 0
.
thus if number contains 0 program fail.
Comments
Post a Comment