Write a program to find sum of middle 2 or 3 digits of an entered number in python

For example take 2345, how we add middle numbers 3 and 4 and gives output ..the sum of middle numbers is 7.

xavier This code works for 4 digit number. If you want code that works for any digit, you can tell me. I will give you. #include int main() { int a,sum; printf("Enter a number: "); scanf("%d",&a); sum=(a/10)%10+(a/100)%10; printf("Answer: %d",sum); return 0; }

Write a program to find sum of middle 2 or 3 digits of an entered number in python

The easiest way is to convert the number into an array of characters, take the substring and then find the sum.

Write a program to find sum of middle 2 or 3 digits of an entered number in python

i have tried but no use can you provide me with complete code

Write a program to find sum of middle 2 or 3 digits of an entered number in python

If you are sure it is a four digit number then #include /* Function to get sum of digits */ int getSum(int n) { int sum; sum=0; /* Single line that calculates sum */ sum=sum+(n/100)%10+(n%100)/10; return sum; } // Driver code int main() { int n = 3547; printf(" %d ", getSum(n)); return 0; }

Write a program to find sum of middle 2 or 3 digits of an entered number in python

Can you suugest me with code

Write a program to find sum of middle 2 or 3 digits of an entered number in python

Write a program to find sum of middle 2 or 3 digits of an entered number in python

