Answer to Question #213266 in C++ for faszna

Question #213266

write a function called findsequence.

the function takes a 4-digit integer as parameter, validates and returns 1 when the digits are in increasing order or 2 when the digits are in decreasing order or 0 when the digits are not in any order.


1
Expert's answer
2021-07-04T17:12:34-0400
#include <iostream>
using namespace std;
int findsequence(unsigned v)
{
    int d0 = v % 10; v /= 10;
    int d1 = v % 10; v /= 10;
    int d2 = v % 10; v /= 10;
    int d3 = v % 10; v /= 10;


    if(d3 >= d2 && d2 >= d1 && d1 >= d0)
    {
        return 2;
    }
    else
    if(d3 <= d2 && d2 <= d1 && d1 <= d0)
    {
        return 1;
    }
    
    return 0;
}


int main()
{
    cout << "Enter 4-digit integer: ";
    unsigned value;
    cin >> value;
    if(!cin || value > 9999 || value < 1000)
    {
        cout << "Bad input!\n";
        return 1;
    }


    cout << "findsequence(" << value << ") = " << findsequence(value) << "\n";
    
    return 0;    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS