0

c++ numbers divisible by its own digits

I have to display numbers that are divisible by its own digits using loops (cant use functions) I understand the concept how to do it but dont know how to put it into a loop; The code i have right now shows the numbers that are divisible by its last digit only. Please help https://code.sololearn.com/cg2R26OPYK7Z

28th Oct 2019, 3:49 PM
A S
A S - avatar
18 odpowiedzi
+ 2
You need to give input for the range upper limit. I have tested the code up to 1000 and it works. FDBAD is the digit, if the digit is less than 2 (zero or one), or if the number % digit not equal zero then FDBAD will be set to zero, meaning the number is not fully divisible by the digit (unqualified), and therefore break the loop.
28th Oct 2019, 8:13 PM
Ipang
+ 1
here :) #include <iostream> #include <cmath> using namespace std; int main() { int n,d; for (n=0; n<100; n++){ int temp=n; d=temp%10; if(d!=0){ temp/=10; if(n%d==0) { cout<<n<<endl;} } } }
28th Oct 2019, 4:12 PM
A S
A S - avatar
+ 1
I need to confirm something. What if the number has more than one digit? should we check whether the number is fully divisible by each digit or what?. BTW, can you save that code in your profile and share the link instead. I think people will prefer to see the code attached with the question, within the Description section I mean ...
28th Oct 2019, 4:20 PM
Ipang
+ 1
@Ipang yes, it should be divisible by each digit. Okay , i will try to do that
28th Oct 2019, 4:25 PM
A S
A S - avatar
+ 1
Please don't mark my confirmation, it's a confirmation rather than an answer. Here's a guide to sharing links in case you didn't know yet. Mind you the link only works when using SoloLearn app, can't open it in browser. https://www.sololearn.com/post/74857/?ref=app
28th Oct 2019, 4:30 PM
Ipang
+ 1
yes, number with digit 0 is unqualified ( it is in my code already "if(d!=0)" ), 0 is not divisible
28th Oct 2019, 5:49 PM
A S
A S - avatar
28th Oct 2019, 6:39 PM
Ipang
+ 1
Is this any help...to access individual digits in a two digit number:- int mynum = 45; int a = mynum%10; // this get the 5 from the 45. int b = (mynum - mynum%10)/10; // this gets the 4 from the 45. //or for b int b = (mynum - a)/10; // this gets the 4 form the 45
28th Oct 2019, 9:26 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Had a go...came up with this: for(int i = 20; i < 100; i++){ if(i%10 == 0) continue; if(i%(i%10) == 0 && i%((i - i%10) / 10) == 0) cout << i << endl; }
28th Oct 2019, 10:46 PM
rodwynnejones
rodwynnejones - avatar
+ 1
@Ipang thank you
30th Oct 2019, 1:40 PM
A S
A S - avatar
0
(Whispers in despair) Share your code so people can help : )
28th Oct 2019, 4:05 PM
Ipang
0
A S One more thing, what about digit of zero? should we ignore it and continue checking other digits? or should a number having digit zero be assumed unqualified?
28th Oct 2019, 5:22 PM
Ipang
0
Off the top of my head...I would convert the number to a string, use substr to access the first and second 'digits' (as your example implied lower than 100) , convert them back (individually) to a number, use them in your modulo expression, and use a 'continue' statement if the converted sub-string is a '0' or a '1'. Just to add....shouldn't you for loop start at 12? ..because'...(as I understand it).... no point checking numbers under 10...and 11 has two '1's.
28th Oct 2019, 7:06 PM
rodwynnejones
rodwynnejones - avatar
0
@rodwynnejones good point about starting not from one digit numbers! (and yeah i found the ways to do it with strings online but i am in my first year of CS, we havent learnt strings yet so i shouldnt use it)
28th Oct 2019, 7:58 PM
A S
A S - avatar
0
@inpang code doesnt work, no output.. also can you please explain purpose of this line if(FDBAD < 2 || (i % FDBAD)) { FDBAD = 0;
28th Oct 2019, 8:04 PM
A S
A S - avatar
0
R uoy student
29th Oct 2019, 4:33 AM
Winzi A
Winzi A - avatar
0
Modify the logic as per requirement: cin>>num; int a=num; while(a!=0) { If(num%(a%10)!=0) exit(0); a/=10; } Cout<<"num is divisible by their own digits";
29th Oct 2019, 7:37 AM
Abhinav Koora
0
Hi
29th Oct 2019, 12:47 PM
Winzi A
Winzi A - avatar