#I need help to understand how to do these problems with while loops

1912 messages · Page 2 of 2 (latest)

normal juniper
#

a

obtuse plover
#

You ruined my day

sleek halo
#
int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
while(attempts != 0) {
cin >> guess;
cout <<"You've run out of attempts.Bye!"<< endl;
attempts -= 1;
if(guess== ans) {
cout <<" Well Done!";
}
if(attempt==0) {
cout << ans;
break;
}
}
return 0;
}```
#

so like this?

sleek halo
obtuse plover
#

Check this out

sleek halo
#

what is std:: cout << riddle

obtuse plover
normal juniper
#

riddle is a string variable

obtuse plover
normal juniper
#

so it essentially means "output the riddle variable"

sleek halo
#

oh thats what std mean

obtuse plover
#

Standard

#

Means, it's from the standard library of C++

sleek halo
#

i see

#

also for guess i need to change the datatype

#

but its a number

soft rapids
sleek halo
#

being guess

#

so why is int not okay

normal juniper
#

std is just a namespace, aka a sort of library/collection of functions and other stuff

obtuse plover
#

Because what the user types in is a string, a string isn't an integer

#

You need to convert this string to an integer first

#

This can be done with std::stoi(str)

soft rapids
#

No no

obtuse plover
#

stoi = String To Integer

soft rapids
#

There is no need to convert any string to a integer in this program

#

And that is way too advanced too

#

For their stage

sleek halo
#

u can store a character in a number?

#

huh

obtuse plover
#

So, instead, we'll just change to "std::string ans = "196""?

soft rapids
#

This program just wants to check if the user input is equal to 196

#

Two numbers

#

That’s it

obtuse plover
#

We actually are working with one string and one integer

soft rapids
#

Where do u see string?

obtuse plover
#

Is there implicit type conversion (int -> string) when we compare string and int?

sleek halo
#

alright so i change the ans to a string instead of a int?

obtuse plover
#

Or am I being dumb

sleek halo
#

it gives numbers

soft rapids
#

Cin doesn’t give u anything?

sleek halo
#

the user is supose to give numbers

soft rapids
#

It just returns false or true if the value read is valid

#

There’s no need of strings anywhere

#

We are just working with integers

obtuse plover
#

What's the definition of 'valid' here?

#

I'm confused, please hang onto me for a second

soft rapids
#

If the data type read from the cin stream is equal to the data type being changed

#

Such as

#

Int value;

cin >> “string”;

#

This is off topic though

#

Just look up what cin returns

#

Anyways what step are u on again?

sleek halo
#

who me?

#

or the other person

#

uh i ma just assume u mean me

#
int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
while(attempts != 0) {
cin >> guess;
cout <<"You've run out of attempts.Bye!"<< endl;
attempts -= 1;
if(guess== ans) {
cout <<" Well Done!";
break;
}
}
return 0;
}```
#

so apparently i have to fix the attempts thing

obtuse plover
#

That's so cool, new knowledge

#

I never knew that

#

Thank you very much

sleek halo
#

alright so at expected it still writes it

#

what if i keep it out of the loop?

normal juniper
#

your code is going to produce the "run out of attempts" thing anyway

#

because you didn't add a condition around it

#

it's guaranteed to output it

sleek halo
#

i assumed the while( attempts !=0)

#

was the condition for that

normal juniper
#

@obtuse plover i'll get back to ur question later

sleek halo
#

oh wait

#

the attempts thing is after the cout statement

normal juniper
sleek halo
#

does that make a difference

normal juniper
#

so while( attempts !=0) is largely irrelevant

normal juniper
sleek halo
#

so i should put the attempts- before the cout statement

normal juniper
#

you're basically saying, "please input your answer", then before checking if the answer is correct, you're outputting the message that they've ran out of attempts

sleek halo
#

ohh

normal juniper
#

you don't need 2 while loops

#

that just complexifies the branching logic unnecessarily

#

even if it's just for a validation sanitisation

obtuse plover
#

How will you simplify it?

sleek halo
#

i might have to do that attempts thing with a if statement then 🤔

#

or i could modify the condition in the while?

#

maybe add something

#

liek a ||

normal juniper
#

also if (attempts <= 0) should be if (attempts == 0) instead

#

it'll never reach under 0

#

plus it's simpler to understand the condition

sleek halo
#

oh alright

