Sum of all N digit palindrome numbers | C ++ program for the above approach ; Function to calculate sum of n digit number ; Corner case ; Using above approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long double getSum ( int n ) { long double sum = 0 ; if ( n == 1 ) { sum = 45.0 ; } else { sum = ( 99.0 / 2.0 ) * pow ( 10 , n - 1 ) * pow ( 10 , ( n - 1 ) / 2 ) ; } return sum ; } int main ( ) { int n = 3 ; long double ans = getSum ( n ) ; cout << setprecision ( 12 ) << ans << ' ' ; return 0 ; }