Ask your own question, for FREE!
Mathematics 15 Online
OpenStudy (calculusxy):

Write a program that asks the user to enter a three-digit number, then prints the number with its digits reversed. Do the previous problem with a approach that makes use of the division and modulus operators.

OpenStudy (calculusxy):

@jango_IN_DTOWN

OpenStudy (jango_in_dtown):

okay let me post the program

OpenStudy (jango_in_dtown):

#include<stdio.h> int main() { int n, rev= 0,rem; printf("Enter a number \n"); scanf("%d",&n); while (n != 0) { rev=rev* 10; rem=n%10; rev = rev+rem; n= n/10; } printf("Reverse of entered number is = %d\n", rev); return 0; }

OpenStudy (calculusxy):

this is what i did with two-digit number

OpenStudy (jango_in_dtown):

it will be same.

OpenStudy (calculusxy):

OpenStudy (calculusxy):

The thing is I didn't use the while loop

OpenStudy (calculusxy):

Can I do something similar? If not, then can you explain to me why you chose the variables that you chose and how you used them? (Preferably with example)

OpenStudy (jango_in_dtown):

yeah your program will work for 2 digits only

OpenStudy (jango_in_dtown):

if you want to do the way you did for the 3 digit problem, then i will post the solution. however my method is for any general number.

OpenStudy (calculusxy):

OK. Can you explain your method?

OpenStudy (jango_in_dtown):

you may write the program and check it is working or not

OpenStudy (calculusxy):

Well I believe that your one works because I have seen something similar to it on another website. So I just want to have it explained what each variable stands & why you did certain functions =?

OpenStudy (jango_in_dtown):

okay let me tell you how i did

OpenStudy (calculusxy):

OK

OpenStudy (jango_in_dtown):

do you have pen and paper beside you, it will be very helpful

OpenStudy (calculusxy):

yes i do

OpenStudy (jango_in_dtown):

ok.. rem stands for remainder. when you write rem=n%10, it gives the remainder when the number is divided by 10. i,e, you will get the last digit of n

OpenStudy (calculusxy):

yes

OpenStudy (jango_in_dtown):

and rev will be the variable where we want to store the reversed value of n

OpenStudy (jango_in_dtown):

lets do a dry run. ok??

OpenStudy (calculusxy):

ok

OpenStudy (jango_in_dtown):

take n=123. come to the while loop.. obviously n is not equal to 0, so the loop will run now initially rev was 0 now rev=rev*10=0*10=0 rem=n%10=123%10=3 rev=rev+rem=0+3=0 n=n/10=123/10=12

OpenStudy (calculusxy):

What do you mean by this: now initially rev was 0 now rev=rev*10=0*10=0

OpenStudy (jango_in_dtown):

we declared rev as 0... when we write rev=rev*10, we have rev=0*10=0

OpenStudy (calculusxy):

oh yes I see now

OpenStudy (calculusxy):

can you give me a moment while i just process this?

OpenStudy (jango_in_dtown):

yeah you dry run it

OpenStudy (jango_in_dtown):

in the 6th line rev will be 3 , i wrote 0 it is wrong

OpenStudy (jango_in_dtown):

0+3=3

OpenStudy (calculusxy):

Why did you do rev=0?

OpenStudy (calculusxy):

And then have rev+rem?

OpenStudy (jango_in_dtown):

see what you need to do, is bring that 3 at first place, right, for that you need to multiply 3by 100, 2 by 10 and 1 by 1 and just add this thing.. then you get 300+20+1=321. thats we are doing in this while loop

OpenStudy (calculusxy):

OK

OpenStudy (jango_in_dtown):

if you need solution by using the method you use, let me post that program. I guees you will understand it better

OpenStudy (calculusxy):

ok

OpenStudy (jango_in_dtown):

void main(){ int n,rev,fd,md,ld; printf("Enter a three digit number\n"); scanf("%d",&n); fd=n%10; n=n/10; md=n%10; n=n/10; ld=n; rev=fd*100+md*10+ld; printf("\n The reverse of the given number is %d",rev); }

OpenStudy (calculusxy):

OK I am going to dry run it

OpenStudy (jango_in_dtown):

yeah, its the method you used

OpenStudy (calculusxy):

So let's say i inputted 123 fd = 123%10 = 3 n = 123/10 = 12 md = 123%10 = 3 (does this make sense?)

OpenStudy (jango_in_dtown):

no you missed something.. n is 12 so md=12%10=2

OpenStudy (calculusxy):

But if someone enters 123 for n during the line with the scanf, then won't it detect that n=123 or will it just read off with the n=123/10=2?

OpenStudy (jango_in_dtown):

when you write n=n/10, n vale will not contain the same value as it had previosly

OpenStudy (calculusxy):

that makes sense now

OpenStudy (welshfella):

what language is this C?

OpenStudy (calculusxy):

Yes it's C

OpenStudy (jango_in_dtown):

let me do a dry run take n=123 then fd=123%10=3 n=n/10=123/10=12 md=n%10=12%10=2 n=n/10=12/10=1 ld=n=1 rev=fd*100+md*10+ld=3*100+2*10+1=321 bingo

OpenStudy (calculusxy):

WOW that is amazing!

OpenStudy (jango_in_dtown):

its the method you used in case of 2 digits.

OpenStudy (calculusxy):

Ok gotcha

OpenStudy (calculusxy):

Thanks tremendously for ur help.. during what times r u available in OS because i take a CS1 class and i usually need help with the hw involving writing c programs

OpenStudy (jango_in_dtown):

If I am online , I will definitely try to help you. :).

OpenStudy (jango_in_dtown):

I dont have any fixed time. SO cant tell before hand

OpenStudy (calculusxy):

Thanks so much :) U are amazing at C!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!