#
int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
while(attempts == 0) {
cin >> guess;
cout <<"You've run out of attempts.Bye!"<< endl;
attempts -= 1;
if(guess== ans) {
cout <<" Well Done!";
break;
}
}
return 0;```
}
normal juniper
#

it's much more intuitive than while (!cin >> guess)

sleek halo
#

why would ==

#

be better

obtuse plover
#

I never knew I can return 0 or 1 inside a while loop to either force it to close or to work like a "continue" in C#

#

That's so great to know

sleek halo
#

nice we're all learning ^_^

#

oh right

#

so uh why would != not be good

normal juniper
#

yknow what wait i'll remake @obtuse plover's code

sleek halo
#

ye

obtuse plover
#

kernel, just to verify, when you return 1, it skips to the next loop without checking the conditions?

normal juniper
#
#include <iostream>

int main()
{
  const int ans = 196;
  int attempts = 4;
  bool win = false;
  std::string riddle = " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit. \n What am I? \n";
  std::cout << riddle;
  do
  {
    int guess;
    std::cout << "What's your guess?\n";
    
    cin >> guess;

    if (guess == "") 
    {
      std::cout << "Invalid input. Try again!\n";
    }

    if (guess == ans)
    {
      win = true;
      std::cout << "You won! Congrats!";
      return 0;
    }
    else
    {
      attempts -= 1;
    }
  } while ((win == false) && (attempts > 0));

  std::cout << "you've ran out of attempts, try again next time";
  return 0;
}
#

it should be something like this

obtuse plover
#

You missed the Run out of attempts message

normal juniper
#

also i didn't take into account the string input validation between with the int but i left that out for the purpose of explanation

normal juniper
obtuse plover
#

I'll get that message even if I win?

normal juniper
#

you won't

sleek halo
#

alright so bool works like a if statement right?

normal juniper
#

actually wait

sleek halo
#

expect u can just write false or true

#

instead of equations

#

to make it mean true or false

normal juniper
#
#include <iostream>

int main()
{
  const int ans = 196;
  int attempts = 4;
  bool win = false;
  std::string riddle = " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit. \n What am I? \n";
  std::cout << riddle;
  do
  {
    int guess;
    std::cout << "What's your guess?\n";
    
    cin >> guess;

    if (guess == "") 
    {
      std::cout << "Invalid input. Try again!\n";
      continue;
    }

    if (guess == ans)
    {
      std::cout << "You won! Congrats!";
      return 0;
    }
    else
    {
      attempts -= 1;
    }
  } while (attempts > 0);

  std::cout << "you've ran out of attempts, try again next time";
  return 0;
}
#

there

#

even better

#

this should be good now

obtuse plover
sleek halo
#

oh alright

normal juniper
normal juniper
obtuse plover
#

/tmp/sqvGJ78dxG.cpp:17:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
17 | if (guess == "")
| ~~^

normal juniper
#

the std::cin >> guess part?

#

oh yeah

sleek halo
#

hmm instresting the while statment is completly at the end

normal juniper
#

¯_(ツ)_/¯

sleek halo
#

i thought that needed to be set for so the code knows what number it can go to

obtuse plover
#

It's a do-while loop

#

You use it when you want to ensure the content inside the loop always runs at least once

#

It runs through the codes, then check if the condition is true, then it'll repeat the loop, else it continues with next lines

sleek halo
#

ah okay

#

can it be done with just a while loop first

#

or because if i use a while loop first, it causes more issues?

normal juniper
#

it can be done with a while loop or a do while loop

#

either is fine

#

personally i'd pick a do while loop for this situation

sleek halo
#

alright i'll put the if statements first

obtuse plover
#

BTW, return 0 and return 1 didn't work how I expected it to

#

It returns 0 and 1 to main(), not the loop, like my speculation

sleek halo
#

wait but if i wanted to do with the while loop, how would it have to go

normal juniper
#

it doesn't need to reach the loop

#

it's more of a shortcut in the logic branch

#

so it can directly quit the program

#

@sleek halo fyi, the code i showed doesn't work on purpose

#

it was a demonstration of the logical path

#

that it should take

sleek halo
#

so the logical path being starting with if statements first

normal juniper
#

there's 2 in the code

sleek halo
#

no im saying u started with if statements

#

and i started with while

normal juniper
#

ok

sleek halo
#

ur saying it was more logical to do if statements first?

normal juniper
#

in my opinion yes

sleek halo
#

but i'm pretty sure my prof wants me to do it with a while

#

well is say "A "while loop is required

#

so as long as one is there

#

its probably fine im assuming

#

u know what i'm a beginner anyway, i should do what's simplier

normal juniper
#

i mean, it's already simple enough

#

i can't make it any more simple

normal juniper
sleek halo
#

alright i don't know how bool works

#

never used it, my prof only did stuff with if statements so far

normal juniper
#

do you know what a type is in c++

sleek halo
#

yeah stuff like bool

normal juniper
#

mhm

sleek halo
#

char

#

int

#

double

#

there's also float but i think its less accurate than double

#

but double is used more

normal juniper
#

a bool is essentially just a true of false statement. you can do stuff like:

bool test = (1 + 10 == 11);

bool test2 = false;
#

expressions like ```cpp
if (10 == 10)

#

it's just a logical expression that's either true or false

#

you can also use in this way:

bool test = (10 == 10);
if (test) {
  std::cout << "expression is true";
} else {
  std::cout << "expression if false";
}```
#

hopefully that clears things up

#

and if statement can only take true or false

#

nothing else

sleek halo
#

and bool can do more than that

normal juniper
#

wdym

sleek halo
#

so u said if statemenrs can only take true or false

normal juniper
#

yeah pretty much

#

you can't do ```cpp
if ("something")

#

it MUST be a true or false statement

#

nothing more, nothing less

#

well technically this isn't true but for the sake of learning, think of it that way

sleek halo
#

Yeah alright

#

And bool can do more than that?

normal juniper
#

no

#

a bool is just true or false

sleek halo
#

bruh this internet

normal juniper
#

it can't do much

sleek halo
#

Oh alright

#

So u can do bool something

#

And later say if it’s true or false

#

Alright

normal juniper
#

can you give me an example of what you mean

sleek halo
#

Like bool test

#

And later

#

Make that false

#

Bool test = false

normal juniper
#

yep

sleek halo
#

Alright

#

Lemme write the code

normal juniper
#

ping me when ur done

sleek halo
#

Ye

obtuse plover
#
#include <iostream>
#include <string>

bool isPosNumber(std::string str)
{
    for (int i =0; i < str.length(); i++)
    {
        if (!(isdigit(str[i]))) { return false; }
    }
    return true;
}
int main()
{
  const int ans = 196;
  int attempts = 4;
  bool win = false;
  std::string riddle = " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit. \n What am I? \n";
  std::cout << riddle;
  do
  {
    int guess;
    do 
    {
        std::cout << "What's your guess?\n";
        std::string _strGuess = "";
        std::cin >> _strGuess;
        if (isPosNumber(_strGuess))
        {
            guess = std::stoi(_strGuess);
            break;
        } else
        {
            std::cout << "Invalid Input!\n";
        }
    } while (true);
    if (guess == ans)
    {
      win = true;
      std::cout << "You won! Congrats!";
    }
    else
    {
      attempts -= 1;
      std::cout << "That's not right. Try again. \n";
      if (attempts <= 0)
      {
        std::cout << "Sorry, you ran out of attempts.\n";
      }
    }
  } while ((win == false) && (attempts > 0));
}```
#

I finally found a version that works

sleek halo
#

oh right

#

@normal juniper

#
if (guess == "")```
#

also this part i dont get

#

its supose to be wrong but u just put qutoes

#

i'm gonna continue working on this tomorrow

#

for now i gotta go

soft rapids
#

And also how does
if (guess = “”) work? Isn’t that nothing in the quotations?

soft rapids
# sleek halo i'm gonna continue working on this tomorrow

This is the polished version of your code that u wrote earlier btw

