DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

write a .NET program to find the sum of odd-positioned digits and even-positioned digits of a given number

By Ashok Nalam on 18 Dec 2013 | Category: C# | Tagged: .NET number 
The program finds the sum of odd-positioned digits and even-positioned digits of a given number using C#.NET example.
  Discuss Add Comment   |  
Add rating Rate this resource   

Introduction:

The program finds the sum of odd-positioned digits and even-positioned digits of a given number using C#.NET example.

E.g: If you enter number as 23456, so even number are 2,4,6  & Odd numbers are 3,5 and expected output should be 
SumOfEven = 2+4+6 =12 
SumOfOdd = 3+5 =8

Program:

using System;
namespace DotNetMirror
{
    class SumOfOddEvenDigitsOfNumber
    {
        static void Main()
        {
            Console.Write("Enter number to find sum of Even/Odd digits: ");
            int number = int.Parse(Console.ReadLine());
            int orginalNumber = number;
            int sumEven = 0; int sumOdd = 0;
            int tempNumber = 0;
            while (number != 0)
            {
                tempNumber = number % 10;
                if (tempNumber % 2 == 0)
                {
                    sumEven = sumEven + tempNumber;
                }
                else
                {
                    sumOdd = sumOdd + tempNumber;
                }
                number = number / 10;
            }
            Console.WriteLine("Sum of Event numbers in '{0}' is:{1}", orginalNumber, sumEven);
            Console.WriteLine("Sum of odd numbers in '{0}' is:{1}", orginalNumber, sumOdd);
            Console.ReadKey();
        }
    }
}

Ouput:

SumOfOddEvenDigitsOfNumber
  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 8586  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.