answersLogoWhite

0


Best Answer

/*The coding style used in this source code is for convenience.

* It is widely used style of coding. */

#include <stdio.h>

void main() {

int number, modulus, reverse;

reverse = 0;

printf("Enter a number \n");

scanf("%d", &number);

while(number != 0) {

modulus = number % 10;

reverse = (reverse * 10) + modulus;

number =number / 10;

}

printf("The reversed number is %d", reverse);

getch();

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

int RevNum( int num )

{

const int base = 10;

int result = 0;

do

{

result *= base;

result += num % base;

} while( num /= base);

return( result );

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There are two ways to reverse a number, as a string and as a value. The string form is required if you wish values with trailing zeroes to be written literally, so that 100 becomes 001. As a value, 100 becomes 1. Both forms are shown in the example below, which prints 10 random numbers in reverse.

#include<iostream>

#include<time.h>

int rev_as_num(int num)

{

int result = 0;

while( num )

{

result*=10;

result+=num%10;

num/=10;

}

return(result);

}

std::string rev_as_string(int num)

{

std::string result;

while( num )

{

result.push_back(num%10+'0');

num/=10;

}

return(result);

}

int main()

{

srand((unsigned) time(NULL));

for( int loop=0; loop<10; ++loop )

{

int num = rand(); // random number in range [0...RAND_MAX].

std::cout<<"Original:\t"<<num<<'\n'

<<"Reversed:\t"<<rev_as_string(num).c_str()<<'\n'

<<"As value:\t"<<rev_as_num(num)<<'\n'<<std::endl;

}

return(0);

}

Example output:

Original: 32254

Reversed: 45223

As value: 45223

Original: 15754

Reversed: 45751

As value: 45751

Original: 19691

Reversed: 19691

As value: 19691

Original: 10922

Reversed: 22901

As value: 22901

Original: 19449

Reversed: 94491

As value: 94491

Original: 24028

Reversed: 82042

As value: 82042

Original: 5746

Reversed: 6475

As value: 6475

Original: 29410

Reversed: 01492

As value: 1492

Original: 6189

Reversed: 9816

As value: 9816

Original: 20712

Reversed: 21702

As value: 21702

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

int RevNum( int num )

{

const int base = 10;

int result = 0;

int remain = 0;

do

{

remain = num % base;

result *= base;

result += remain;

} while( num /= base);

return( result );

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C code programming to reverse a number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Code for creating pascal's triangle in C programming language?

code for creating pascal's triangle in C programming language?


Getting source code of c?

C is a programming language, so it doesn't have source code.(On the other hand, C compilers do have source code, but you, as a beginner in programming, could not understand them.)


How do you reverse of the answer program integers in C programming?

sense question Your makes no.


Where can one find C programming tutorials?

You can find C programming tutorials online at the C programming website. They provide both free and paid tutorials for many aspects of the C and C++ code.


What does generic programming in c plus plus means?

Generic programming means that the code is generic enough that it compile on any C++ implementation. That is, it has no platform-specific code.


What is c and why you use?

C is a programming language, and it's mostly used for Systems Programming. Most kernels these days have at least some C code in them.


Microsoft office's source code written by which programming language?

C++


How Create mouse in C programming source code?

Here is the article for Mouse programming in C. In this link full details are given nicely.http://electrofriends.com/articles/computer-science/c-tutorials/mouse-programming-in-cc/


What is programming languages in c plus plus?

Programming in C++ means designing and writing a computer program using the C++ programming language. C++ is a high-level, machine-independent language which must be converted into machine-dependent code by a compiler.


What is Turbo C download used for?

Turbo C is a compiler for a general purpose computer programming language called C. It transforms code written in C into the computer language needed for executable programming.


C programming source code for push down automata?

i cant sorry


What do you mean by c taken programming in c?

A C program is a computer program written using the C programming language.