C# program to find sum of even and odd number

 Algorithm:-

(1)start

(1)take three variables range,totaleven,totalodd and insert initial values of total even and total odd is zero

(2)take number from user and store in variable range

(3)Divide all number by 2 from 0 to range-1

(4)if number is divisible by 2 then add those number to totaleven variable

Else add those number to totalod variable

(5)print the value of totaleven and total odd

(6)Stop



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace TestApplicationOnly

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                int range, totaleven = 0, totalodd = 0;

            Console.Write("Input the range to print all even and odd number :");

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

                Console.Write("All even numbers are :");

                Console.WriteLine("\t All odd numbers are :");

                for (int n = 0; n < range; n++)

                {

                    if (n % 2 == 0)

                    {

                        Console.Write(" " + n);

                        totaleven = totaleven + n;

                    }

                    else

                    {

                        Console.WriteLine(" \t\t\t" + n);

                        totalodd = totalodd + n;

                    }

                }


                Console.WriteLine("\nSum of all even numbers are :" + totaleven);

                Console.WriteLine("\nSum of all odd number are :" + totalodd);

                Console.ReadKey();

            }

            catch (Exception ex)

            {

                Console.WriteLine("input is not in correct formate");

            }

            Console.ReadKey();  

        }

    }

}


Input the range to print all even and odd number :8

All even numbers are :   All odd numbers are :

 0                                        1

 2                                        3

 4                                       5

 6                                       7


Sum of all even numbers are :12

Sum of all odd number are :16

एक टिप्पणी भेजें

0 टिप्पणियाँ