int ans = 196;
int attempts = 4;
int guess;
cout << "I am a three-digit number. My tens digit is five more than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
// Loop until we run out of attempts
while(attempts != 0) {
  cin >> guess;
  attempts -= 1;
  // If guess is the answer, congratulate and break out of the loop
  if(guess == ans) {
    cout <<"Well Done!";
    break;
  }
  // If attempts is zero, print that you ran out of attempts
  // then the while loop will exit itself right afterwards because of the condition
  else if (attempts == 0) 
    cout <<"You've run out of attempts.Bye!"<< endl;
  // When the guess is wrong and there are still attempts left, then this else will run
  else
    cout << "Wrong answer, try again: ";
}
return 0;
}```
#

And here's version 2 for learning purpose of learning "continue" because it's the counterpart to "break" and people usually learn it together:

int ans = 196;
int attempts = 4;
int guess;
cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
cout << "What number am I? ";
// Loop until we run out of attempts
while(attempts != 0) {
  cin >> guess;
  attempts -= 1;
  // If guess is the answer, congratulate and break out of the loop
  if(guess == ans) {
    cout <<"Well Done!";
    break;
  }
  // If attempts is zero, print that you ran out of attempts
  // then the while loop will exit itself right afterwards because of the condition
  if (attempts == 0) {
    cout <<"You've run out of attempts.Bye!"<< endl;
    // continue is similar to break where you use it inside of a loop
    // and it makes the program jump back up to the loop immediately 
    // so that no code below the continue will run
    continue;
  }
  // This will print "wrong answer" every time when the two above if statements weren't executed
  // because both above if statements will either break out of the loop or jump back up to the        loop, so this code won't be reached unless both are false (AKA - isn't the right answer and      isn't out of attempts)
  // so, no need for an "else" 
  cout << "Wrong answer, try again: ";
}
return 0;
}```
#

Both of these versions work btw, i tested it. U can delete all the comments out if u want

normal juniper
normal juniper
normal juniper
normal juniper
#

it's arguably not required though

soft rapids
#

How can the user enter nothing tho, wouldn’t it just be a \n

normal juniper
soft rapids
#

So they just press enter and the \n gets ignored?

normal juniper
#

\n has nothing to do with it

#

the guess variable will be a null byte

soft rapids
#

Im just saying the \n since u have to press enter to use cin

#

But since it’s an integer then it’s just nothing in there 0x00

#

Is that what u mean?

normal juniper
#

this is how i'd do it

normal juniper
#

unless i'm misunderstanding something

soft rapids
#

Does that even work tho? I feel like I remember if I press enter without typing anything, then the program doesn’t do anything it just waits until I actually enter something

soft rapids
#

Oh

normal juniper
#

if nothing is inputed, the literal string for guess is just empty

#

and the whitespace is treated as null

soft rapids
#

And if it’s an integer then it won’t do anything until I enter something?

normal juniper
#

the sanitiation condition simply checks if it's empty

soft rapids
#

Yeah

soft rapids
#

I swear I could’ve remembered that if I have a cin >>
And then I just press enter, it doesn’t do anything until I actually type something else and then enter

normal juniper
#

that's not how cin works

soft rapids
#

I guess I remember it wrong

normal juniper
#

i just tested it

soft rapids
#

Oh

normal juniper
#

turns out i forgot how cin works

soft rapids
#

Where’d u get that info then? About the null

#

Oh okay lol

#

I guess it’s understandable since ur proficient and I’m still a beginner so i remember still lol

normal juniper
#

it's mostly an input validation technique i got from non-cin io stuff

#

i thought it would work the same way

#

but apparently not

soft rapids
#

Ohh I see

#

Makes sense

normal juniper
#

i'll edit the code btb

#

brb

#
#include <iostream>
#include <string>

using namespace std;

int main()
{
  const int ans = 196;
  int attempts = 4;
  bool win = false;
  std::string riddle = " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit. \n What am I? \n";
  std::cout << riddle;
  do
  {
    std::string guess = "";
    std::cout << "What's your guess?\n";
    
    std::cin >> guess;

    if (guess == std::to_string(ans))
    {
      std::cout << "You won! Congrats!\n";
      return 0;
    }
    else
    {
      std::cout << "Wrong answer, try again\n";
      attempts -= 1;
    }
  } while (attempts > 0);

  std::cout << "you've ran out of attempts, try again next time\n";
  return 0;
}
#

there

#

@sleek halo sry for another ping but this is how i'd do it fr this time

soft rapids
#

It’s really interesting how u do a return 0 inside the loop, never seen that before but then again I’ve never seen professional work

normal juniper
#

i'm nowhere near professional

soft rapids
#

Well.. I mean like code for an actual job or project

#

Instead of basic shit

normal juniper
#

loops are pretty much irrelevant to return, since it's directly responsible for returning a function's value

#

it has no effect to loops other than immediately breaking it and discarding the rest of the code within the function

soft rapids
#

I see

normal juniper
# soft rapids I see

it's sorta like instead of using a knife to cut open a watermelon and have clean slices in a procedural and repetitive cutting motion, you'd bring out a shotgun and blow the watermelon to pieces

#

that's how return is used in the code i showed

soft rapids
#

LMAO

normal juniper
#

it's probably a shit analogy but both methods work, it's just a difference of how direct it is at solving a problem

soft rapids
#

That’s a good comparison LOL

#

So do u see that method often in professional code?

#

Or it just depends

normal juniper
#

i'd say it's more on the professional end

#

cuz it minimises the branching and logical paths it has to take when the answer is already known

soft rapids
#

Yeah that makes sense

#

And it’s just unnecessary to get to the last main’s return ig

normal juniper
#

yep

#

that's exactly it

soft rapids
#

How are you 19 and already a systems programmer

normal juniper
soft rapids
#

Im 20 going on 21 this year and I barely know shit… mostly because I need to work harder lol

soft rapids
#

I just supplement with my animal skills lol

normal juniper
#

it's not something i necessarily brag about

#

it just shows i have no social life and spent half my life sticking my face on a computer

soft rapids
#

Your work ethic is something I envy though so be happy lol

normal juniper
soft rapids
#

Def

soft rapids
#

What drives you though? Do you just love the work that much or, you have a goal in mind like I do

normal juniper
#

i also figured it would open up a new field for me, so i put all my effort into learning how to code

#

and start making stuff i was intrigued about

soft rapids
normal juniper
#

i have a few failed projects tho

#

but it doesn't matter cuz i learned tons of new stuff anyway

soft rapids
#

Very true ^p

#

So are u able to get a programming job without a degree?

normal juniper
#

i'm confident i can get one even without a degree

soft rapids
#

Definitely

