Find the slope of the given number | C ++ implementation to find slope of a number ; function to find slope of a number ; to store slope of the given number ' num ' ; loop from the 2 nd digit up to the 2 nd last digit of the given number ' num ' ; if the digit is a maxima ; if the digit is a minima ; required slope ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int slopeOfNum ( string num , int n ) { int slope = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( num [ i ] > num [ i - 1 ] && num [ i ] > num [ i + 1 ] ) slope ++ ; else if ( num [ i ] < num [ i - 1 ] && num [ i ] < num [ i + 1 ] ) slope ++ ; } return slope ; } int main ( ) { string num = "1213321" ; int n = num . size ( ) ; cout << " Slope ▁ = ▁ " << slopeOfNum ( num , n ) ; return 0 ; }