Function

Thread Starter

aspirea5745

Joined Apr 17, 2019
99
How can I get the reverse number by the function. If the user enters a number like 987, then I should get its reverse 789. function should be return reverse number
C:
#include<stdio.h>

int reverse(int number);

int reverse(int number)
{
   
    ......
   
    return returnNumber ;
}

int main()
{
    int Number;
   
    int ReverseNumber;
   
    printf ("Enter number \n");
    scanf ("%d", &Number);
   
    ReverseNumber = reverse(int Number);
   
    printf("\n reverse Number %d",ReverseNumber);
   
return 0;
}
 

MrChips

Joined Oct 2, 2009
30,821
Are you sure you do not mean the "inverse function"?

For example the atan( ) function is the inverse of the tan( ) function.
 

Thread Starter

aspirea5745

Joined Apr 17, 2019
99
Are you sure you do not mean the "inverse function"?

For example the atan( ) function is the inverse of the tan( ) function.
No, Its like something
If the user enters a number like 987, then I should get its reverse 789
If the user enters a number like 654, then I should get its reverse 456
If the user enters a number like 987, then I should get its reverse 789
If the user enters a number like 741, then I should get its reverse 147
 

Papabravo

Joined Feb 24, 2006
21,225
You convert the number to a string, and then you reverse the string. Or.
You could also use the modulus operator along with integer division.
Example:
quotient = 987 / 10 --> 98
remainder = 987 % 10 --> 7
calculate the next quotient remainder pair, until quotient is == 0
 

MrChips

Joined Oct 2, 2009
30,821
1) Find the length of the string = N
2) Using string index that start with 0 index
move source to destination[j]

where j = 0 to N - 1 and i = N - 1 - i
 

WBahn

Joined Mar 31, 2012
30,071
The point of an assignment like this is to get you to think about how you can get at the information you need that is embedded within something else. You will get the most out of it if YOU do the bulk of the reasoning and figuring out, instead of just having someone else tell you how to do it.

So think about what mathematical operations you could perform to get the different pieces of a number.

x = 987;

How could you get the units digit into y0, the ones digit into y2, and the hundreds digit into y2, such that

y0 = 7;
y1 = 8;
y2 = 9;

What could you replace the constants with so that they are functions of x?

If you have y0, y1, and y2, how could you combine these to get a result for z that is the reverse value such that

z = 789;

Again, replace the constant with an expression using the y variables.

Do you see the pattern that would allow you to apply it to an arbitrary value of x (whether x represents a 1 digit number or a 7 digit number or whatever)?
 
Top