Skip to content
← Back to Community
While Loop
Profile icon
WILLIAMMACDONAL

An ascending integer occurs when each digit is greater than any digit that precedes it. An example is
478. Write a program that finds the number of ascending integers between 200 and 300.

I know how to do basic while loops but cannot figure out how to separate a number and determine if it is an ascending integer.

C++.

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
AnthonyKeogh1
Profile icon
WILLIAMMACDONAL
Comments
hotnewtop
Profile icon
Coder100

So sorry about that, logic mistakes :(

#include <iostream> #include <vector> int main() { unsigned int hundred = 2, ten = 0, one = 0; std::vector<int> nums; while (hundred < 3) { while (ten < 10) { while (one < 10) { if (hundred < ten && ten < one) { nums.push_back(100 * hundred + 10 * ten + one); } one ++; } one = 0; ten++; } ten = 0; hundred ++; } for (int i : nums) { std::cout << i << " "; } std::cout << std::endl; return 0; }

If you need I can explain the code to you, but it should be pretty straightforward (I think)

Profile icon
WILLIAMMACDONAL

@Coder100 Thank you very much.

Profile icon
Coder100
Profile icon
realTronsi

@Coder100 here is better solution

#include <iostream> int d1 = 2; int d2 = d1 + 1; int main(){ while(d2<10) { int d3 = d2 + 1; while (d3<10) { int number = d1*100+d2*10+d3; std::cout<<number; std::endl; d3++; } d2++; } }
Profile icon
Coder100

pls do std::endl @realTronsi

Profile icon
realTronsi

@Coder100 idk cpp meanie

Profile icon
Coder100

xd i mean its just user preference @realTronsi

Profile icon
realTronsi

@Coder100 fixed

Profile icon
Coder100

but the thing is std::endl is better because some people use \r\n so you could have just gotten garbled text jk jk most os now do that convert automatically @realTronsi

Profile icon
realTronsi

@Coder100 ok at edit your answer so it is the better version >:)

Profile icon
Coder100
Profile icon
Summit

@Coder100 its not rlly user preference cus realTronsi didnt use use namespace std so it wouldnt work if he/she didnt use std::endl

Profile icon
Summit

@realTronsi yes you do if you dont put mark it as standard library

Profile icon
Coder100

After thinking about this problem, I got to this solution.

#include <iostream> #include <vector> int main() { unsigned int hundred = 2, ten = 0, one = 0; std::vector<int> nums; while (hundred < 3) { while (ten < 10) { while (one < 10) { one ++; if (hundred < ten && ten < one) { nums.push_back(100 * hundred + 10 * ten + one); } } one = 0; ten++; } ten = 0; hundred ++; } for (int i : nums) { std::cout << i << " "; } std::cout << std::endl; return 0; }

thank god for 0!

Hmm there's an extraneous 300 in there, how strnage

Profile icon
realTronsi

@Coder100 he asked for number of, not the exact numbers (and your answer also doesn't work) but literally just 6+5+4+3+2+1 (21) is your answer

basic permutation as such:

23_ ,4-9: 6 possibilities
24_ ,5-9: 5 possibilities,

etc.

Your script gave these numbers:

234 235 236 237 238 239 240 245 246 247 248 249 250 256 257 258 259 260 267 268 269 270 278 279 280 289 290 300

some such as 280 aren't ascending, and you also missed a few

Profile icon
Coder100

num.size() iirc @realTronsi

Profile icon
Coder100

yeah unfortunately i did a bad plz read the marked one @realTronsi

Profile icon
realTronsi

@Coder100 ok good. Just that your answer is questionable.

//I'm not gonnna write actual code since I don't know much cpp but this is the actual logic: digit2 = digit1 + 1 while digit2 < 10 digit3 = digit2 + 1 while digit3 < 10 cout << digit1*100 + digit2*10 + digit3 digit3++ digit2++

(not actual code since idk much cpp but there is no point in looping over every single number.)

Profile icon
Coder100

hey does it not work? @realTronsi

Profile icon
Coder100

it stores all the integers, and if I took the time to study permutations, it might be more elegant. @realTronsi

Profile icon
realTronsi

@Coder100 yours work but it's a lazy solution lol

Profile icon
Coder100

Something like:
3*2 * 8! * 7!
should suffice but it doesn't i don't think
@realTronsi

Profile icon
realTronsi

@Coder100 did you see my code answer though? If you put that in cpp syntax it should work, I could put it in js syntax to prove a point

Profile icon
Coder100

indeed plz helpz makez betterz @realTronsi

Profile icon
Coder100

OH RIGHT i should probably skip a few numbers yes yes @realTronsi

Profile icon
realTronsi

@Coder100

let d1 = 2 let d2 = d1+1 while(d2<10){ let d3 = d2+1; while(d3<10){ console.log(d1*100+d2*10+d3) d3++ } d2++ }

This extremely short chunk of code in js works, (just an extra 9 at the end that can easily be solved)

Profile icon
Coder100

hmm true, but whats with the 9 anyways @realTronsi

Profile icon
realTronsi

@Coder100 idk its nodejs problem, try putting a console.log() after the while loops and the 9 disappears

Profile icon
realTronsi

@Coder100 ill convert it into cpp

Profile icon
Coder100
Profile icon
realTronsi

@Coder100

int d1 = 2; int d2 = d1 + 1; do { int d3 = d3 + 1; do { std::cout<<d1*100+d2*10+d3<< d3++ } while (d3<10); d2++ } while(d2<10);

there might be syntax errors since im not the best cpp

Profile icon
realTronsi

@Coder100

#include <iostream> int d1 = 2; int d2 = d1 + 1; while(d2<10) { int d3 = d3 + 1; while (d3<10) { std::cout<<d1*100+d2*10+d3<< d3++; } d2++; }

theres an error unqualifed-id idk how to fix but anyways theres code

Profile icon
Coder100

hol' up why is there an extra << @realTronsi

Profile icon
realTronsi

@Coder100 thats an old version dont look at it

Profile icon
Coder100

what lang?

Profile icon
WILLIAMMACDONAL
Profile icon
Coder100
Profile icon
Coder100

please read the question thanks @RYANTADIPARTHI

Profile icon
Coder100

btw your example won't work endl isn't defined @RYANTADIPARTHI

Profile icon
Coder100

actually read the question smh smh @RYANTADIPARTHI