Hello! Could You please help to solve this? My question is how to reverse a number in decimal notation using recursive function.
1234 => 4321
2 => 2
20 => 0
20000 => 2 (yes!)
0 => 0
The maximum length of test input is 5 numbers. No need to check for negative inputs or too large numbers. It should be used the division with 10, sprintf() and atoi() functions.
Thanks for your help!

Recommended Answers

All 5 Replies

#include<stdio.h>
#include<conio.h>

void main(){

	long n;
	char ch;
	clrscr();
	do{
	   clrscr();
	   printf("\nEnter a number : ");
	   scanf("%ld",&n);
	   long p,sum =0 ;
	   int temp=0;
	   p=n;
	   while(1){
		temp= p%10;
		sum = sum*10+temp;
		p= p/10;
		if(p==0) break;
	   }
	   printf("\nAfter rererse number %ld is %ld \n",n, sum);
	   printf("\nDo you want to continue ?(Y/N) ");
	   ch=getche();
	}while(ch=='Y'||ch=='y');

}

good day ....

Thanks a lot! If you could make some changes that you do not need to enter the numbers cause there is already another file main.o which has data and all the tests for this programm. And sorry, I forgot to add that the function should be int reverse(int i). Thanks in advance!!!!

dragonbone,

Don't post source code until opening poster has put some effort. Use int main().

I use int main() - Depending on the exit code of the executable the initating application can determine if the invocation was a successful one or a failure. Its good when someone wants to wrap a higher level API around your executables.

Take a look at,

1. http://www.cprogramming.com/faq/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376
2. http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3

there is full code , I separated reverse() function for you ....
good luck...

#include<stdio.h>
#include<conio.h>

long reverse(long n){

	long p,sum =0 ;
	   int temp=0;
	   p=n;
	   while(1){
		temp= p%10;
		sum = sum*10+temp;
		p= p/10;
		if(p==0) break;
	   }
	return sum;
}
void main(){

	long n;
	char ch;
	clrscr();
	do{
	   clrscr();
	   printf("\nEnter a number : ");
	   scanf("%ld",&n);
	   long sum=reverse(n);
	   printf("\nAfter reserver number %ld is %ld \n",n, sum);
	   printf("\nDo you want to continue ?(Y/N) ");
	   ch=getche();
	}while(ch=='Y'||ch=='y');

}
commented: It's not a proper way. -3

Thanks a lot!! It works for me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.