[c] 8th

Viewer

  1. // Online C compiler to run C program online
  2. #include <stdio.h>
  3.  
  4. int isHillShape(int x) {
  5.     
  6.     // Convert to an array
  7.     int numOfDigits = log10(x) + 1; 
  8.     if(numOfDigits < 3 || x<0)
  9.         return 0;
  10.     int* arr = calloc(numOfDigits, sizeof(int)); 
  11.     for(int i=numOfDigits-1; i>=0; i--, x/=10) { 
  12.             arr[i] = x % 10;
  13.     }
  14.     
  15.     // Check if the number is a HillShape Number
  16.     int isIncreasing = 1;
  17.     for (int i=0;i<numOfDigits-1;i++){
  18.         if (isIncreasing == 1) {
  19.             if (arr[i]<arr[i+1]){
  20.                 continue;
  21.             } 
  22.             else if(arr[i] > arr[i+1]) {
  23.                 isIncreasing = 0;
  24.             }
  25.             else {
  26.                 return 0;
  27.             }
  28.         }
  29.         else {
  30.             if (arr[i] > arr[i+1]){
  31.                 continue;
  32.             }
  33.             else {
  34.                 return 0;
  35.             }
  36.         }
  37.     }
  38.     
  39.     // Check if there was a downward slope
  40.     if(isIncreasing)
  41.         return 0;
  42.     else
  43.         return 1;
  44. }
  45.  
  46. int main() {
  47.     // Hillshape Number
  48.     int x =-123212321;
  49.     
  50.     if (isHillShape(x) ==1){
  51.         printf("The number is a Hillshape number");
  52.     } else{
  53.         printf("The number is not a Hillshape number");
  54.     }
  55.     
  56. }

Editor

You can edit this paste and save as new:


File Description
  • 8th
  • Paste Code
  • 15 Jun-2021
  • 1.25 Kb
You can Share it: