# reverse six digit number input from the keyboard

Page 1 of 1

## 9 Replies - 25295 Views - Last Post: 23 January 2011 - 06:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=64902&amp;s=94f78dfb255807cdaeed8206a8d8dd43&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 amita.vij

Reputation: 1
• Posts: 6
• Joined: 24-September 08

# reverse six digit number input from the keyboard

Posted 24 September 2008 - 01:14 AM

Hi friends,

Iam facing in writing one program.

Question: f a six digit number is input through the keyboard i.e. accept from the user. Display a number in reverse order.

I have written this program. But this program is only reversing 2 digit, 3 digit, 4 digit and 5 digit number but not 6 digit number...so plss check my program and do let me know my mistakes.

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

void main()
{
int num,rev_num;
int b,c,d,e,f,g;
int b1,c1,d1,e1,f1,g1;
clrscr();

printf("Enter six digit number ");
scanf("%d",&num);

b=num-((num/10)*10);
b1=b;

c=num/10-((num/100)*10);
c1=(b1*10)+c;

d=num/100-((num/1000)*10);
d1=(c1*10)+d;

e=num/1000-((num/10000)*10);
e1=(d1*10)+e;

f=num/10000-((num/100000)*10);
f1=(e1*10)+f;

g=num/100000-((num/1000000)*10);
g1=(f1*10)+g;

rev_num=g1;

printf("\n Reverse number is %d",rev_num);

getch();

}

```

** Edit **

Is This A Good Question/Topic? 1

## Replies To: reverse six digit number input from the keyboard

### #2 David W

• DIC supporter

Reputation: 281
• Posts: 1,791
• Joined: 20-September 08

## Re: reverse six digit number input from the keyboard

Posted 24 September 2008 - 01:23 AM

amita.vij, on 24 Sep, 2008 - 01:14 AM, said:

Hi friends,

Iam facing in writing one program.

Question: f a six digit number is input through the keyboard i.e. accept from the user. Display a number in reverse order.

I have written this program. But this program is only reversing 2 digit, 3 digit, 4 digit and 5 digit number but not 6 digit number...so plss check my program and do let me know my mistakes.

...

Why don't you consider a loop structure, and let the computer work for you?

Take a look at the a solution to you problem posted just here a few posts before this ...

### #3 amita.vij

Reputation: 1
• Posts: 6
• Joined: 24-September 08

## Re: reverse six digit number input from the keyboard

Posted 24 September 2008 - 01:32 AM

David W, on 24 Sep, 2008 - 01:23 AM, said:

amita.vij, on 24 Sep, 2008 - 01:14 AM, said:

Hi friends,

Iam facing in writing one program.

Question: f a six digit number is input through the keyboard i.e. accept from the user. Display a number in reverse order.

I have written this program. But this program is only reversing 2 digit, 3 digit, 4 digit and 5 digit number but not 6 digit number...so plss check my program and do let me know my mistakes.

...

Why don't you consider a loop structure, and let the computer work for you?

Take a look at the a solution to you problem posted just here a few posts before this ...

Hi,

where i should look.BTW for me it will be more comfortable if any one tells the mistakes in this program...so plsss tell me why this program is reversing a 6 digit number

Hi,

where i should look.BTW for me it will be more comfortable if any one tells the mistakes in this program...so plsss tell me why this program is reversing a 6 digit number

### #4 David W

• DIC supporter

Reputation: 281
• Posts: 1,791
• Joined: 20-September 08

## Re: reverse six digit number input from the keyboard

Posted 24 September 2008 - 02:08 AM

Quote

... where i should look ... reversing a 6 digit number...

You really need to learn to look a little ... and to use Google too. Here is a copy/paste just to get you started ...

```// This would make a good test question ...maybe ?
// Write a function to reverse the digits of a number ...
// and test it out ...

#include <iostream>

// prototype for reverseDigits
int reverse (int number);

int main()
{
using std::cin;
using std::cout;
using std::endl;

int number = 1;
while( number )
{
number = -999999999;
cout << "\nEnter a number between -999,999,998 and 999,999,999 (0 to quit): ";
cin >> number;
cin.clear();
cin.sync();
number =  reverse( number );
if (number != -999999999)
cout << "The number with its digits reversed is ........................: "
<< number << endl;
}
return 0;
}

// IF n ends with ZERO(s) ... theses zeros are NOT shown,
// since leading zero are NOT significant.
int reverse (int n )
{
if ( n== 0 )
return 0;
if (abs(n) > 999999999)
return -999999999;

// Ok more than 1 digit  ...
// so ... get last digit of old and move to first of new
int reverse = 0;
int nextDig;
while ( n ) // exits when n == 0;
{
nextDig = n % 10;
reverse = reverse*10 + nextDig;
// update n ...
n  = n/10;
}
return reverse; // return reversed number

// see http://developers-heaven.net/forum/index.php/topic,46.0.html
// for HLA (High Level Assemley) introduced beside Python ...
}

```

Shalom,

David

### #5 baavgai

• Dreaming Coder

Reputation: 6292
• Posts: 13,458
• Joined: 16-October 07

## Re: reverse six digit number input from the keyboard

Posted 24 September 2008 - 05:53 AM

Let's play a little rewrite the code game. Then it may make more sense.

First, we'll get rid of most of the variables and organize our logic in a function.

```#include<stdio.h>

