Write a function called FindSequence. The function takes a 4-digit integer as parameter, validates the integer 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.
#include <iostream>
using namespace std;
int FindSequence(int number);
int main(){
int value;
cout << "Enter integer number: ";
cin>>value;
if(FindSequence(value)==1){
cout<<"The digits are in increasing order.\n";
}else if(FindSequence(value)==2){
cout<<"The digits are in decreasing order.\n";
}else{
cout<<"The digits are not in any order.\n";
}
system("pause");
return 0;
}
int FindSequence(int number){
int digit = number % 10;
number = number/10;
int increasingOrderCounter=0;
int decreasingOrderCounter=0;
int numberDigits=0;
while(number>0){
if(digit > number % 10){
increasingOrderCounter++;
}
if(digit < number % 10){
decreasingOrderCounter++;
}
digit = number % 10;
number = number/10;
numberDigits++;
}
if(increasingOrderCounter==numberDigits){
return 1;
}
if(decreasingOrderCounter==numberDigits){
return 2;
}
return 0;
}
Comments
Leave a comment