Write a C# program to create a function to calculate the sum of the individual digits of a given number


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2.  
  3. public class functionexcercise
  4. {
  5. public static int SumCal(int number)
  6. {
  7. string n1 = Convert.ToString(number);
  8. int sum = 0;
  9. for (int i = 0; i < n1.Length; i++)
  10. sum += Convert.ToInt32(n1.Substring(i, 1));
  11. return sum;
  12. }
  13.  
  14. public static void Main()
  15. {
  16. int num;
  17. Console.Write("Enter a number: ");
  18. num = Convert.ToInt32(Console.ReadLine());
  19. Console.WriteLine("The sum of the digits of the number {0} is : {1} \n", num, SumCal(num));
  20. Console.ReadLine();
  21. }
  22. }

Result

C# language techstudy

     





Related Topics




Popular Posts