normal juniper
#

a lot of programming jobs allow employees without a degree anyway

#

even apple does it

#

it's mostly about individual ability rather than what you did at university for 3 years

#

that's why i never took a CS degree

soft rapids
#

And why I didn’t go for a degree either.. mostly because it’s so fucking expensive for so much time and unwanted classes

normal juniper
#

lol yeah

soft rapids
#

Though every time I look at jobs, just to see what’s out there, they all require a degree. I’m thinking I’m not looking in the right place. Where do u look? Linkedin?

normal juniper
#

i don't have a job

#

teehee

soft rapids
#

Ik but u know where to look don’t u?

normal juniper
#

not rly icl

soft rapids
#

Oh nvm then

normal juniper
soft rapids
normal juniper
#

a certificate is mostly what would be recommended though

#

especially in cybersec jobs

#

like cybersecurity in uni is absolute dogshit, it's even worse than how CS is taught

soft rapids
#

I actually just finished a certificate this month for C/C++ programming by UCSD - extended studies

#

I feel like I didn’t learn nearly enough as I should’ve tho

soft rapids
#

It was nothing literally

#

I need to work harder lol

normal juniper
#

oh

soft rapids
#

I mean it wasn’t nothing, it was definitely hard

#

But I just don’t have enough knowledge as I should

normal juniper
#

it's all input at the end

soft rapids
#

LOL I like that ^

#

It’s true

normal juniper
#

it's depressing

soft rapids
#

Hm, uni really seems like a scam nowadays

normal juniper
#

i like how this thread just ended up as a casual convo lol

soft rapids
#

And it’s interesting to know that it’s no different where you live as well

#

As it is in the US ^

soft rapids
normal juniper
#

but other than that, there's loads of value

soft rapids
#

Oh

#

You’re doing a degree for geopolitics now right

normal juniper
#

yeah

soft rapids
#

What for?

normal juniper
#

cuz i wanna work at NATO or the UN

soft rapids
#

LOL

normal juniper
#

it has fuckall to do with programming

soft rapids
#

I like where your ambition LOL

#

Oh really

normal juniper
#

ty

soft rapids
#

So what kind of work would u want to do for them?

normal juniper
#

not sure yet

#

i haven't even started any classes yet

soft rapids
#

Yeah

#

That’s really cool though

#

Interesting combination

soft rapids
#

Hopefully u won’t have issues learning… whatever it is you learn in geopolitics

#

Good luck with that lol

normal juniper
#

thanks lol

soft rapids
normal juniper
sleek halo
#
#include <iostream>
using namespace std;
int main () {

        int ans = 196;
        int attempts = 4;
        int guess;
        cout << " I am a three-digit number.My tens digit is five move than my ones digit,and my hundreds digit is eight less than my tens digit." << endl;
        cout << "What number am I? ";

        while(attempts != 0) {
        cin >> guess;
        attempts -= 1;

        if(guess == ans) {
        cout << "Well Done!";
        break;
        }
        else if(attempts == 0)
        cout <<"You've run out of attempts.Bye!"<< endl;

        else
        cout <<"Wrong answer, try again: ";
}
return 0;
}
~    ```
#

it seems so obvious when someone else writes out some of the code that thats how the problem should be solved

#

but still its bad that I couldn't get to this point on my own

#

I know I've only been learning coding for 4 weeks now, but I'm definetly screwed for my upcoming Exam if I'm like this man 🥲

#

so anyway it runs and stuff

#

So now I just need help with my 3rd question

#

so 4 digit number so I probably have to set a while to 10,000 at max im assuming

#

I'm not too sure if i should do a if statement

#

this is what i have so far

soft rapids
sleek halo
#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {
        x = x + 1
        if(x%10
sleek halo
#

even if i'm not understanding, at least the hw is done at least i supose but yeah i'm trying my best to understand

#

so currently I need to make the if statements work

soft rapids
#

Whelp looks like ur gonna have to do some math for this one I think

sleek halo
#

yup

soft rapids
#

I’ll get back to u in a bit gotta afk

sleek halo
#

so far i have the max number it should stop at 9999 and hopefully print all those numbers in between after then math

#

yeah sure

lean spindleBOT
#

@sleek halo Has your question been resolved? If so, run !solved :)

sleek halo
#

okay so this is what i have so far

#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {
        x = x + 1
        if(x%10 == 3 * x%10000 && x%10000 + x%1000 == x%10) {
        cout <<"x";```
#

when your back, lemme know what you think?

soft rapids
#

Don’t forget the space after the x though in the cout

#

Uh for the math idk how to do I’d have to think on it

#

But yeah it’s probably something like what you did with the % and getting digit’s places

soft rapids
#

@sleek halo u there?

sleek halo
#

Yeah sorry I was really tired today so I fell asleep just a moment ago but I’m up now

soft rapids
#

Oh rip

#

When is this due

sleek halo
#

On Monday night

#

After I finish this though, I gotta study for the quiz for this since I have that on Tuesday for it

soft rapids
#

Also I saw a lot of these numbers have the same difference of 110, so if u can’t figure out the equation maybe u could use that instead

soft rapids
sleek halo
#

It’s all the way from the beginning to while loops

#

And then the exam is a week after that

#

Whihc yeah problems like these and that one is up to four loops or maybe the nested loops of she does a lesson on that next class

soft rapids
#

Wtf an exam already

#

That sucks

sleek halo
#

Yeah everything is going to fast

soft rapids
#

The numbers in the sample

#

The answers

sleek halo
#

Oh yeah

#

I should have looked at the numbers more

#

But yeah I’ll probably have to include that in the math then

soft rapids
#

I think the equation might be easier

sleek halo
#

Yeah since it gives the same thing

soft rapids
#

Yesh

sleek halo
#

So ye maybe I don’t need to include the 110

#

Since the math will already do it

soft rapids
#

Yeah ur right

sleek halo
#

Alright so hopefully the program should run

#

But I might be missing a few things I’m not too sure

#

Oh right maybe the break statement

#

But I set a limit in the while

#

To 10000

#

So I’m hoping it would stop there

#

Well I guess I should run it first then

soft rapids
#

Yeah it should stop

sleek halo
#

Ah but it doesn’t print the numbers right

