Complement of a number with any base b | CPP program to find complement of a number with any base b ; Function to find ( b - 1 ) 's complement ; Calculate number of digits in the given number ; Largest digit in the number system with base b ; Largest number in the number system with base b ; return Complement ; Function to find b 's complement ; b ' s ▁ complement ▁ = ▁ ( b - 1 ) ' s complement + 1 ; Driver code
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; int prevComplement ( int n , int b ) { int maxDigit , maxNum = 0 , digits = 0 , num = n ; while ( n != 0 ) { digits ++ ; n = n / 10 ; } maxDigit = b - 1 ; while ( digits -- ) { maxNum = maxNum * 10 + maxDigit ; } return maxNum - num ; } int complement ( int n , int b ) { return prevComplement ( n , b ) + 1 ; } int main ( ) { cout << prevComplement ( 25 , 7 ) << endl ; cout << complement ( 25 , 7 ) ; return 0 ; }