C# Program to Read a String and Find the Sum of all Digits Last Updated : 19 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string, our task is to first read this string from the user then find the sum of all the digits present in the given string. Examples Input : abc23d4 Output: 9 Input : 2a3hd5j Output: 10 Approach: To reada String and find the sum of all digits present in the string follow the following steps: First of all we read the string from the user using Console.ReadLine() method.Initialize a integer sum with value 0.Now iterate the string till the end.If the character value is greater than or equal to '0' and less than or equal to '9' (i.e. ascii value between 48 to 57) then perform character - '0' (this gives value of character) and add the value to the sum.Now the sum contains the value of sum of all the digits in the strings. Example: C# // C# program to read the string from the user and // then find the sum of all digits in the string using System; class GFG{ public static void Main() { string str; Console.WriteLine("Enter a string "); // Reading the string from user. str = Console.ReadLine(); int count, sum = 0; int n = str.Length; for(count = 0; count < n; count++) { // Checking if the string contains digits or not // If yes then add the numbers to find their sum if ((str[count] >= '0') && (str[count] <= '9')) { sum += (str[count] - '0'); } } Console.WriteLine("Sum = " + sum); } } Output 1:Enter a string abc23d4 Sum = 9 Output 2: Enter a string 2a3hd5j Sum = 10 Explanation: In the above example, first we read the string and we will iterate each character and check if the character is an integer or not by comparing the ASCII value of the character. If the character is an integer then add the value to the sum. At the end of the iteration, the sum variable will have the total sum of digits in the string. Comment More infoAdvertise with us Next Article isdigit() in C++ R raghu135ram Follow Improve Article Tags : C# CSharp-programs CSharp-Strings-Programs Similar Reads C# Program to Find Sum of Digits of a Number Using Recursion Given a number, we need to find the sum of digits in the number using recursion. In C#, recursion is a process in which a function calls itself directly or indirectly and the corresponding function is known as a recursive function. It is used to solve problems easily like in this article using recur 2 min read How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read How to Convert String to Number Given a string representation of a numerical value, convert it into an actual numerical value. In this article, we will provide a detailed overview about different ways to convert string to number in different languages. Table of Content Convert String to Number in CConvert String to Number in C++Co 5 min read isdigit() in C++ The isdigit() function in C++ checks whether a given character is a digit or not. Let's take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { char c = '5'; if (isdigit(c)) cout << c << " is a Digit"; else cout << c << " is not a Digit"; 3 min read C Program to Add 2 Binary Strings Given two Binary Strings, we have to return their sum in binary form.Approach: We will start from the last of both strings and add it according to binary addition, if we get any carry we will add it to the next digit.Input: 11 + 11Output: 110C// C Program to Add 2 Binary Strings // and Print their B 8 min read How to Write a Command Line Program in C? In C, we can provide arguments to a program while running it from the command line interface. These arguments are called command-line arguments. In this article, we will learn how to write a command line program in C. How to Write a Command Line Program in C? Command line arguments are passed to the 2 min read Like