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);

Recommended Answers

All 2 Replies

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 . . . .

commented: Good logic :) +2
Be a part of the DaniWeb community

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