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.
@jango_IN_DTOWN
okay let me post the program
#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; }
this is what i did with two-digit number
it will be same.
The thing is I didn't use the while loop
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)
yeah your program will work for 2 digits only
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.
OK. Can you explain your method?
you may write the program and check it is working or not
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 =?
okay let me tell you how i did
OK
do you have pen and paper beside you, it will be very helpful
yes i do
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
yes
and rev will be the variable where we want to store the reversed value of n
lets do a dry run. ok??
ok
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
What do you mean by this: now initially rev was 0 now rev=rev*10=0*10=0
we declared rev as 0... when we write rev=rev*10, we have rev=0*10=0
oh yes I see now
can you give me a moment while i just process this?
yeah you dry run it
in the 6th line rev will be 3 , i wrote 0 it is wrong
0+3=3
Why did you do rev=0?
And then have rev+rem?
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
OK
if you need solution by using the method you use, let me post that program. I guees you will understand it better
ok
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); }
OK I am going to dry run it
yeah, its the method you used
So let's say i inputted 123 fd = 123%10 = 3 n = 123/10 = 12 md = 123%10 = 3 (does this make sense?)
no you missed something.. n is 12 so md=12%10=2
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?
when you write n=n/10, n vale will not contain the same value as it had previosly
that makes sense now
what language is this C?
Yes it's C
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
WOW that is amazing!
its the method you used in case of 2 digits.
Ok gotcha
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
If I am online , I will definitely try to help you. :).
I dont have any fixed time. SO cant tell before hand
Thanks so much :) U are amazing at C!
Join our real-time social learning platform and learn together with your friends!