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 checkInteger(int num){
int number=num;
int currentDigit = number % 10;
number = number/10;
int counter=0;
int numberOfDigits=0;
while(number>0){
if(currentDigit > number % 10){
counter++;
}
currentDigit = number % 10;
number = number/10;
numberOfDigits++;
}
if(counter==numberOfDigits){
return 1;
}else{
number=num;
currentDigit = number % 10;
number = number/10;
counter=0;
numberOfDigits=0;
while(number>0){
if(currentDigit < number % 10){
counter++;
}
currentDigit = number % 10;
number = number/10;
numberOfDigits++;
}
if(counter==numberOfDigits){
return 2;
}
}
return 0;
}
int main(){
int number=4321;
cout << "Enter 4-digit integer: ";
cin>>number;
if(checkInteger(number)==1){
cout<<"The digits are in increasing order.\n";
}else if(checkInteger(number)==2){
cout<<"The digits are in decreasing order.\n";
}else{
cout<<"The digits are not in any order.\n";
}
system("pause");
return 0;
}
Comments
Leave a comment