Sum of n digit numbers divisible by a given number | Efficient CPP program to find the sum divisible numbers . ; find the Sum of having n digit and divisible by the number ; compute the first and last term ; first number which is divisible by given number ; last number which is divisible by given number ; total divisible number ; return the total sum ; Driver code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int totalSumDivisibleByNum ( int digit , int number ) { int firstnum = pow ( 10 , digit - 1 ) ; int lastnum = pow ( 10 , digit ) ; firstnum = ( firstnum - firstnum % number ) + number ; lastnum = ( lastnum - lastnum % number ) ; int count = ( ( lastnum - firstnum ) / number + 1 ) ; return ( ( lastnum + firstnum ) * count ) / 2 ; } int main ( ) { int n = 3 , number = 7 ; cout << totalSumDivisibleByNum ( n , number ) ; return 0 ; }