C# Program to Find the Sum of Numbers in a String

This is a C# Program to read a string and find the sum of all digits in the string.

Problem Description

This C# Program Reads a String and find the Sum of all Digits in the String.

Problem Solution

Here the program accepts a character string, then add all the character’s integer value, thereby summing all the digits of a string.

Program/Source Code

Here is source code of the C# Program to Print the Sum of all the Multiples of 3 and 5. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Read a String and find the Sum of all Digits in the String
 */
using System;
class program
{
    public static void Main()
    {
        char[] string1 = new char[20];
        int count, nc = 0, sum = 0, n, i;
        Console.WriteLine("Enter the Length of the sentence  :");
        n = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the string1 containing both digits and alphabet :");
        for (i = 0; i < n; i++)
        {
            string1[i] = Convert.ToChar(Console.Read());
        }
 
        for (count = 0; string1[count] != '\0'; count++)
        {
            if ((string1[count] >= '0') && (string1[count] <= '9'))
            {
                nc += 1;
                sum += (string1[count] - '0');
            }
        }
        Console.WriteLine("NO. of Digits in the string1 = {0}", nc);
        Console.WriteLine("Sum of all digits = {0}", sum);
        Console.ReadLine();
        Console.ReadLine();
    }
}
Program Explanation

In this C# program, we are reading the length of the sentence using ‘n’ variable. Using for loop we are splitting the string in digits and alphabet as separate using ToChar() function.

advertisement

Another for loop is used to check that value of ‘string1[]’ variable is greater than or equal to 0 and less than or equal to 9 using Logical AND operators. If the condition is true then execute the statement. Then add all the character’s integer value, thereby summing all the digits of a string. Print the sum of all digits in the string.

Runtime Test Cases
 
Enter the Length of the sentence  :
6
Enter the string1 containing both digits and alphabet :
SAN193
NO. of Digits in the string1 = 3
Sum of all digits = 13

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Free 30-Day Python Certification Bootcamp is Live. Join Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.