Height of Factor Tree for a given number | C ++ program for the above approach ; Function to find the height of the Factor Tree of the integer N ; Stores the height of Factor Tree ; Loop to iterate over values of N ; Stores if there exist a factor of N or not ; Loop to find the smallest factor of N ; If i is a factor of N ; Increment the height ; If there are no factors of N i . e , N is prime , break loop ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorTree ( int N ) { int height = 0 ; while ( N > 1 ) { bool flag = false ; for ( int i = 2 ; i <= sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { N = N / i ; flag = true ; break ; } } height ++ ; if ( ! flag ) { break ; } } return height ; } int main ( ) { int N = 48 ; cout << factorTree ( N ) ; return 0 ; }