Sum of integers upto N with given unit digit ( Set 2 ) | C ++ implementation of the approach ; Function to return the required sum ; Decrement N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll getSum ( ll n , int d ) { if ( n < d ) return 0 ; while ( n % 10 != d ) n -- ; ll k = n / 10 ; return ( k + 1 ) * d + ( k * 10 + 10 * k * k ) / 2 ; } int main ( ) { ll n = 30 ; int d = 3 ; cout << getSum ( n , d ) ; return 0 ; }