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