#help with problem

44 messages · Page 1 of 1 (latest)

brittle spoke
#

could someone help me with this program i dont really know how i am supposed to do it.

reverse.
For example, if the input is 19, you should calculate 19 + 91, then print 110.
For instance, if the input is 123, you should calculate 123 + 321, then print 444.
Input and output
Input will contain a single line with a natural numbers n.
Output should contain a single line with a number indicating the value of n added to the digitreverse of n.```
this is the instructions and i am supposed to reverse an interger and then add them toghether.
grim hingeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

brittle spoke
#

int main()
{
  int n;
  while(scanf("%d", &n)==1)
  {
    for (size_t i = 0; i < count; i++)
    {
      /* code */
    }
    
  }
}``` this is all i have written so far but i am not sure what to put in the for loop to reverse the int.
jade acorn
brittle spoke
#

oh okay is it easier to use a for loop or a while loop?

jade acorn
#

they are identical:

#

;compile -Wall -Wextra -Werror -Wpedantic -g -fsanitize=address,undefined

#include <stdio.h>
#include <stdlib.h>

int main() {
  for (int i = 0; i < 3; i++) {
    printf("%d\n", i);
  }

  int i = 0;
  while (i < 3) {
    printf("%d\n", i);
    i++;
  }

  return EXIT_SUCCESS;
}
hazy perchBOT
#
Program Output
0
1
2
0
1
2
jade acorn
#

Notice how a for-loop is just a way to compactly put the initialization, condition, and step into a single line

brittle spoke
#

oh okay, but i dont really know how to reverse it still do i do somthing like this? for (int i = 0; i < n/2; i++)

prisma goblet
#

why n/2 ?

#

why n at all

jade acorn
brittle spoke
#

yes okay

jade acorn
#

Are you absolutely, 100% sure that the exercise will be giving your program the input like echo 42 | ./program, and not ./program 42?
Cause the exercise's description isn't clear to me

brittle spoke
#

what do you mean?

jade acorn
#

My guess is that they indeed mean that you need to read it from stdin, but maybe you're supposed to read it from argv

brittle spoke
#

if i input 19 it is supposed to reverse it to 91 and take 91 + 19 = 110

jade acorn
#

There are two ways to pass the number to your program

brittle spoke
#
  {
    reversing = reversing * 10 + i % 10;
  }``` why does this work?
jade acorn
#

Try to mentally walk through it yourself

#

Where did you get this code? chatgpt/a friend?

brittle spoke
#

i just serched how to reverse an int

#

okay so if the input is 19

#

it sets i = 19

#

is checks if 19 is not equal to 0 witch it is not

#

then is says that 19 = 19 / 10

#

which becomes 1.9

#

but then i dont understand

#

!solved

grim hingeBOT
#

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

jade acorn
brittle spoke
#

yes i think so

jade acorn
#

I recommend still trying to solve it by reversing the input string, rather than interpreting it as an integer immediately, since it's quite a bit simpler

brittle spoke
#

yes okay will do!

languid grotto
#

for example 123 modulo 10 = 3

brittle spoke
#

ohh thank you that makes sense!

languid grotto
#

so they take the last digit and add it to the reversed number but as the most significant digit(msd)

#

when they add to it so for example your last didit is 1
then reverse becomes 1

#

then they multiply by 10, that basically means they shift all digits to the left, so reverse becomes 10

#

then they add the next digit lets say 5, then reversed becomes 15

#

etc etc

#

Quick tip: multiplying by the base of the numeral system shifts all digits to the left, dividing shifts to the right.