#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {

        if(x < 10000) {
        x = x + 1;
        if(x%10 == 3 * x%10000 && x%10000 + x%1000 == x%10) {
        cout << x;

                }
        }
}
return 0;
}```
#

do u know which is wrong then?

soft rapids
#

Tbh what u should do is comment out ur entire code, and just focus on making the equation and test it just once until u figure out the equation

soft rapids
#

But if ur too tired to focus just sleep

sleek halo
#

yeah I probably should

soft rapids
#

Cya later

sleek halo
#

yeah i'll try to figure it out tomorrow

#

ttyl

cursive dagger
#

1466 messages wth

normal juniper
sleek halo
#

hey guys

#
if(x < 10000) {
        x = x + 1;```
#

could it be because of this statement?

#

since i'm repeating what i said in the while condition in the if condition

#

nvm it just didn't run when i tried it

dark veldt
#

Crazy, what are you talking about with so long history tbh .

soft rapids
soft rapids
normal juniper
sleek halo
#

i changed the placement of the x = x+ 1

#

to under the while statement

#

and removed the first if

sleek halo
# normal juniper what’s the full code
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {

        if(x < 10000) {
        x = x + 1;
        if(x%10 == 3 * x%10000 && x%10000 + x%1000 == x%10) {
        cout << x;

                }
        }
}
return 0;
}```
dark veldt
soft rapids
#

Yeah

sleek halo
#

i'm not sure

#

The while says 1000 <10000

soft rapids
#

Also I think it didn’t run at all because of your equation

sleek halo
#

and it should add 1 each time

#

until it can print out what conditions i put in the second if statement

sleek halo
#

i dont get how

soft rapids
#

Also I don’t think u need the first if statement

sleek halo
#

yeah its just repeating what the while is saying right

#

lemme get rid of it again

dark veldt
#

Btw, do you know ... recursion?

soft rapids
#

For ur equation to even run, u need to surround stuff in parenthesis.
For example for this part u need:

x % 10 == (3 * (x % 10000)) && …

sleek halo
#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {

        if(x < 10000) {
        x = x + 1;
        if(x%10 == 3 * x%10000 && x%10000 + x%1000 == x%10) {
        cout << x;

                }
        }
}
return 0;
}#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) 
        x = x + 1;
        if((x%10) == (3 * (x%10000) && (x%10000) + (x%1000) == (x%100)) { // First is meant to say if the last didgit equals 3 TIMES the fourth digit and second part is meant to say if the first digit PLUS the second digit equals the third digit)
        cout << x;

                }
        }
}
return 0;
}```
sleek halo
#

also i think i just realized

#

x%10

soft rapids
#

I mean most beginners don’t know that until ur code doent run at all

sleek halo
#

gives the last digit

#

so for my second part of the condition

#

it should be x%100 to give me the thrid digit i think 🤔

#

Lemme put stuff in parentesis first then

soft rapids
#

Idk

sleek halo
#

ok i edited it

#

what about this

dark veldt
#

I'd like to share the recursion method for you, if you like.

soft rapids
#

Honestly what I would do is, open an empty project, and just do short simple tests to see how to find the digits u want

dark veldt
#
#include <cstdio> 
#include <vector> 

void check(int digits[], int n, int current, std::vector<int> &result) {
    // if first one is 0, no 4 digit numbers 
    if (digits[0] == 0) return ; 
    // first one is 1/3 of the last one 
    if (digits[0] * 3 != digits[n-1]) return ; 
    // sum of first two equals the thrid 
    if (digits[0] + digits[1] != digits[2]) return ; 
    // who is I ? 
    result.push_back(current); 
}

void search_result(int digits[], int n, int k, int current, std::vector<int> &result) {
    // nothing to enumerate 
    if (k == n) {
        check(digits, n, current, result); 
        return ; 
    }
    // enumerate all possible digits at position k 
    for (int i = 0; i < 10; i++) {
        digits[k] = i; 
        search_result(digits, n, k + 1, current * 10 + i, result); 
    } 
}

int main() {
    int digits[4];
    std::vector<int> result; 
    search_result(digits, 4, 0, 0, result); 
    for (auto i : result) {
        printf("%d ", i); 
    }
    puts(""); 
}
sleek halo
#

wow

#

it all looks like greek to me 🥲

#

what is void check

soft rapids
#

Yeah I don’t think midnight knows arrays or functions yet so he def won’t know recursion

sleek halo
#

i heard an array stores mutilple number but ye i haven't gotten to those yet

dark veldt
#

Enumerate all possible four digit numbers, then check it's acceptable or not.

sleek halo
#

so far the teachers have taught us up to for loops

dark veldt
#

So you even shouldn't use std::vector collection?

sleek halo
#

wait list every single one

#

what is vector collection

soft rapids
#

I found this code online, it might help u

int main()
{
int dig;
cout << "enter a four dig num " << endl;
cin >> dig ;
int git;
git = (dig % 10);
cout << "num in once place is: " << git << endl;
int one;
one = git;
git = (dig % 100);
cout << "num in tenth place: " << (git - one) / 10 << endl;
git = (dig % 1000);
cout << "num in hundredth:" << (git - one) / 100 << endl;
git = (dig % 10000);
cout << "num in thousandth: " << (git - one) / 1000 << endl;
return 0;
}

dark veldt
#

A dynamic list-like collection for you to collect your answers together, even when you can't know the total size of them.

#

I'd like to use it to collect all information then output my answer in a next separate step.

sleek halo
#

yeah sounds useful

#

but I've only gone up four loops in the lesson so my prof probably won't let me use anything farther than that

dark veldt
#

Ummm, I can give another easy solution only concern loop.

sleek halo
#

well the one i'm working on rn is just with while loops so i can't even use for loops yet for this

dark veldt
#

Can you use function?

sleek halo
#

so what is that

soft rapids
#

I don’t think he can

#

His instructions just say to use a while loop

#

All he needs to do right now is figure out one equation and he should be done

sleek halo
#

yeah this is what i've done do far

