Given a number N in decimal base, find the sum of digits in any base B
Given a number N in decimal base, the task is to find the sum of digits of the number in any base B.
Examples:
Input: N = 100, B = 8
Output: 9
Explanation:
(100)8 = 144
Sum(144) = 1 + 4 + 4 = 9
Input: N = 50, B = 2
Output: 3
Explanation:
(50)2 = 110010
Sum(110010) = 1 + 1 + 0 + 0 + 1 + 0 = 3
Approach: Find unit digit by performing modulo operation on number N by base B and updating the number again by N = N / B and update sum by adding the unit digit at each step.
Below is the implementation of above approach
C++
#include <iostream>
using namespace std;
int sumOfDigit( int n, int b)
{
int unitDigit, sum = 0;
while (n > 0) {
unitDigit = n % b;
sum += unitDigit;
n = n / b;
}
return sum;
}
int main()
{
int n = 50;
int b = 2;
cout << sumOfDigit(n, b);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int sumOfDigit( int n, int b)
{
int unitDigit, sum = 0 ;
while (n > 0 )
{
unitDigit = n % b;
sum += unitDigit;
n = n / b;
}
return sum;
}
public static void main(String[] args)
{
int n = 50 ;
int b = 2 ;
System.out.print(sumOfDigit(n, b));
}
}
|
Python3
def sumOfDigit(n, b):
unitDigit = 0
sum = 0
while (n > 0 ):
unitDigit = n % b
sum + = unitDigit
n = n / / b
return sum
n = 50
b = 2
print (sumOfDigit(n, b))
|
C#
using System;
class GFG
{
static int sumOfDigit( int n, int b)
{
int unitDigit, sum = 0;
while (n > 0)
{
unitDigit = n % b;
sum += unitDigit;
n = n / b;
}
return sum;
}
public static void Main(String[] args)
{
int n = 50;
int b = 2;
Console.Write(sumOfDigit(n, b));
}
}
|
Javascript
<script>
function sumOfDigit(n, b)
{
var unitDigit, sum = 0;
while (n > 0)
{
unitDigit = n % b;
sum += unitDigit;
n = parseInt(n / b);
}
return sum;
}
var n = 50;
var b = 2;
document.write(sumOfDigit(n, b));
</script>
|
Time Complexity: O(logbN), where N is the given number
Auxiliary Space: O(1)
Approach 2: Using Recursion:
In this approach, we define a function sumOfDigit that takes two arguments – the number n and the base b. The function first checks for the base case, which is when the number becomes 0, and returns 0. Otherwise, the function computes the sum of the unit digit (i.e., the remainder when n is divided by b) and the sum of digits of the quotient (i.e., n divided by b) recursively.
This approach is also efficient and readable, and can handle large numbers without the risk of overflow.
Here is the code of above approach:
C++
#include <iostream>
using namespace std;
int sumOfDigit( int n, int b) {
if (n == 0) {
return 0;
}
return (n % b) + sumOfDigit(n / b, b);
}
int main() {
int n = 50;
int b = 2;
cout << sumOfDigit(n, b) << endl;
return 0;
}
|
Java
public class SumOfDigits {
public static int sumOfDigit( int n, int b) {
if (n == 0 ) {
return 0 ;
}
return (n % b) + sumOfDigit(n / b, b);
}
public static void main(String[] args) {
int n = 50 ;
int b = 2 ;
System.out.println(sumOfDigit(n, b));
}
}
|
Python3
def sum_of_digit(n, b):
if n = = 0 :
return 0
return (n % b) + sum_of_digit(n / / b, b)
if __name__ = = '__main__' :
n = 50
b = 2
print (sum_of_digit(n, b))
|
C#
using System;
public class SumOfDigits
{
public static int SumOfDigit( int n, int b)
{
if (n == 0)
{
return 0;
}
return (n % b) + SumOfDigit(n / b, b);
}
public static void Main( string [] args)
{
int n = 50;
int b = 2;
System.Console.WriteLine(SumOfDigit(n, b));
}
}
|
Javascript
function sum_of_digit(n, b) {
if (n == 0) {
return 0;
}
return (n % b) + sum_of_digit(Math.floor(n / b), b);
}
let n = 50;
let b = 2;
console.log(sum_of_digit(n, b));
|
Time Complexity: O(logbN), where N is the given number
Auxiliary Space: O(1)
Last Updated :
23 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...