Number formed by adding product of its max and min digit K times | C ++ Code for the above approach ; function that returns the product of maximum and minimum digit of N number . ; finds the last digit . ; Moves to next digit ; Function to find the formed number ; K -- ; M ( 1 ) = N ; check if minimum digit is 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prod_of_max_min ( int n ) { int largest = 0 ; int smallest = 10 ; while ( n ) { int r = n % 10 ; largest = max ( r , largest ) ; smallest = min ( r , smallest ) ; n = n / 10 ; } return largest * smallest ; } int formed_no ( int N , int K ) { if ( K == 1 ) { return N ; } int answer = N ; while ( K -- ) { int a_current = prod_of_max_min ( answer ) ; if ( a_current == 0 ) break ; answer += a_current ; } return answer ; } int main ( ) { int N = 487 , K = 100000000 ; cout << formed_no ( N , K ) << endl ; return 0 ; }