two letters = one number?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

two letters = one number?

 
0
  #1
Jul 20th, 2008
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");
}
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 25
Reputation: adarshcu is an unknown quantity at this point 
Solved Threads: 4
adarshcu adarshcu is offline Offline
Light Poster

Re: two letters = one number?

 
0
  #2
Jul 20th, 2008
since this is a c program.
u cannot use iostream.h
and namespaces.
remove the 2 of them anf check it out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: two letters = one number?

 
1
  #3
Jul 22nd, 2008
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.
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC