Jump to content
xisto Community
kvarnerexpress

Five Digit

Recommended Posts

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 codeCode:#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; }

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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.

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Erm, yes. I was wrong with what I posted. Thanks, cse :angry: I was thinking of the standard division operator /, instead of the modulo operator.if ( (sum/100000) >= 1 ) is true, then sum is too bigif ( (sum/100000) < 0.1 ) is true, then sum is too smallSorry about any inconvenience caused.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.