/* 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 = 3547; printf(" %d ", getSum(n)); return 0; }

Write a program to find sum of middle 2 or 3 digits of an entered number in python

Write a program to find sum of middle 2 or 3 digits of an entered number in python

Write a program to find sum of middle 2 or 3 digits of an entered number in python

Use while loop to get number of digits

Write a program to find sum of middle 2 or 3 digits of an entered number in python

i dont know to use while loop that is why i request you to provide code

Write a program to find sum of middle 2 or 3 digits of an entered number in python

Write a program to find sum of middle 2 or 3 digits of an entered number in python

Am new here who can teach me python directly

Write a program to find sum of middle 2 or 3 digits of an entered number in python

Given a number, find the sum of its digits.

Examples : 

Input: n = 687
Output: 21

Input: n = 12
Output: 3

Follow the below steps to solve the problem:

  • Get the number
  • Declare a variable to store the sum and set it to 0
  • Repeat the next two steps till the number is not 0
  • Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
  • Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit.
  • Print or return the sum

Below is the implementation of the above approach:

C++

#include

using namespace std;

class gfg {

public:

    int getSum(int n)

    {

        int sum = 0;

        while (n != 0) {

            sum = sum + n % 10;

            n = n / 10;

        }

        return sum;

    }

};

int main()

{

    gfg g;

    int n = 687;

    cout << g.getSum(n);

    return 0;

}

C

#include

int getSum(int n)

{

    int sum = 0;

    while (n != 0) {

        sum = sum + n % 10;

        n = n / 10;

    }

    return sum;

}

int main()

{

    int n = 687;

    printf(" %d ", getSum(n));

    return 0;

}

Java

import java.io.*;

class GFG {

    static int getSum(int n)

    {

        int sum = 0;

        while (n != 0) {

            sum = sum + n % 10;

            n = n / 10;

        }

        return sum;

    }

    public static void main(String[] args)

    {

        int n = 687;

        System.out.println(getSum(n));

    }

}

Python3

def getSum(n):

    sum = 0

    while (n != 0):

        sum = sum + int(n % 10)

        n = int(n/10)

    return sum

if __name__ == "__main__":

    n = 687

    print(getSum(n))

C#

using System;

class GFG {

    static int getSum(int n)

    {

        int sum = 0;

        while (n != 0) {

            sum = sum + n % 10;

            n = n / 10;

        }

        return sum;

    }

    public static void Main()

    {

        int n = 687;

        Console.Write(getSum(n));

    }

}

PHP

function getsum($n)

{

    $sum = 0;

    while ($n != 0)

    {

        $sum = $sum + $n % 10;

        $n = $n/10;

    }

    return $sum;

}

$n = 687;

$res = getsum($n);

echo("$res");

?>

Javascript

Time Complexity: O(log N)
Auxiliary Space: O(1)

How to compute in a single line?

The below function has three lines instead of one line, but it calculates the sum in one line using for loop. It can be made one-line function if we pass the pointer to the sum. 

Below is the implementation of the above approach:

C++

#include

using namespace std;

class gfg {

public:

    int getSum(int n)

    {

        int sum;

        for (sum = 0; n > 0; sum += n % 10, n /= 10)

            ;

        return sum;

    }

};

int main()

{

    gfg g;

    int n = 687;

    cout << g.getSum(n);

    return 0;

}

C

#include

int getSum(int n)

{

    int sum;

    for (sum = 0; n > 0; sum += n % 10, n /= 10)

        ;

    return sum;

}

int main()

{

    int n = 687;

    printf(" %d ", getSum(n));

    return 0;

}

Java

import java.io.*;

class GFG {

    static int getSum(int n)

    {

        int sum;

        for (sum = 0; n > 0; sum += n % 10, n /= 10)

            ;

        return sum;

    }

    public static void main(String[] args)

    {

        int n = 687;

        System.out.println(getSum(n));

    }

}

Python3

def getSum(n):

    sum = 0

    while(n > 0):

        sum += int(n % 10)

        n = int(n/10)

    return sum

if __name__ == "__main__":

    n = 687

    print(getSum(n))

C#

using System;

class GFG {

    static int getSum(int n)

    {

        int sum;

        for (sum = 0; n > 0; sum += n % 10, n /= 10)

            ;

        return sum;

    }

    public static void Main()

    {

        int n = 687;

        Console.Write(getSum(n));

    }

}

PHP

function getsum($n)

{

    for ($sum = 0; $n > 0; $sum += $n % 10,

                                  $n /= 10);

    return $sum;

}

$n = 687;

echo(getsum($n));

?>

Javascript

Time Complexity: O(log N)
Auxiliary Space: O(1)

Sum of the digits of a given number using recursion:

Follow the below steps to solve the problem:

  • Get the number
  • Get the remainder and pass the next remaining digits
  • Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
  • Divide the number by 10 with help of the ‘/’ operator to remove the rightmost digit.
  • Check the base case with n = 0
  • Print or return the sum

Below is the implementation of the above approach:

C++

#include

using namespace std;

class gfg {

public:

    int sumDigits(int no)

    {

        if (no == 0) {

            return 0;

        }

        return (no % 10) + sumDigits(no / 10);

    }

};

int main(void)

{

    gfg g;

    cout << g.sumDigits(687);

    return 0;

}

C

#include

int sumDigits(int no)

{

    if (no == 0) {

        return 0;

    }

    return (no % 10) + sumDigits(no / 10);

}

int main()

{

    printf("%d", sumDigits(687));

    return 0;

}

Java

import java.io.*;

class GFG {

    static int sumDigits(int no)

    {

        if (no == 0) {

            return 0;

        }

        return (no % 10) + sumDigits(no / 10);

    }

    public static void main(String[] args)

    {

        System.out.println(sumDigits(687));

    }

}

Python3

def sumDigits(no):

    return 0 if no == 0 else int(no % 10) + sumDigits(int(no/10))

if __name__ == "__main__":

    print(sumDigits(687))

C#

using System;

class GFG {

    static int sumDigits(int no)

    {

        return no == 0 ? 0 : no % 10 + sumDigits(no / 10);

    }

    public static void Main()

    {

        Console.Write(sumDigits(687));

    }

}

PHP

function sumDigits($no)

{

return $no == 0 ? 0 : $no % 10 +

                      sumDigits($no / 10) ;

}

echo sumDigits(687);

?>

Javascript

Time Complexity: O(log N)
Auxiliary Space: O(log N)

Sum of the digits of a given number with input as string:

When the number of digits of that number exceeds 1019 , we can’t take that number as an integer since the range of long long int doesn’t satisfy the given number. So take input as a string, run a loop from start to the length of the string and increase the sum with that character(in this case it is numeric)

Follow the below steps to solve the problem:

  • Declare a variable sum equal to zero
  • Run a loop from zero to the length of the input string
    • Add the value of each character into the sum, by converting the character into it’s integer value
  • Return sum

Below is the implementation of the above approach:

C++14

#include

using namespace std;

int getSum(string str)

{

    int sum = 0;

    for (int i = 0; i < str.length(); i++) {

        sum = sum + str[i] - 48;

    }

    return sum;

}

int main()

{

    string st = "123456789123456789123422";

    cout << getSum(st);

    return 0;

}

Java

import java.io.*;

class GFG {

    static int getSum(String str)

    {

        int sum = 0;

        for (int i = 0; i < str.length(); i++) {

            sum = sum + str.charAt(i) - 48;

        }

        return sum;

    }

    public static void main(String[] args)

    {

        String st = "123456789123456789123422";

        System.out.print(getSum(st));

    }

}

Python3

def getSum(n):

    sum = 0

    for i in n:

        sum = sum + int(i)

    return sum

if __name__ == "__main__":

    n = "123456789123456789123422"

    print(getSum(n))

C#

using System;

public class GFG {

    static int getSum(String str)

    {

        int sum = 0;

        for (int i = 0; i < str.Length; i++) {

            sum = sum + str[i] - 48;

        }

        return sum;

    }

    static public void Main()

    {

        String st = "123456789123456789123422";

        Console.Write(getSum(st));

    }

}

PHP

function getsum($str)

{

      $sum = 0;

        for ($i = 0; $i<strlen($str); $i++) {

            $sum = $sum + (int)$str[$i];

        }

    return $sum;

}

$str = "123456789123456789123422";

echo(getsum($str));

?>

Javascript

Time Complexity: O(log N)
Auxiliary Space: O(1)

Sum of the digits of a given number using tail recursion:

Follow the below steps to solve the problem:

  • Add another variable “Val” to the function and initialize it to ( Val = 0 )
  • On every call to the function add the mod value (n%10) to the variable as “(n%10)+val” which is the last digit in n. Along with passing the variable n as n/10. 
  • So on the First call, it will have the last digit. As we are passing n/10 as n, It follows until n is reduced to a single digit. 
  • n<10 is the base case so When n < 10, then add the n to the variable as it is the last digit and return the val which will have the sum of digits

Below is the implementation of the above approach:

C++

#include

using namespace std;

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);

}

int main()

{

    int num = 12345;

    int result = sum_of_digit(num, 0);

    cout << "Sum of digits is " << result;

    return 0;

}

C

#include

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);

}

int main()

{

    int num = 12345;

    int result = sum_of_digit(num, 0);

    printf("Sum of digits is %d", result);

    return 0;

}

Java

import java.io.*;

import java.lang.*;

import java.util.*;

class sum_of_digits {

    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);

    }

    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

def sum_of_digit(n, val):

    if (n < 10):

        val = val + n

        return val

    return sum_of_digit(n // 10, (n % 10) + val)

if __name__ == "__main__":

    num = 12345

    result = sum_of_digit(num, 0)

    print("Sum of digits is", result)

C#

using System;

class GFG {

    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);

    }

    public static void Main()

    {

        int num = 12345;

        int result = sum_of_digit(num, 0);

        Console.Write("Sum of digits is " + result);

    }

}

Javascript

Output

Sum of digits is 15

Time Complexity: O(log N)
Auxiliary Space: O(log N)

Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the same problem.


How do you sum a 3 digit number in Python?

Sum of Digits Program in Python.
Take the value of the integer and store in a variable..
Using a while loop, get each digit of the number and add the digits to a variable..
Print the sum of the digits of the number..

How do you find the middle digit of a three digit number?

Find if 0 is removed more or 1 by deleting middle element if consecutive triplet is divisible by 3 in given Binary Array. ... .
Count numbers in a range with digit sum divisible by K having first and last digit different. ... .
Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B..

How do you find the sum of the digits of a number in Python?

27 comments on “Python Program to Find Sum Of Digits Of a Number”.
abhishekrajhans5001. #sum of digits of a number. sod=0. ... .
Bharath. n = [int(d) for d in input('Enter the number : ')] print('Sum of digits of a number : ',sum(n)) ... .
Bharath. n = [int(d) for d in input('Enter the number : ')] print('the sum of digits is : ',sum(n)).

How do you print the middle digit of a number in Python?

Answer.
Question:- Write a python program to find the middle digit of a 5-digit number..
Program:- n=int(input("Enter a 5 digit number: ")) if n>=10000 and n<=99999: d=n//100. d=d%10. print("Middle digit is: ",d) else: ... .
Logic:- Consider a number, say 12345. It is a five digit number. So, d=n//100=12345//100=123. Now, d=d%10..