#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) 
        x = x + 1;
        if((x%10) == (3 * (x%10000) && (x%10000) + (x%1000) == (x%100)) { // First is meant to say if the last didgit equals 3 TIMES the fourth digit and second part is meant to say if the first digit PLUS the second digit equals the third digit)
        cout << x;

                }
        }
}
return 0;
}```
#

oh i copied it twice

#

what i'm not understanding is how what i wrote for the math wouldn;t print

#

10 is supose to grab the last digit

#

100, theird

#

1000, the second

#

and 10000, the first

#

so with that in mind, what could i be doing wrong

soft rapids
#

int main()
{
int n = 7569;
int th,h,t,u; // Thousands,hundreds,tens,units

u=n%10; 
t=(n/10)%10; 
h=(n/100)%10; 
th=n/1000;
#

Um let me look

sleek halo
#

so 7569%10 supose to give me 9

#

7569/10 gets rid of the 9, then grabs the next digit being 6

dark veldt
#

Ummm, the second demo I can give you like this:

sleek halo
#

7569/100 gets rid of both 69 and grabs 5

dark veldt
#
#include <cstdio> 
#include <cstdlib> 
#include <vector> 

int main() {
    std::vector<int> rst; 
    for (int current;;current += 1) {
        std::div_t output; 
        int first, second, third, fourth; 
        output = div(current, 10); 
        fourth = output.rem; 
        output = div(output.quot, 10); 
        third = output.rem; 
        output = div(output.quot, 10); 
        second = output.rem; 
        output = div(output.quot, 10); 
        first = output.rem; 
        // if current can't expressed in four digits, break. 
        if (output.quot != 0) break; 
        // if first is 0, it's not the four digits number. 
        if (first == 0) continue; 
        // first one is 1/3 of the last one 
        if (first * 3 != fourth) continue; 
        // sum of first two equals the third 
        if (first + second != third) continue; 
        rst.push_back(current); 
    }
    for (auto c: rst) {
        printf("%d ", c); 
    }
    puts(""); 
}
soft rapids
sleek halo
#

so are u saying i should get rid of the number after it prints???

soft rapids
#

Uh no

#

Just so you know, the “n” value was never changed in all of those conversions

#

Only used for it’s 7569

sleek halo
#

yeah

#

the x = x+ 1

#

is meant to change it

soft rapids
#

Oh yeah that’s fine

sleek halo
#

but its not doing that

#

at least when it run it

soft rapids
#

Because u need to do that conversion for every single number from 1,000 to whatever

sleek halo
#

actually u know lemme run what i hvae so far

soft rapids
#

Oooh I see

#

You’re missing some syntax after your while loop condition lol

sleek halo
#

btw what is syntax

#

i've seen that word being used but i still don't know what it means

soft rapids
#

Like curly brackets, semi colons, parenthesis

#

That are required in ur code otherwise it’ll give u a fatass error

dark veldt
#

Syntax is something make the compiler know what you're talking about.

soft rapids
#

Or it won’t work like how u intend it to

#

Like if u forgot curly brackets after an if statement, that if statement will only run 1 instruction instead of however many u wanted it to

soft rapids
#

Yeah so.. ur while loop is missing something important

sleek halo
#

so i need a {

#

after my while

soft rapids
#

Yep

sleek halo
#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000){
        x = x + 1;
        if((x%10) == (3 * (x%10000) && (x%10000) + (x%1000) == (x%100)) { // First is meant to say if the last didgit equals 3 TIMES the fourth digit and second part is meant to say if the first digit PLUS the second digit equals the third digit)
        cout << x;

                }
        }
}
return 0;
}```
#

oh my bad i stillneed to edit

soft rapids
sleek halo
#

before?

soft rapids
#

Uh I mean after the condition

#

The starting bracket

#

U have the end

sleek halo
#

oh alright

#

like this right?

soft rapids
#

Another reason why u rlly need a good IDE lol

#

Yeah

#

Also I think you have 1 too many curly brackets at the end

dark veldt
#

Yes, what's your dev environment?

sleek halo
#

alright this is how i hve it

#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {
        x = x + 1;
        if(x%10) == ((3 * (x%10000) && (x%10000) + (x%1000) == (x%100) {
        cout << x;
                }

        }

return 0;
}```
soft rapids
sleek halo
#

yeah just terminal

soft rapids
#

Ok that’s much better

dark veldt
#

Crazy ?!

sleek halo
#

ah and now the parenthesis is the issue

sleek halo
#

this is just what the teacher said to use only

#

at least on a mac, on windows, i think u gotta download something else

#

to code

soft rapids
dark veldt
#

Such unusual requirements.

soft rapids
#

Agreed

sleek halo
#

so since u need to manually learn how to code every single thing on ur own

#

ye i think the teacher mentioned something like that

dark veldt
sleek halo
#

ok temp variable

#

is the temp being declared

#

or is that something else

#
#include <iostream>
using namespace std;
int main () {```
#

u mean this part?

soft rapids
#

He doesn’t need to store the result though

sleek halo
#

this is just what the teacher said to write always

soft rapids
#

The only thing he’s printing is the x variable

sleek halo
#

the standard beginging

soft rapids
#

Unless he needs to use the result for something

#

Idek

dark veldt
#

No, I mean you can use more variables in the inner loop.

soft rapids
sleek halo
#

inside the while loop, i need more varaibles 🤔

#

but don't i need just one to work with the number

#

could u elaborate on what u mean

dark veldt
#

For example, to evaluate the fourth digit of your number, write int l = x % 10;

soft rapids
#

Oh I see what ur saying now

sleek halo
#

ohh

#

each digit

soft rapids
#

Yeah that would be more clean

sleek halo
#

has a varibale

#

right??

soft rapids
#

Exactly

#

And then u do the equation with the variables

dark veldt
#

Then less paren would be needed in your condition.

sleek halo
#

ok that could work

soft rapids
#

So it’s easier to read

dark veldt
#

And you won't mismatch your condition expression in this style.

soft rapids
#

And just name each variable the correct place it is

soft rapids
sleek halo
#

alright so instead of int x = 1000
i can do
int a =
int b =
int c =
int d =

#

i ogtta make it equal to the mod things

#

so int a = x%10

dark veldt
#

Are you sure? Then a would be 100.

sleek halo
#

1000%10 would make a the last digit no?

dark veldt
#

Returns 0, which is what you want.

sleek halo
sleek halo
dark veldt
#

I mean you need to repeat dividing then do modulo operations.

dark veldt
sleek halo
#

yeah

#

the statement at the end

dark veldt
#

I mean the expression 1000 % 10 would returns the 0 when evaluation.

sleek halo
#

oh thats right

#

so i need to make it first that 1000 changes

#

but isn't that done with the x++

#

could u maybe code what u mean then?

