Sum of n digit numbers divisible by a given number | Simple CPP program to sum of n digit divisible numbers . ; Returns sum of n digit numbers divisible by ' number ' ; compute the first and last term ; sum of number which having n digit and divisible by number ; Driver code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int totalSumDivisibleByNum ( int n , int number ) { int firstnum = pow ( 10 , n - 1 ) ; int lastnum = pow ( 10 , n ) ; int sum = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) sum += i ; return sum ; } int main ( ) { int n = 3 , num = 7 ; cout << totalSumDivisibleByNum ( n , num ) << " STRNEWLINE " ; return 0 ; }