[c] 8th
Viewer
- // Online C compiler to run C program online
- #include <stdio.h>
- int isHillShape(int x) {
- // Convert to an array
- int numOfDigits = log10(x) + 1;
- if(numOfDigits < 3 || x<0)
- return 0;
- int* arr = calloc(numOfDigits, sizeof(int));
- for(int i=numOfDigits-1; i>=0; i--, x/=10) {
- arr[i] = x % 10;
- }
- // Check if the number is a HillShape Number
- int isIncreasing = 1;
- for (int i=0;i<numOfDigits-1;i++){
- if (isIncreasing == 1) {
- if (arr[i]<arr[i+1]){
- continue;
- }
- else if(arr[i] > arr[i+1]) {
- isIncreasing = 0;
- }
- else {
- return 0;
- }
- }
- else {
- if (arr[i] > arr[i+1]){
- continue;
- }
- else {
- return 0;
- }
- }
- }
- // Check if there was a downward slope
- if(isIncreasing)
- return 0;
- else
- return 1;
- }
- int main() {
- // Hillshape Number
- int x =-123212321;
- if (isHillShape(x) ==1){
- printf("The number is a Hillshape number");
- } else{
- printf("The number is not a Hillshape number");
- }
- }
Editor
You can edit this paste and save as new: