Sum of n digit numbers divisible by a given number | Efficient Java 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
using System ; class GFG { static int totalSumDivisibleByNum ( int digit , int number ) { int firstnum = ( int ) Math . Pow ( 10 , digit - 1 ) ; int lastnum = ( int ) Math . Pow ( 10 , digit ) ; firstnum = ( firstnum - firstnum % number ) + number ; lastnum = ( lastnum - lastnum % number ) ; int count = ( ( lastnum - firstnum ) / number + 1 ) ; return ( ( lastnum + firstnum ) * count ) / 2 ; } public static void Main ( ) { int n = 3 , number = 7 ; Console . WriteLine ( totalSumDivisibleByNum ( n , number ) ) ; } }