Skip to main content

How to find sum of the digits of a number in c#?

Sum of the Digits of a number

Program:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, sum = 0;
            Console.WriteLine("Enter a number :\n");
            num = Convert.ToInt32(Console.ReadLine());
            if (num > 0)
            {
                for (int temp = num; temp != 0; temp /= 10)
                    sum += temp % 10;
                Console.WriteLine("Sum of the digits of  " + num + " is : " + sum);
            }
            else
                Console.WriteLine("Enter A valid Number :\n");
            Console.ReadKey();
        }

    }
}
  
Output:

Enter a number :
151
Sum of the digits of  151 is : 7

Comments

Popular posts from this blog

How to Create Login Page with Material Design !

Material Design for web application In this article I’m going to show you how to how to apply material design in web application.  we will take  materialize CSS framework to design our login page .   Materialize CSS is free open source frontend designing framework. It provides material design.   It is like a bootstrap framework , but we can do many thing using this framework. < link href ="https://fonts.googleapis.com/icon?family=Material+Icons" rel ="stylesheet" />   < link rel ="stylesheet" href ="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" />   < script type ="text/javascript" src ="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></ script >   < script type ="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></ s...

How to check a number Prime or not in c# ?

/*Check a number Prime or not in C# */ Program : using  System; using  System.Collections.Generic; using  System.Linq; using  System.Text; using  System.Threading.Tasks; namespace  Primenumber {      class   Program     {          static   void  Main( string [] args)         {              int  num, flag = 0;              Console .WriteLine( "Enter A number :" );             num =  Convert .ToInt32( Console .ReadLine());              if  (num > 0)             {  ...

Date picker in asp.net !!!

Date Picker Using Bootstrap and ASP.net <html> <head> <title>Date Picker Using Bootstrap and ASP.net</title> <link href="../bootstrap-3.3.7-dist/bootstrap-datepicker/css/bootstrap-datepicker.css" rel="stylesheet" type="text/css" /> </head>      <body>                 <form ID="Form1" runat="server">                 <div class="col-md-4 col-xs-6 col-xs-offset-3 col-md-offset-0">                     <div class="form-group">                         <div class="has-feedback">      ...