Answer to Question #213265 in C++ for faszna

Question #213265

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-03T15:12:08-0400
#include <iostream>

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

    std::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