LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   reversing a number (https://www.linuxquestions.org/questions/linux-software-2/reversing-a-number-116298/)

narendra_i 11-15-2003 08:37 AM

reversing a number
 
how to take a number from input
and reverse it


int sum=0,t=1;
char ch;
scanf("%c",&ch);
while(ch!='.')
{
sum=sum+(ch-'0')*t;
t=t*10;
scanf("%c",&ch);
}
printf("%d",sum);


the abvove code works with a '\n' (or) '.'
can i write a funtion tom reverse it with out using . or \n;



waiting for u r reply.......

TheOther1 11-15-2003 09:25 AM

Why not do this in Perl? :twocents:

#!/usr/bin/perl -w
use strict;
my $digit;
print "Enter number:";
$digit = <STDIN>;
chomp $digit;
print "Number = $digit\n";
$digit = (reverse $digit);
print "Reverse Number = $digit\n";

If want to cut this script down even more:

#!/usr/bin/perl -w
print "Enter number:";
$digit = <STDIN>;
chomp $digit;
$digit = (reverse $digit);
print "Reverse Number = $digit\n";


All times are GMT -5. The time now is 05:53 AM.