-2
#include<bits/stdc++.h>
using namespace std; 

int main() {


int n,m,z;
   cout<<"enter n: ";
   cin>>n;
     z=n;

    int count=0;
while(n>0){
    m = n % 10;
    if(z%m == 0){
        count++;
    }
    n=n/10;
}
cout<<count;

}

Code should work like that ex - for n = 12, it is divisible by both 1 , 2 so, the output will be 2 if i am taking any value which have '0' in their last then it is not working ..and i am getting an error "Floating-point exception (SIGFPE)". Could anyone help me to get rid out of this.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

1 Answers1

0

This while loop

while(n>0){
    m = n % 10;
    if(z%m == 0){
        count++;
    }
    n=n/10;
}

does not make a great sense. For example m can be equal to 0 after this statement

    m = n % 10;

and as a result this statement

    if(z%m == 0){

produces a run-time error.

The program can look for example the following way

#include <iostream>

int main()
{
    unsigned int count = 0;

    int n;

    std::cout << "enter n: ";

    if ( std::cin >> n )
    {
        const int Base = 10;

        int tmp = n;
    
        do
        {
            int digit = tmp % Base;
            if ( digit != 0 && n % digit == 0 ) ++count;
        } while ( tmp /= Base ); 
    }

    std::cout << "count = " << count << '\n';
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335