dark veldt
#

I just suggest you use local variables to simplify your expressions to avoid bugs.

sleek halo
#

if i just %

#

it would grab and print the zero

dark veldt
#

You shouldn't just do modulo but more things.

sleek halo
#

oh alright

dark veldt
#

int a = x % 10; int b = (x / 10) % 10; int c = (x / ...) ...;

#

Move the caocluating operations out of the if condition statements to make it clear.

sleek halo
#

right if i just made the varible each digit number, i would still be doing the same thing still

sleek halo
#

in the varaibles

#
#include <iostream>
using namespace std;
int main () {

        
        int x = 1000;
        int a = x%10;
        int b = (x / 10) % 10;
        int c = (x / 100) % 10;
        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {
        x = x + 1;
        if(x%10) == ((3 * (x%10000) && (x%10000) + (x%1000) == (x%100) {
        cout << x;
                }

        }

return 0;
}```
#

i might have understood wrong

dark veldt
#

Yes, x is changing in the loop scope.

#

a, b, c, wouldn't changing when x is changing.

#

C programming language is a process oriented language, to describe some variable assignment operations with step by step statement. It's not a language to describe some mathematical INVARIANT.

sleek halo
#

so i cant make a b c equal to the math?

dark veldt
#

After one assignment statement, it assigns at that time.

#

It won't keep forever, if you changes something in the future.

soft rapids
sleek halo
#

the int x= 1000

#

yeah

soft rapids
#

Yep

#

Now look inside ur while loop

#

It’s just using the x variable, but all the Variables you made aren’t getting updated inside your loop

sleek halo
#

there is a x = x+ 1
and the if statement with the math
and then x print statement

soft rapids
#

Yeah

#

It will work, but you want to use your variables to simplify the equation right

#

So in your equation, first thing is “x%10”

sleek halo
#

yea

soft rapids
#

Instead of that, it would be the variable “a”

#

Or whatever variable

sleek halo
#

right alright

#

so a = x%10

soft rapids
#

Variable unit

#

I’d Call them unit, tens, hundreds, thousands

#

Tbh

#

So , you need to move the code where u made ur variables to BEFORE if statement

#

And then once your variables are all set to their digits

#

Then do the math

#

Sorry I fucked up I edited it

#

Then do math inside if statement condition

sleek halo
#

after my while statement

soft rapids
#

if (tens == hundreds * 3 … ) {
cout << x;
}

sleek halo
#

i need to declare these varibles

soft rapids
#

Yeah

#

Yep

#

BC you want those digit variables to update every time x changes

#

Otherwise the digit variables will be set to 1,000 and never change, u don’t want that

sleek halo
#
#include <iostream>
using namespace std;
int main () {

        
        int x = 1000;
       
        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {
        x = x + 1;
        int unit = x%10;
        int tens = (x / 10) % 10;
        int hundreds = (x / 100) % 10;
        int thousands = (x / 1000);
        if(x%10) == ((3 * (x%10000) && (x%10000) + (x%1000) == (x%100) {
        cout << x;
                }

        }

return 0;
}```
soft rapids
#

Yes exactlyyy

#

Now all u have to do is make your equation more readable by using your variables

sleek halo
#

so each varibles grabs a different digit

soft rapids
#

Yep

sleek halo
#

so now for the if statement part

soft rapids
#

Also for the thousands place

#

You only need to divide by 1,000

#

Delete the % 10

sleek halo
#

how come

soft rapids
#

Idk just the math

sleek halo
#

but dont need the % to grab it

#

i guess since it touches the first digit

#

no?

soft rapids
#

If u divide the number by 1,000

#

You will always get that 1,000th digit

#

So if it’s 5,000 divided by 1,000

#

It’s 5

#

Etc

sleek halo
#

and if i % after

#

it woulnd't do anything

soft rapids
#

Idk u can just test it in another terminal and see what all the math does

sleek halo
#

yeah i'll do it after i fix the if statement

#

so now i just gotta make the conditions

#

do what i want

#

one sec

#
#include <iostream>
using namespace std;
int main () {

        
        int x = 1000;
       
        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {
        x = x + 1;
        int unit = x%10;
        int tens = (x / 10) % 10;
        int hundreds = (x / 100) % 10;
        int thousands = (x / 1000);
        if(thousands == 3 * unit && thousands + hundreds == tens) {
        cout << x;
                }

        }

return 0;
}```
#

something like this?

dark veldt
#

Match the parenthesis correctly..

#

thousands == 3 * unit is also wrong, pls reconsider the question description again.

sleek halo
#

wait why is that wrong

#

thouands

#

holds x/1000

#

which holds the first digit

dark veldt
#

thousands is the first digit, unit is the last.

sleek halo
#

i saying if the first digit equal to 3 * last digit

dark veldt
#

What it means my first digit is one-third of my last digit.

sleek halo
#

but thats what i want

sleek halo
dark veldt
#

Maybe the correct statement is thousands * 3 == unit.

sleek halo
#

first digit times 3

acoustic pulsar
#

Am I on trees or what?

sleek halo
#

= last digit

acoustic pulsar
#

1 min ago?

dark veldt
sleek halo
#

damn almost 2000 messages 💀

steel moat
sleek halo
dark veldt
#

Are you sure?

sleek halo
#

so if the firgit * 3, it would make the first digit three times more than the last right?

sleek halo
# dark veldt Are you sure?

no cause well i'm always wrong but i'm not understanding how having 3 times the first digit would make it one third of the last digit

dark veldt
#

Test latex math support $x = \frac13 y$

dark veldt
#

What would happen when you want to accurate get one-third of some int value like 4.

sleek halo
#

But then it’s not supose to print

#

Only supose to print numbers that meet those conditions

dark veldt
#

I just tell you it's impossible to get one-third of any int.

#

So we play a trick on this to avoid problem.

#

Do both integer multiplication on the both sides to avoid the decimal problem.

sleek halo
#

So u want me to muilply 3 with thousands and unit???

dark veldt
#

I just tell you the solution to avoid decimal problem.

sleek halo
#

Alright I think I may be misunderstanding

#

What I’m confused about is

#

The decimal problem

dark veldt
#

If you do integer division, like 4 / 3, you would just get 1 but not 1.33... in C type system.

sleek halo
#

Why would there be a decimal be a problem if I declared the variables with int and not double

dark veldt
#

Because you now need a division, 'one-third' operation.

soft rapids
#

It SHould be fine though right, he doesn’t need to get the decimal value right

#

Rounding should get answers

dark veldt
#

In math, integer division is not complete.

sleek halo
#

But only integer numbers are being printed out anyway

dark veldt
#

So pls use multiplication to avoid this problem, but not do the stupid floating division.

dark veldt
#

We're talking about the trouble predicate, with one-third condition.

sleek halo
#

Ok so if we want the program to be accurate like u said

#

In that case shouldn’tI just declare the variables as double

#

Instead of int

dark veldt
#

wdym

#

You prefer double x;?

sleek halo
#

Since u said it wouldn’t print the correct division

#

No

dark veldt
#

I mean it wouldn't evaluate the correct division.

sleek halo
#

Because everything in the code is using integers

soft rapids
#

What happens when u run ur program rn?

sleek halo
#

But I’m trying to understand what u are saying

sleek halo
soft rapids
#

Wait also add a space after the cout

sleek halo
#

Gimme me five minutes

soft rapids
#

And the. Run it

#

And then

dark veldt
#

I must notify you that it's a bad practice, when you code, you just check the output to trust your program rather than prove it in math.

soft rapids
#

cout << x << “ “;

sleek halo
#

is it only gonna printt once?

soft rapids
#

Did u run it?

dark veldt
#

loop repeats the print operations......

sleek halo
sleek halo
soft rapids
#

It prints out the number of x

#

And then prints out a space

sleek halo
#

in the x varible it prints out the number anyway

#

ohh

dark veldt
#

Nearly equivalent to

cout << x; 
cout << " "; 
sleek halo
#

the quotes are for space

soft rapids
#

So instead of 123
It prints
1 2 3

sleek halo
#

ohh alrigtht good to know

#

that means i need to do that so it does space

#

alright this is how it runs

dark veldt
#

But it's wrong obviously.

sleek halo
#

i need to put endl after what number am i

dark veldt
#

Reconsider your condition evaluation.

sleek halo
#

the output seems to stop before 4000

#

and yeah it is

#

well i mean it follows the condition

#

first two numbers

#

add up to the third

dark veldt
#

Are you sure?

sleek halo
#

last number is 1/3 of the first

#

it apears to me that the numbers do fit these conditions

#

its just that i didn't realize the output stopedbefore 4 thousand

#

what i don't understand is why because its supose to guess all the 4 digit numbers

sleek halo
dark veldt
dark veldt
#

first digit is one-third of last digit

#

It means the condition like thousands == 1 / 3 * unit.

sleek halo
#

hwo could that condition be wrong if the question is asking for that condition 😭

#

ok nvm just proceed

dark veldt
#

I have already told you the key..

sleek halo
#

ur telling me about decimals

#

from what i understand

#

u said if a number like 4/3

dark veldt
sleek halo
#

the output would just print the integer

dark veldt
dark veldt
#

But it's dangerous to describe a condition with division operations with int type.

sleek halo
#

first digit = 1/3 last digit

dark veldt
#

Then I suggest you to play a trick on it, with a better and equivalent condition.

sleek halo
#

so it would be 3(first digit) = 1/3 *1(last digit)

dark veldt
#

first digit = 1/3 last digit

#

then, 3 * first digit = 3 * 1/3 last digit

#

then 3 * first digit = last digit.

#

The equation implied.

#

So the condition you should use is like that 3 * thounsands == unit.

#

Don't you agree?

sleek halo
#

3 * 3(first digit) = 3 * 1/3 (9)(lastdigit) ??

#

3 * fristdigit = lastdigit

#

is what ur saying?

#

but wouldn't that make the first digit 3 times more than the last

#

hold on lemme read my code

#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?";

        while(x < 10000) {
        x = x + 1;
        int unit = x%10;
        int tens = (x / 10) % 10;
        int hundreds = (x / 100) % 10;
        int thousands = (x / 1000);
        if(thousands == 3 * unit && thousands + hundreds == tens) {
        cout << x << " ";
                }

        }

return 0;
}```
#

this is what i have

#

but ur saying

#

thousands * 3 == unit

dark veldt
#

......

#

My first digit is one-third of my last digit.

dark veldt
sleek halo
#

so what i have rn is saying my first digit is equal to 3 times my last digit

sleek halo
#

could be 9

#

right

#

and what im hoping is

#

the last digit should be a third one of 9

#

3

dark veldt
#

So you think it's correct, when first is 9 and last is 3?

sleek halo
#

yea

dark veldt
#

My first digit(9) is one-third of my last digit(3).

#

Are you sure?

sleek halo
#

i had it reverse ohh

#

i see it now

dark veldt
#

Fix it by yourself.

sleek halo
#

ok i think since i saw one third near the last digit, in my brain i kept thinking the last digit should be more

#

okay

#
#include <iostream>
using namespace std;
int main () {


        int x = 1000;

        cout << "I am a four-digit number.My first digit is one-third of my last digit,and the sum of my first two digits is equal to my thrid digit.What number am I?" << endl;

        while(x < 10000) {
        x = x + 1;
        int unit = x%10;
        int tens = (x / 10) % 10;
        int hundreds = (x / 100) % 10;
        int thousands = (x / 1000);
        if(thousands * 3 == unit && thousands + hundreds == tens) {
        cout << x << " ";
                }

        }

return 0;
}```
#

this time it stoped before 4000

#

okay but what was the other thing u were meanioning?

#

about the deciamls?

sleek halo
#

were u refering to the same thing?

#

i thought u were talking about doing math with decimals so I was confused

#

but yeah since it runs now and my questions are done, I'll go now

lean spindleBOT
#

Did you really just try to ping 28,656 people?

sleek halo
#

...

#

oops

#

i thought it would just ping the people here uhh

#

but ye thanks everyone for the help

#

!solved

lean spindleBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

sleek halo
# dark veldt Fix it by yourself.

also thank you, you made me see a mistake i had after saying it multiple times but i didn't understand so i'm sorry about that, I'm still not sure what u meant by the decimals but thank u

burnt charm
dark veldt
normal juniper
#

dang it’s finally over

#

after neary 2k messages

burnt charm
soft rapids
#

I was kinda hoping we would reach the 2k mark

#

Maybe slowly one day

quartz mesa
#

2k messages for repeatedly asking for input