Sum of integers upto N with given unit digit

Given two integer N and D, the task is to find the sum of all the integers from 1 to N whose unit digit is D.

Examples:

Input: N = 30, D = 3
Output: 39
3 + 13 + 23 = 39

Input: N = 5, D = 7
Output: 0

Naive approach:

  • Traverse from 1 to N.
  • If the unit digit of the number is D add the number to the sum.
  • Finally print the value of sum.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class solution
{
  
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    for (int i = 1; i <= n; i++) {
  
        // If the unit digit is d
        if (i % 10 == d)
            sum += i;
    }
    return sum;
}
  
// Driver code
public static void main(String args[])
{
    int n = 30, d = 3;
    System.out.println(getSum(n, d));
     
}
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the required sum 
def getSum(n, d) :
    sum = 0
    for i in range(n + 1) : 
  
        # If the unit digit is d 
        if (i % 10 == d) :
            sum +=
    return sum
  
# Driver code 
if __name__ == "__main__" :
  
    n , d = 30, 3
    print(getSum(n, d))
  
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
class gfg
{
  // Function to return the required sum
 public static int getSum(int n, int d)
 {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
  
        // If the unit digit is d
        if (i % 10 == d)
            sum += i;
    }
    return sum;
}
  
// Driver code
 public static int Main()
 
    int n = 30, d = 3;
    Console.WriteLine( getSum(n, d));
    return 0;
 }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to return the required sum
function getSum($n, $d)
{
    $sum = 0;
    for ($i = 1; $i <= $n; $i++) 
    {
  
        // If the unit digit is d
        if ($i % 10 == $d)
            $sum += $i;
    }
    return $sum;
}
  
// Driver code
$n = 30;
$d = 3;
echo getSum($n, $d);
      
// This code is contributed by Sachin
?>

chevron_right


Output:

39

Efficient approach: While D < N update sum = sum + D and D = D + 10. Print the sum in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
  
// Function to return the required sum
ll getSum(int n, int d)
{
    ll sum = 0;
    while (d <= n) {
        sum += d;
        d += 10;
    }
    return sum;
}
  
// Driver code
int main()
{
    int n = 30, d = 3;
    cout << getSum(n, d);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class Solution
{
  
   
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    while (d <= n) {
        sum += d;
        d += 10;
    }
    return sum;
}
   
// Driver code
public static void main(String args[])
{
    int n = 30, d = 3;
    System.out.print(getSum(n, d));
      
}
}
//contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
# Function to return the required sum 
def getSum(n, d): 
    sum = 0
    while (d <= n): 
        sum += d
        d += 10
    return sum
  
# Driver code 
n = 30
d = 3
print(getSum(n, d))
  
# This code is contributed 
# by sahishelangia

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
class GFG
{
  
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    while (d <= n) 
    {
        sum += d;
        d += 10;
    }
    return sum;
}
  
// Driver code
public static void Main()
{
    int n = 30, d = 3;
    Console.Write(getSum(n, d));
}
}
  
// This code is contributed 
// by Akanksha Rai

chevron_right


PHP

Output:

39


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.