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 validateInteger(int number);
int main(){
int value;
cout << "Enter 4-digit integer value: ";
cin>>value;
if(validateInteger(value)==1){
cout<<"The digits are in increasing order.\n";
}else if(validateInteger(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 validateInteger(int number){
int value=number;
int digit = value % 10;
value = value/10;
int flagCounter=0;
int numberOfDigits=0;
while(value>0){
if(digit > value % 10){
flagCounter++;
}
digit = value % 10;
value = value/10;
numberOfDigits++;
}
if(flagCounter==numberOfDigits){
return 1;
}else{
value=number;
digit = value % 10;
value = value/10;
flagCounter=0;
numberOfDigits=0;
while(value>0){
if(digit < value % 10){
flagCounter++;
}
digit = value % 10;
value = value/10;
numberOfDigits++;
}
if(flagCounter==numberOfDigits){
return 2;
}
}
return 0;
}
Comments
Leave a comment