Computer Science, asked by kunalthakral3917, 7 months ago

Need to design a program wherein when the human enters an integer, the computer should return an integer in the reversed manner. Write a program to calculate the reverse of the given number.  Note: Take care of the leading zeros (e.g. 5200 gives 25). It means reversed number never has any trailing zeros.

Answers

Answered by meowwww75
3
Hey !


Logic to follow :

1) - Check for trailing zeros

------ If yes, consider printing from first non zero digit from right.

----- If no, proceed with next step.



2) - Loop -1 to no. of digits

====> To get the units digit from no. , use modulus Operation of 10 each time and take quotient as the number, make note of no. of digits too and loop until the number of digits in the given number.

- store the modulus (remainder) result to a variable and result

- consider the quotient as the number now.

- to count the number of digits, take a variable and increment until quotient is not zero. Initialise to 0 at first.



eg : 4567

No. = 4567

let count be c;

At first , c = 0

4567%10 = 7 (remainder) 456 (quotient) ; No. = 456 ; c =1

456 % 10 = 6 ( remainder) 45 (quo.) ; No. = 45; c = 2

45 % 10 = 5 (remainder) 4 (quo) ; No. = 4; c = 3

4 % 10 = 4(rem) 0(quo). ; !!!!!!!!!!!!!!! STOP !!!!!!!!!!!!!!!!!


3) - Loop -2 to execute the code

4567 has to be 7654 , which means we have to multiply remaiders with 10^c with c decreasing each time of loop, and result has to be added to the existing number from the second time.



eg : 4567

No. = 4567

c = 3, existing no. = 0

4567%10 = 7 (remainder) ; No. = 7 ×10^{3} = 7000 +0 = 7000

456 % 10 = 6 ( remainder) ; No. = 6×10² = 600 + 7000 = 7600

45 % 10 = 5 (remainder) ; No. = 5 ×10 = 50 +7600 = 7650

4 % 10 = 4(rem); No. = 4 + 7650 = 7654

This is how 4567 --------------------> 7654

Hope you understood the logic



meowwww xD
Answered by priyankeshbadgujar21
10

Answer:

#include <iostream>

#include<math.h>

using namespace std;

int main()  

{

int n,rev=0,rem;

  cin>>n;

  while(n!=0)

   {

    rem = n % 10;

      rev = rev * 10 + rem;

      n = n / 10;

   }

  cout<<rev<<endl;

return 0;

}

Explanation:

Similar questions