To find sum of number using For loop in C#


To find the sum of a number using For loop in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MYPROJECT.startup
{
    class Startupday1
    {
        static void Main()
        {

            Console.Write("enter number");

            int n = int.Parse(Console.ReadLine());
            int sum = 0;
            int rem;
            
           for(n=n;n>0;n=n/10)
            {
                rem = n % 10;
                sum = sum + rem;
                }
           
            Console.Write("sum of digits are {0}",sum);
            Console.ReadKey();
        }
    }

}



Comments

Popular posts from this blog

Printing Hello World in C#

Passing value without reference in C#

Input and output in C#