**New course!**Every coder should learn

**Generative AI!**

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

18 Answers

+ 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.

+ 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;}
}
}
}

+ 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 ...

+ 1

@Ipang yes, it should be divisible by each digit.
Okay , i will try to do that

+ 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

+ 1

yes, number with digit 0 is unqualified ( it is in my code already "if(d!=0)" ), 0 is not divisible

+ 1

This is what I have in mind
https://code.sololearn.com/c5g3VM2R2kld/?ref=app

+ 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

+ 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;
}

+ 1

@Ipang thank you

0

(Whispers in despair) Share your code so people can help : )

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?

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.

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)

0

@inpang
code doesnt work, no output..
also can you please explain purpose of this line
if(FDBAD < 2 || (i % FDBAD))
{
FDBAD = 0;

0

R uoy student

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";

0

Hi