Programa para Suma de los dígitos de un número dado – Part 1

Dado un número, hallar la suma de sus dígitos.

Ejemplos: 

C++

// C program to compute sum of digits in
// number.
#include <iostream>
using namespace std;
 
/* Function to get sum of digits */
class gfg {
public:
    int getSum(int n)
    {
        int sum = 0;
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }
};
 
// Driver code
int main()
{
    gfg g;
    int n = 687;
    cout << g.getSum(n);
    return 0;
}
// This code is contributed by Soumik

C

// C program to compute sum of digits in
// number.
#include <stdio.h>
 
/* Function to get sum of digits */
int getSum(int n)
{
    int sum = 0;
    while (n != 0) {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
 
// Driver code
int main()
{
    int n = 687;
    printf(" %d ", getSum(n));
    return 0;
}

Java

// Java program to compute
// sum of digits in number.
import java.io.*;
 
class GFG {
 
    /* Function to get sum of digits */
    static int getSum(int n)
    {
        int sum = 0;
 
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 687;
 
        System.out.println(getSum(n));
    }
}
 
// This code is contributed by Gitanjali

Python3

# Python 3 program to
# compute sum of digits in
# number.
 
# Function to get sum of digits
 
 
def getSum(n):
 
    sum = 0
    while (n != 0):
 
        sum = sum + int(n % 10)
        n = int(n/10)
 
    return sum
 
 
# Driver code
n = 687
print(getSum(n))

C#

// C# program to compute
// sum of digits in number.
using System;
 
class GFG {
    /* Function to get sum of digits */
    static int getSum(int n)
    {
        int sum = 0;
 
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 687;
        Console.Write(getSum(n));
    }
}
 
// This code is contributed by Sam007

PHP

<?php
// PHP Code to compute sum
// of digits in number.
 
// Function to get
// $sum of digits
function getsum($n)
{
    $sum = 0;
    while ($n != 0)
    {
        $sum = $sum + $n % 10;
        $n = $n/10;
    }
    return $sum;
}
 
// Driver Code
$n = 687;
$res = getsum($n);
echo("$res");
 
// This code is contributed by
// Smitha Dinesh Semwal.
?>

Javascript

<script>
 
// Javascript program to compute sum of digits in
// number.
 
/* Function to get sum of digits */
function getSum(n)
{
    var sum = 0;
    while (n != 0) {
        sum = sum + n % 10;
        n = parseInt(n / 10);
    }
    return sum;
}
 
// Driver code
var n = 687;
document.write(getSum(n));
 
</script>

C++

#include <iostream>
using namespace std;
 
/* Function to get sum of digits */
class gfg {
public:
    int getSum(int n)
    {
        int sum;
 
        /* Single line that calculates sum */
        for (sum = 0; n > 0; sum += n % 10, n /= 10)
            ;
 
        return sum;
    }
};
 
// Driver code
int main()
{
    gfg g;
    int n = 687;
    cout << g.getSum(n);
    return 0;
}
// This code is contributed by Soumik

C

#include <stdio.h>
 
/* Function to get sum of digits */
int getSum(int n)
{
    int sum;
 
    /* Single line that calculates sum */
    for (sum = 0; n > 0; sum += n % 10, n /= 10)
        ;
 
    return sum;
}
 
// Driver code
int main()
{
    int n = 687;
    printf(" %d ", getSum(n));
    return 0;
}

Java

// Java program to compute
// sum of digits in number.
import java.io.*;
 
class GFG {
 
    /* Function to get sum of digits */
    static int getSum(int n)
    {
        int sum;
 
        /* Single line that calculates sum */
        for (sum = 0; n > 0; sum += n % 10, n /= 10)
            ;
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 687;
 
        System.out.println(getSum(n));
    }
}
 
// This code is contributed by Gitanjali

Python3

# Function to get sum of digits
 
 
def getSum(n):
 
    sum = 0
 
    # Single line that calculates sum
    while(n > 0):
        sum += int(n % 10)
        n = int(n/10)
 
    return sum
 
 
# Driver code
n = 687
print(getSum(n))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to compute
// sum of digits in number.
using System;
 
class GFG {
    static int getSum(int n)
    {
        int sum;
 
        /* Single line that calculates sum */
        for (sum = 0; n > 0; sum += n % 10, n /= 10)
            ;
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 687;
        Console.Write(getSum(n));
    }
}
 
// This code is contributed by Sam007

PHP

<?php
// PHP Code for Sum the
// digits of a given number
 
// Function to get sum of digits
function getsum($n)
{
 
    // Single line that calculates $sum
    for ($sum = 0; $n > 0; $sum += $n % 10,
                                  $n /= 10);
    return $sum;
}
 
// Driver Code
$n = 687;
echo(getsum($n));
 
// This code is contributed by
// Smitha Dinesh Semwal.
?>

Javascript

<script>
 
// Javascript program to compute
// sum of digits in number.
 
// Function to get sum of digits
function getSum(n)
{
    let sum;
 
    // Single line that calculates sum
    for(sum = 0; n > 0;
        sum += n % 10,
        n = parseInt(n / 10))
        ;
    return sum;
}
 
// Driver code
let n = 687;
 
document.write(getSum(n));
 
// This code is contributed by subhammahato348
 
</script>

C++

// C++ program to compute
// sum of digits in number.
#include <iostream>
using namespace std;
class gfg {
public:
    int sumDigits(int no)
    {
        if(no == 0){
          return 0 ;
        }
       
        return (no % 10) + sumDigits(no / 10) ;
    }
};
 
// Driver code
int main(void)
{
    gfg g;
    cout << g.sumDigits(687);
    return 0;
}

C

// C program to compute
// sum of digits in number.
#include <stdio.h>
 
int sumDigits(int no)
{
  if(no == 0){
    return 0 ;
  }
 
  return (no % 10) + sumDigits(no / 10) ;
}
 
int main()
{
    printf("%d", sumDigits(687));
    return 0;
}

Java

// Java program to compute
// sum of digits in number.
import java.io.*;
 
class GFG {
 
    /* Function to get sum of digits */
    static int sumDigits(int no)
    {
        if(no == 0){
          return 0 ;
        }
 
        return (no % 10) + sumDigits(no / 10) ;
     }
 
    // Driver code
    public static void main(String[] args)
    {
        System.out.println(sumDigits(687));
    }
}
 
// This code is contributed by Gitanjali

Python3

# Python program to compute
# sum of digits in number.
 
 
def sumDigits(no):
    return 0 if no == 0 else int(no % 10) + sumDigits(int(no/10))
 
 
# Driver code
print(sumDigits(687))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to compute
// sum of digits in number.
using System;
 
class GFG {
    /* Function to get sum of digits */
    static int sumDigits(int no)
    {
        return no == 0 ? 0 : no % 10 + sumDigits(no / 10);
    }
 
    // Driver code
    public static void Main()
    {
        Console.Write(sumDigits(687));
    }
}
 
// This code is contributed by Sam007

PHP

<?php
// PHP program to compute
// sum of digits in number.
function sumDigits($no)
{
return $no == 0 ? 0 : $no % 10 +
                      sumDigits($no / 10) ;
}
 
// Driver Code
echo sumDigits(687);
 
// This code is contributed by aj_36
?>

Javascript

<script>
// Program to compute
// sum of digits in number
  // Function to get sum of digits
          
     function sumDigits(no)
     {
        if(no == 0){
          return 0 ;
        }
 
        return (no % 10) + sumDigits(parseInt(no/10)) ;
      }
       
// Driver code
      document.write(sumDigits(687));
       
// This is code is contributed by simranarora5sos
</script>

C++14

// C++ implementation of the above approach
#include <iostream>
using namespace std;
int getSum(string str)
{
    int sum = 0;
 
    // Traversing through the string
    for (int i = 0; i < str.length(); i++) {
        // Since ascii value of
        // numbers starts from 48
        // so we subtract it from sum
        sum = sum + str[i] - 48;
    }
    return sum;
}
 
// Driver Code
int main()
{
    string st = "123456789123456789123422";
    cout << getSum(st);
    return 0;
}

Java

// Java implementation of the above approach
import java.io.*;
class GFG {
 
    static int getSum(String str)
    {
        int sum = 0;
 
        // Traversing through the string
        for (int i = 0; i < str.length(); i++) {
 
            // Since ascii value of
            // numbers starts from 48
            // so we subtract it from sum
            sum = sum + str.charAt(i) - 48;
        }
        return sum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String st = "123456789123456789123422";
        System.out.print(getSum(st));
    }
}
 
// This code is contributed by Dharanendra L V.

Python3

# Python implementation of the above approach
def getSum(n):
    # Initializing sum to 0
    sum = 0
    # Traversing through string
    for i in n:
        # Converting char to int
        sum = sum + int(i)
 
    return sum
 
 
n = "123456789123456789123422"
print(getSum(n))

C#

// C# implementation of the above approach
using System;
public class GFG {
    static int getSum(String str)
    {
        int sum = 0;
 
        // Traversing through the string
        for (int i = 0; i < str.Length; i++) {
 
            // Since ascii value of
            // numbers starts from 48
            // so we subtract it from sum
            sum = sum + str[i] - 48;
        }
        return sum;
    }
 
    // Driver Code
    static public void Main()
    {
        String st = "123456789123456789123422";
        Console.Write(getSum(st));
    }
}
 
// This code is contributed by Dharanendra L V.

Javascript

<script>
// Javascript implementation of the above approach
 
function getSum(str)
{
    let sum = 0;
 
    // Traversing through the string
    for (let i = 0; i < str.length; i++)
    {
     
        // Since ascii value of
        // numbers starts from 48
        // so we subtract it from sum
        sum = sum + parseInt(str[i]);
    }
    return sum;
}
 
// Driver Code
let st = "123456789123456789123422";
document.write(getSum(st));
 
// This code is contributed by subhammahato348.
</script>

C++

// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to check sum
// of digit using tail recursion
int sum_of_digit(int n, int val)
{
    if (n < 10)
    {
        val = val + n;
        return val;
    }
    return sum_of_digit(n / 10, (n % 10) + val);
}
 
// Driver code
int main()
{
    int num = 12345;
    int result = sum_of_digit(num, 0);
     
    cout << "Sum of digits is " << result;
     
    return 0;
}
 
// This code is contributed by subhammahato348

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class sum_of_digits {
   
    // Function to check sum
    // of digit using tail recursion
    static int sum_of_digit(int n, int val)
    {
        if (n < 10) {
            val = val + n;
            return val;
        }
        return sum_of_digit(n / 10, (n % 10) + val);
    }
 
    // Driven Program to check above
    public static void main(String args[])
    {
        int num = 12345;
        int result = sum_of_digit(num, 0);
        System.out.println("Sum of digits is " + result);
    }
}

Python3

# Python3 program for the above approach
 
# Function to check sum
# of digit using tail recursion
def sum_of_digit(n, val):
     
    if (n < 10):
        val = val + n
        return val
         
    return sum_of_digit(n // 10, (n % 10) + val)
 
# Driver code
num = 12345
result = sum_of_digit(num, 0)
 
print("Sum of digits is", result)
 
# This code is contributed by subhammahato348

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to check sum
// of digit using tail recursion
static int sum_of_digit(int n, int val)
{
    if (n < 10)
    {
        val = val + n;
        return val;
    }
    return sum_of_digit(n / 10, (n % 10) + val);
}
 
// Driver code
public static void Main()
{
    int num = 12345;
    int result = sum_of_digit(num, 0);
 
    Console.Write("Sum of digits is " + result);
}
}
 
// This code is contributed by subhammahato348

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to check sum
// of digit using tail recursion
function sum_of_digit(n, val)
{
    if (n < 10)
    {
        val = val + n;
        return val;
    }
    return sum_of_digit(parseInt(n / 10),
    (n % 10) + val);
}
 
// Driver code
    let num = 12345;
    let result = sum_of_digit(num, 0);
     
    document.write("Sum of digits is " + result);
 
// This code is contributed by subhammahato348
 
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *