while loop statement in C#

 

For Example: Program to demonstrate using while loop statement.

(Calculate the addition of digits using while loop. For example if the user enters 1234 then it should display the result as 10.)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication4

{

class Program

    {

static void Main(string[] args)

        {

int x;

int no;

int result = 0;

Console.Write("Enter any number:");

            no = Convert.ToInt32(Console.ReadLine());

while (no > 0)

            {

                x = no % 10;

                result = result + x;

                no = no / 10;

            }

Console.WriteLine("Addition of digits is:{0}", result);

Console.ReadLine();

 

        }

    }

}

Output:

Comments

Popular posts from this blog

simple program C#

Regular Expression Validator Control in asp.net

two dimension array in C#