+ 3

What is the logic in making a any digit number reverse

Like "12345" becomes "54321" how to make such program i dont understand the logic im a beginner in programming (c)

5th Sep 2020, 5:33 PM
Sreenesh
Sreenesh - avatar
23 Respostas
+ 10
Easiest one is: int x = 123; int rev = 0, lastDigit; do { lastDigit = x % 10; rev = (rev Ɨ 10) + lastDigit; x = x / 10; } while(x!=0); print(rev); Logic: We are taking out last digit (by taking its remainder after dividing it by 10), Eg: 123%10 = 3 And putting it in another variable (rev = (revƗ10) + lastDigit ), And we're eliminating last digit from original number (x), by dividing it by 10 Eg: 123/10 = 12 We're multiplying "rev" by 10 so we can use another last digit, First time: rev=0, lastDigit=3 & x=12 rev = (0Ɨ10) + 3 = 0+3 = 3 Second time: rev=3, lastDigit=2 & x=1 rev = (3Ɨ10) + 2 = 30+2= 32 Third time: rev=32, lastDigit=1 & x=0 rev = (32Ɨ10) + 1 = 320+1= 321 As x=0 loop will terminate We've got REVERSE: rev = 321
6th Sep 2020, 7:41 AM
Vatsal Singh
Vatsal Singh - avatar
+ 4
#include <iostream> using namespace std; int main() { int x; int rev = 0, lastDigit; cout << "Enter any numeric value: "; cin >> x; do { lastDigit = x % 10; rev = (rev * 10) + lastDigit; x = x / 10; } while(x!=0); cout << "Reverse of the number is: " << rev; }
6th Sep 2020, 7:51 AM
Vatsal Singh
Vatsal Singh - avatar
+ 3
Evan Flair He has seen the code already but doesn't understand. He was to know what's happening in the loop so add a line or two comments in it. I remember being in that same state years back.
5th Sep 2020, 6:19 PM
code0rdie
code0rdie - avatar
+ 3
Step 1 Convert number into string Step 2 Reverse it Step 3 Convert back into number
6th Sep 2020, 2:58 AM
Sanjay Kamath
Sanjay Kamath - avatar
6th Sep 2020, 7:52 AM
Vatsal Singh
Vatsal Singh - avatar
+ 2
You got my question URL OPERATOR im exactly in the same situation
5th Sep 2020, 6:20 PM
Sreenesh
Sreenesh - avatar
+ 2
Ok I'll try it but you see, programmers create a program based on thoughts, not by copying other's logics so i wanted to be creative and check if there's any other way of writing this code
5th Sep 2020, 6:22 PM
Sreenesh
Sreenesh - avatar
+ 2
the % operator will save the digits one at a time starting from behind and in each iteration you the / will remove the last digit from num after saving on reverse. until all digits in num finish.
5th Sep 2020, 6:23 PM
code0rdie
code0rdie - avatar
+ 2
Ok that's the explanation i was waiting for from here thanks for ur time guys!
5th Sep 2020, 6:25 PM
Sreenesh
Sreenesh - avatar
+ 2
This is very simple and as time goes on you will come to realize that this is just a joke,.
5th Sep 2020, 6:29 PM
code0rdie
code0rdie - avatar
+ 2
Bro in first program why condition is n!=0? And why operation are done using 10?
5th Sep 2020, 6:32 PM
Sreenesh
Sreenesh - avatar
+ 2
in first one rev*10+ num%10 will save last digit in rev and then num/10 will remove last digit. when all digits are removed num becomes zero and loop must stop hence while num! =0
5th Sep 2020, 6:35 PM
code0rdie
code0rdie - avatar
+ 2
yeah very well, cause we never got someone to explain something like this to us and thats how we force our selves to understand people code. Even in the universities no one will explain such details
5th Sep 2020, 6:40 PM
code0rdie
code0rdie - avatar
+ 1
Sreenesh You can attach the code so that we explain to you. There different ways to reverse a number.
5th Sep 2020, 6:12 PM
code0rdie
code0rdie - avatar
+ 1
#include <stdio.h> int reverse(int num) { int reversed = 0; // Lets reverse the number while (num != 0) { reversed = reversed * 10 + num % 10; num = num / 10; } return reversed; } int main() { int number; printf("Enter a number to reverse \n\n"); scanf("%d", &number); printf("%d", reverse(number)); return 0; } I dont understand the logic here that modula operator and dividing it
5th Sep 2020, 6:16 PM
Sreenesh
Sreenesh - avatar
+ 1
Yeah but the first is better, faster and more efficient
5th Sep 2020, 6:27 PM
code0rdie
code0rdie - avatar
+ 1
Damn u guys are pros :O I just want to be like u, I'm working hard to be creative and good programmer
5th Sep 2020, 6:27 PM
Sreenesh
Sreenesh - avatar
+ 1
And by the way, in loop why condition should not be equal to zero?
5th Sep 2020, 6:28 PM
Sreenesh
Sreenesh - avatar
+ 1
And why we should multiply,divide,modula only with 10?
5th Sep 2020, 6:29 PM
Sreenesh
Sreenesh - avatar
+ 1
this one he has used a string rather than the int variable and reversed the string instead
5th Sep 2020, 6:30 PM
code0rdie
code0rdie - avatar