// note, function, because I hate typing
int getReverseNum(int num) {
int rev_num = 0;

rev_num += num-((num/10)*10);

rev_num *= 10;
rev_num += num/10-((num/100)*10);

rev_num *= 10;
rev_num += num/100-((num/1000)*10);

rev_num *= 10;
rev_num += num/1000-((num/10000)*10);

rev_num *= 10;
rev_num += num/10000-((num/100000)*10);

rev_num *= 10;
rev_num += num/100000-((num/1000000)*10);
return rev_num;

}

int main() {
int num;
// use user entry in final version, for now I'll just use a test number
// printf("Enter six digit number "); scanf("%d",&num);

num = 123456; printf("Number is %d\n", num);
printf("Reverse number is %d\n", getReverseNum(num));
}

```

Now for another refactor.

What hell are we doing? We multiply, we divide, we multiply, we divide, but we seem to be undoing the work we're doing in each step. Is is possible to get each step the same? And eliminate unnecessary work?

Maybe:
```num /= 10;
rev_num *= 10;
rev_num += num - ((num/10)*10);

```

So:
```int getReverseNum(int num) {
int rev_num = 0;

rev_num += num-((num/10)*10);

num /= 10;
rev_num *= 10;
rev_num += num - ((num/10)*10);

num /= 10;
rev_num *= 10;
rev_num += num - ((num/10)*10);

num /= 10;
rev_num *= 10;
rev_num += num - ((num/10)*10);

num /= 10;
rev_num *= 10;
rev_num += num - ((num/10)*10);

num /= 10;
rev_num *= 10;
rev_num += num - ((num/10)*10);

return rev_num;

}

```

Yep, that works. Now the thing screams for a loop. How many times do we want to loop? 6?

No. We want to loop until we have no more looping to do. We know we're done when our value is zero.

So, maybe this:
```int getReverseNum(int num) {
int rev_num = 0;

while(num>0) {
rev_num += num-((num/10)*10);
num /= 10;
if (num>0) {
rev_num *= 10;
}
}
return rev_num;
}

```

Hope this helps.

### #6 OliveOyl3471

• Everybody's crazy but me!

Reputation: 134
• Posts: 6,581
• Joined: 11-July 07

## Re: reverse six digit number input from the keyboard

Posted 24 September 2008 - 07:22 AM

Is there another way of doing this? Could you get the number as a string, then slice it and print it out backwards?
Or you could get user input one digit at a time, add each digit to an array and print it out backward.
Would that work?

### #7 baavgai

• Dreaming Coder

Reputation: 6292
• Posts: 13,458
• Joined: 16-October 07

## Re: reverse six digit number input from the keyboard

Posted 24 September 2008 - 09:50 AM

OliveOyl3471, on 24 Sep, 2008 - 10:22 AM, said:

Is there another way of doing this? Could you get the number as a string, then slice it and print it out backwards?
... Would that work?

Yep. Treating it as a string makes the problem significantly easier. Even the example I gave isn't ideal for numeric; mod is a better choice.

If you just want output, you can even, ahem, use recursion.
```void reverseString(char *s) {
if (*s==0) { return; }
reverseString(s+1);
printf("%c", *s);
}

```

OliveOyl3471, on 24 Sep, 2008 - 10:22 AM, said:

Or you could get user input one digit at a time, add each digit to an array and print it out backward.
Would that work?

Perhaps, but getting one character at time can be a challenge in C. String is easier.

This post has been edited by baavgai: 24 September 2008 - 09:50 AM

### #8 Soura

Reputation: 0
• Posts: 27
• Joined: 24-September 08

## Re: reverse six digit number input from the keyboard

Posted 24 September 2008 - 09:02 PM

amita.vij, on 24 Sep, 2008 - 01:14 AM, said:

Iam facing in writing one program.

Question: f a six digit number is input through the keyboard i.e. accept from the user. Display a number in reverse order.

The short program(using for loop) is-

The program is-
```/*Reverse No.*/
#include<stdio.h>
# include<conio.h>
# include<iostream.h>

main()
{
int a,n,s=0,t;
printf("Enter the number=");
scanf("%d", &a);
for(n=a;n!=0;n=n/10)
{
t=n%10;
s=(s*10+t);
}
printf("The Reverse No. is %d", s);
getch();
}
```

This post has been edited by JackOfAllTrades: 23 January 2011 - 06:56 AM
Reason for edit:: Added code tags

Reputation:

## Re: reverse six digit number input from the keyboard

Posted 23 January 2011 - 12:21 AM

hey!!
u r nt getting result for 6 digit number as u r using int data type and its range is from -32767 to 32767...and any 6 digit number exceeds its range..so try with unsigned long..i hope dis vl help u!!

• Saucy!

Reputation: 6203
• Posts: 23,937
• Joined: 23-August 08

## Re: reverse six digit number input from the keyboard

Posted 23 January 2011 - 06:55 AM

Only if you're using some ancient compiler will an int data type range be that small. But, I'm going to go out on a very sturdy limb and assume you're using the fossilized Turbo C/C++ compiler, which is probably older than you, because your professor and or educational system are too damn lazy to bring you up to something approximating current standards.