| | |
two letters = one number?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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;
num=num/10;
}
printf("%d reversed is: %d\n",num , res);
system("pause");
} 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.
It's just a matter of finding the leftmost digit and starting there, instead of starting at the right . . . .
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.
c Syntax (Toggle Plain Text)
#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); printf("\n"); } return 0; }
Last edited by dwks; Jul 22nd, 2008 at 6:44 pm.
dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
![]() |
Similar Threads
- Read a phrase and Display the amount of letters. (C++)
- convert lower case letters to uppercase and vice-versa (C++)
- Regex for password (Shell Scripting)
- Bash shell needs tweeking (Shell Scripting)
- help with counting letters part of program (C++)
- How DO you count Upper Case letters? (Java)
- Need to know how to create a database that will generate a random number (Database Design)
- Trying to create a method to convert string letters into a two dimensional array (Java)
- Counting the amount of letters in a phrase! (C++)
Other Threads in the C Forum
- Previous Thread: Help Me With Sorting Algo
- Next Thread: urgent: modify code please
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h





