943,579 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 651
  • C RSS
Jul 20th, 2008
0

two letters = one number?

Expand Post »
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");
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
plike922 is offline Offline
38 posts
since Jul 2008
Jul 20th, 2008
0

Re: two letters = one number?

since this is a c program.
u cannot use iostream.h
and namespaces.
remove the 2 of them anf check it out.
Reputation Points: 19
Solved Threads: 25
Junior Poster
adarshcu is offline Offline
121 posts
since May 2008
Jul 22nd, 2008
1

Re: two letters = one number?

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.
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int num = 1234;
  5. int digit = 1;
  6.  
  7. if(!num) printf("0\n");
  8. else {
  9. while(digit < (num / 10)) digit *= 10;
  10.  
  11. do {
  12. printf("%d", (num / digit) % 10);
  13. digit /= 10;
  14. } while(digit);
  15.  
  16. printf("\n");
  17. }
  18.  
  19. return 0;
  20. }
It's just a matter of finding the leftmost digit and starting there, instead of starting at the right . . . .
Last edited by dwks; Jul 22nd, 2008 at 6:44 pm.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help Me With Sorting Algo
Next Thread in C Forum Timeline: urgent: modify code please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC