+ 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)
32 Réponses
+ 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
+ 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;
}
+ 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.
+ 3
Step 1 Convert number into string
Step 2 Reverse it
Step 3 Convert back into number
+ 2
You got my question URL OPERATOR im exactly in the same situation
+ 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
+ 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.
+ 2
Ok that's the explanation i was waiting for from here thanks for ur time guys!
+ 2
This is very simple and as time goes on you will come to realize that this is just a joke,.
+ 2
Bro in first program why condition is n!=0? And why operation are done using 10?
+ 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
+ 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
+ 1
Sreenesh You can attach the code so that we explain to you. There different ways to reverse a number.
+ 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
+ 1
Yeah but the first is better, faster and more efficient
+ 1
Damn u guys are pros :O
I just want to be like u, I'm working hard to be creative and good programmer
+ 1
And by the way, in loop why condition should not be equal to zero?
+ 1
And why we should multiply,divide,modula only with 10?
+ 1
this one he has used a string rather than the int variable and reversed the string instead