I am wonder how do you make it that you have to letters = to one number with only typing it once? Here is my program. My problem is that the result printf("%d reversed is: %d\n",num , res); is 0 reversed is: (The number backwords).

#include <iostream>
#include <stdio.h>
#include "simpio.h"
#include "strlib.h"
#include "random.h"
using namespace std;

int main(void)
	int num, res=0, rem;
	printf("Please enter an integer: ");
	num = GetInteger();
	while(num > 0)
rem=num% 10;
res=res* 10 + rem;
printf("%d reversed is: %d\n",num , res);
8 Years
Discussion Span
Last Post by dwks

since this is a c program.
u cannot use iostream.h
and namespaces.
remove the 2 of them anf check it out.


You could save the numbers as you find them instead of printing them, say in a char array, and then print that array in reverse. On the other hand, it would probably be better to use a different algorithm.

What you need to do is find the most significant (highest and leftmost) digit first. To do this, you'll need to skip over the digits to the right first . . . .

Here's how you can do it.

#include <stdio.h>

int main() {
    int num = 1234;
    int digit = 1;

    if(!num) printf("0\n");
    else {
        while(digit < (num / 10)) digit *= 10;

        do {
            printf("%d", (num / digit) % 10);
            digit /= 10;
        } while(digit);


    return 0;

It's just a matter of finding the leftmost digit and starting there, instead of starting at the right . . . .

Votes + Comments
Good logic :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.