While Loop
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++.
@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++; } }
pls do std::endl
@realTronsi
@Coder100 idk cpp meanie
xd i mean its just user preference @realTronsi
@Coder100 fixed
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
@Coder100 ok at edit your answer so it is the better version >:)
@realTronsi yes you do if you dont put mark it as standard library
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
@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
num.size()
iirc @realTronsi
yeah unfortunately i did a bad plz read the marked one @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.)
hey does it not work? @realTronsi
it stores all the integers, and if I took the time to study permutations, it might be more elegant. @realTronsi
@Coder100 yours work but it's a lazy solution lol
Something like:
3*2 * 8! * 7!
should suffice but it doesn't i don't think
@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
indeed plz helpz makez betterz @realTronsi
OH RIGHT i should probably skip a few numbers yes yes @realTronsi
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)
hmm true, but whats with the 9 anyways @realTronsi
@Coder100 idk its nodejs problem, try putting a console.log() after the while loops and the 9 disappears
@Coder100 ill convert it into cpp
c++ god @realTronsi
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
#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
hol' up why is there an extra <<
@realTronsi
@Coder100 thats an old version dont look at it
what lang?
alright! @WILLIAMMACDONAL
please read the question thanks @RYANTADIPARTHI
btw your example won't work endl isn't defined @RYANTADIPARTHI
actually read the question smh smh @RYANTADIPARTHI
So sorry about that, logic mistakes :(
If you need I can explain the code to you, but it should be pretty straightforward (I think)
@Coder100 Thank you very much.
np :) @WILLIAMMACDONAL