April 14, 2020

How to do sum numbers in C# using for loop

Write a program in C# Sharp for sum five numbers using for loop.


How to do sum numbers in C# using for loop


Like Sum of 5 number is 1+2+3+4+5 = 15

Sample Solution

C# Sharp Code

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Five number for sum number");
            int I, sumnum, number;
            for (sumnum = number = I = 0; I < 5; I += 1)
            {
                number = Convert.ToInt32(Console.ReadLine());


                if (number <= 0)
                    continue;
                sumnum = sumnum + number;
            }
            Console.WriteLine("The sum of number is {0}",sumnum);

            Console.ReadLine();
        }

    }
}

                                                          The Sum is: 15

How to do sum numbers in C# using for loop



No comments:

Post a Comment