QUESTION 01
Write the sequence of steps to evaluate the operators given in the following expressions using the procedence and associavity.
1)(10*3%4*(3+((18%5)%4*3+6/(18/4))));
2)int i=9;
x=++i%4*4+(5-2)
QUESTION 02
Write a function called FindSquare.
The function takes a 4-digit integer as parameter, validates the integer and returns, when the digits are in increasing order or 2 when the digits are in decreasing order or 0 when the digits are not any order.
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
int findOutput(int x){
int arr[4], sorted[4];
if(x > 9999 || x < 1000){
cout<<"Not a 4 digit integer\n";
return -1;
}
for(int i = 3,j = 0; i >= 0; i--, j++){
arr[j] = x / (int)pow(10, i);
sorted[j] = x / (int)pow(10, i);
x -= arr[j] * (int)pow(10, i);
}
sort(sorted, sorted + 4);
bool flag1 = true, flag2 = true;
for(int i = 0;i < 4; i++){
if(sorted[i] != arr[i]) flag1 = false;
}
for(int i = 3, j = 0; i >= 0, j < 4; i--, j++){
if(sorted[i] != arr[j]) flag2 = false;
}
if(!flag1 && !flag2) return 0;
if(flag1) return 0;
else return 2;
}
int main(){
int x;
cout<<"Input a 4 digit integer: ";
cin>>x;
cout<<findOutput(x);
return 0;
}
Output:
Comments
Leave a comment