Sum of all N digit palindrome numbers | Java program for the above approach ; Function to calculate sum of n digit number ; Corner case ; Using above approach ; Driver code
class GFG { static double getSum ( int n ) { double sum = 0 ; if ( n == 1 ) { sum = 45.0 ; } else { sum = ( 99.0 / 2.0 ) * Math . pow ( 10 , n - 1 ) * Math . pow ( 10 , ( n - 1 ) / 2 ) ; } return sum ; } public static void main ( String [ ] args ) { int n = 3 ; double ans = getSum ( n ) ; System . out . print ( ans ) ; } }