Jul 26, 2008

Five Digit

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Five Digit

kvarnerexpress
Hi I am trying to code a program that will input five digit numbers and output it on the reverse order,

The code will output the reverse, I still cant find how to check if the input is five digit or not, i have tried using " % / " but i couldn't make it to work, any hint will be appreciated.


Thanks.

This is my code

Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int a,b,c,d,e,sum,find=0;

cout << "Enter Your Number of Five Digits: " << endl;
cin >> sum;


a = (sum % 10);


b = (sum % 100);
b = b / 10;


c = (sum % 1000);
c = c / 100;


d = (sum % 10000);
d = d / 1000;


e = (sum % 100000);
e = e / 10000;

cout << "The Reverse Order is: " << endl;
cout << a << b << c << d << e << endl;

}

Reply

theRealSheep
I can't give you the code for it off the top of my head without trying it myself, but you could do this...

You have e = (sum % 100000);. If e >= 1 then sum is six or more digits, If e < 0.1 then sum is only four or less digits.

Just a couple of if statements should do the trick. Hope that helps...

Reply

cse-icons
Hi theRealSheep,

As far as I know, the modulo or % operator works only for int and also returns an int, so no way you can get 0.1 or something..

also, the operator would wrap around i.e., the number would get negative and all that ( but not sure).

what I suggest is take the input as a string. Then use strlen function to find out the length.
After u do that use a for loop and convert all the string characters to numbers.

CODE
cin >> str;
int i = strlen(str);
int t =0;
if(i==5){
    for(int j=0;j<5;j++){
     if(isdigit(str[j]){
       t = t*10+(str[j]-'0');
     else
       break; // not a number
   }
}
else{
// do something else coz length not equal to 5
}


This code should work. though I have not tested it.
If u want to print reverse just run the for loop in reverse i.e, j =4 to 0
Check it out and confirm if it helped.

Cheers.

 

 

 


Reply

dexter
Soooo, you want to know if it has 5 digits... why not just test the bounds of a 5 digit number... 10000 - 99999....?

It all depends on what the input can be, though. Can it be 02180, or does it have to be an actual 5 digit number. If it is the latter, then the very first thing I said applies to your case.

Reply

theRealSheep
Erm, yes. I was wrong with what I posted. Thanks, cse wink.gif
I was thinking of the standard division operator /, instead of the modulo operator.

if ( (sum/100000) >= 1 ) is true, then sum is too big
if ( (sum/100000) < 0.1 ) is true, then sum is too small

Sorry about any inconvenience caused.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : digit


    Looking for digit

Searching Video's for digit
advertisement



Five Digit



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE