Can someone tell me how can I fix this program? It\'s supposed to tell the user
ID: 3564607 • Letter: C
Question
Can someone tell me how can I fix this program?It's supposed to tell the user if the number he/she entered is polydivisible or not.
#include <iostream> #include <cmath> using namespace std; int main () { int a; int i=10, j=0, x=1;
cout << "Please enter a number "; cin >> a;
do { j++; a= a/10; }
while (a>0);
{ i= pow(10, j-1);
}
while (a%i==0)
{
while (a%j==0) {
j--; a=a/10; i= pow(10, j-2);
} }
cout << "The number is polydivisible" << endl; }
cout << "The number is not polydivisible" << endl; system ("pause"); return 0; }
Can someone tell me how can I fix this program?
It's supposed to tell the user if the number he/she entered is polydivisible or not.
#include <iostream> #include <cmath> using namespace std; int main () { int a; int i=10, j=0, x=1;
cout << "Please enter a number "; cin >> a;
do { j++; a= a/10; }
while (a>0);
{ i= pow(10, j-1);
}
while (a%i==0)
{
while (a%j==0) {
j--; a=a/10; i= pow(10, j-2);
} }
cout << "The number is polydivisible" << endl; }
cout << "The number is not polydivisible" << endl; system ("pause"); return 0; }
It's supposed to tell the user if the number he/she entered is polydivisible or not.
#include <iostream> #include <cmath> using namespace std; int main () { int a; int i=10, j=0, x=1;
cout << "Please enter a number "; cin >> a;
do { j++; a= a/10; }
while (a>0);
{ i= pow(10, j-1);
}
while (a%i==0)
{
while (a%j==0) {
j--; a=a/10; i= pow(10, j-2);
} }
cout << "The number is polydivisible" << endl; }
cout << "The number is not polydivisible" << endl; system ("pause"); return 0; } #include <iostream> #include <cmath> using namespace std; int main () { int a; int i=10, j=0, x=1;
cout << "Please enter a number "; cin >> a;
do { j++; a= a/10; }
while (a>0);
{ i= pow(10, j-1);
}
while (a%i==0)
{
while (a%j==0) {
j--; a=a/10; i= pow(10, j-2);
} }
cout << "The number is polydivisible" << endl; }
cout << "The number is not polydivisible" << endl; system ("pause"); return 0; }
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
int main (){
int a;
int j=0;
cout << "Please enter a number ";
cin >> a;
int temp=a;
do{ //find the number of digits in number, store in j
j++;
temp= temp/10;
}while (temp>0);
int x=2;
bool isPoly=true;
while(x<=j){ //check for each number formed by taking first n digits, if its divisible by n
double i;
i= pow(10.0, j-x);
temp=(int)a/i;
if(temp%x != 0){ //if not divisible, no is not polydivisible
isPoly=false;
break;
}
x++;
}
if(isPoly)
cout << "The number is polydivisible" << endl;
else
cout << "The number is not polydivisible" << endl;
system ("pause");
return 0;
}