# Find the single digit sum of alphabetical values of a string

• Last Updated : 11 Nov, 2021

Given string S of size N, the task is to find the single-digit sum by the repetitive sum of digits of the value obtained by the sum of order of all alphabets in the given string.

The order of alphabets is given by the position at which they occur in English Alaphabets.

Examples:

Input: S = “geek”
Output: 1
Explanation:
The value obtained by the sum order of alphabets is 7 + 5 + 5 + 11 = 28.
The single digit sum obtained by sum of 28 = 2 + 8 = 10 = 1 + 0 = 1.

Input: S = “GeeksforGeeks”
Output: 7

Approach: The given problem can be solved by first find the sum of orders of all the alphabets present in the given string S by adding the value (S[i] – ‘a’ + 1) or (S[i] – ‘A’ + 1) if S[i] is lowercase or uppercase character. After finding the value of sum, find the single digit of this value using the approach discussed in this article.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `int` `findTheSum(string str)``{``    ``string alpha;` `    ``// Traverse the given string``    ``for` `(``int` `i = 0; i < str.length(); i++) {` `        ``// If character is an alphabet``        ``if` `((str[i] >= ``'A'` `&& str[i] <= ``'Z'``)``            ``|| (str[i] >= ``'a'` `&& str[i] <= ``'z'``))``            ``alpha.push_back(str[i]);``    ``}` `    ``// Stores the sum of order of values``    ``int` `score = 0, n = 0;` `    ``for` `(``int` `i = 0; i < alpha.length(); i++) {` `        ``// Find the score``        ``if` `(alpha[i] >= ``'A'` `&& alpha[i] <= ``'Z'``)``            ``score += alpha[i] - ``'A'` `+ 1;``        ``else``            ``score += alpha[i] - ``'a'` `+ 1;``    ``}` `    ``// Find the single digit sum``    ``while` `(score > 0 || n > 9) {``        ``if` `(score == 0) {``            ``score = n;``            ``n = 0;``        ``}``        ``n += score % 10;``        ``score /= 10;``    ``}` `    ``// Return the resultant sum``    ``return` `n;``}` `// Driver Code``int` `main()``{``    ``string S = ``"GeeksforGeeks"``;``    ``cout << findTheSum(S);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG{` `static` `int` `findTheSum(``char` `[]str)``{``    ``String alpha=``""``;` `    ``// Traverse the given String``    ``for` `(``int` `i = ``0``; i < str.length; i++) {` `        ``// If character is an alphabet``        ``if` `((str[i] >= ``'A'` `&& str[i] <= ``'Z'``)``            ``|| (str[i] >= ``'a'` `&& str[i] <= ``'z'``))``            ``alpha+=(str[i]);``    ``}` `    ``// Stores the sum of order of values``    ``int` `score = ``0``, n = ``0``;` `    ``for` `(``int` `i = ``0``; i < alpha.length(); i++) {` `        ``// Find the score``        ``if` `(alpha.charAt(i) >= ``'A'` `&& alpha.charAt(i) <= ``'Z'``)``            ``score += alpha.charAt(i) - ``'A'` `+ ``1``;``        ``else``            ``score += alpha.charAt(i) - ``'a'` `+ ``1``;``    ``}` `    ``// Find the single digit sum``    ``while` `(score > ``0` `|| n > ``9``) {``        ``if` `(score == ``0``) {``            ``score = n;``            ``n = ``0``;``        ``}``        ``n += score % ``10``;``        ``score /= ``10``;``    ``}` `    ``// Return the resultant sum``    ``return` `n;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``String S = ``"GeeksforGeeks"``;``    ``System.out.print(findTheSum(S.toCharArray()));``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python program for the above approach``def` `findTheSum(``str``):` `    ``alpha ``=` `""` `    ``# Traverse the given string``    ``for` `i ``in` `range``(``0``, ``len``(``str``)):` `                ``# If character is an alphabet``        ``if` `((``str``[i] >``=` `'A'` `and` `str``[i] <``=` `'Z'``) ``or` `(``str``[i] >``=` `'a'` `and` `str``[i] <``=` `'z'``)):``            ``alpha ``+``=` `str``[i]` `        ``# Stores the sum of order of values``    ``score ``=` `0``    ``n ``=` `0` `    ``for` `i ``in` `range``(``0``, ``len``(alpha)):` `                ``# Find the score``        ``if` `(alpha[i] >``=` `'A'` `and` `alpha[i] <``=` `'Z'``):``            ``score ``+``=` `ord``(alpha[i]) ``-` `ord``(``'A'``) ``+` `1` `        ``else``:``            ``score ``+``=` `ord``(alpha[i]) ``-` `ord``(``'a'``) ``+` `1` `        ``# Find the single digit sum``    ``while` `(score > ``0` `or` `n > ``9``):``        ``if` `(score ``=``=` `0``):``            ``score ``=` `n``            ``n ``=` `0` `        ``n ``+``=` `score ``%` `10``        ``score ``=` `score ``/``/` `10` `        ``# Return the resultant sum``    ``return` `n` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``S ``=` `"GeeksforGeeks"``    ``print``(findTheSum(S))` `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG {``    ``static` `int` `findTheSum(``string` `str)``    ``{``        ``string` `alpha = ``""``;` `        ``// Traverse the given string``        ``for` `(``int` `i = 0; i < str.Length; i++) {` `            ``// If character is an alphabet``            ``if` `((str[i] >= ``'A'` `&& str[i] <= ``'Z'``)``                ``|| (str[i] >= ``'a'` `&& str[i] <= ``'z'``))``                ``alpha += (str[i]);``        ``}` `        ``// Stores the sum of order of values``        ``int` `score = 0, n = 0;` `        ``for` `(``int` `i = 0; i < alpha.Length; i++) {` `            ``// Find the score``            ``if` `(alpha[i] >= ``'A'` `&& alpha[i] <= ``'Z'``)``                ``score += alpha[i] - ``'A'` `+ 1;``            ``else``                ``score += alpha[i] - ``'a'` `+ 1;``        ``}` `        ``// Find the single digit sum``        ``while` `(score > 0 || n > 9) {``            ``if` `(score == 0) {``                ``score = n;``                ``n = 0;``            ``}``            ``n += score % 10;``            ``score /= 10;``        ``}` `        ``// Return the resultant sum``        ``return` `n;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``string` `S = ``"GeeksforGeeks"``;``        ``Console.WriteLine(findTheSum(S));``    ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``

Output:

`7`

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

My Personal Notes arrow_drop_up