C# tutorial Loops-sum input values

sum input values



Let see other C# code examples of using loops--for loop, while loop, and do while loops to enable a user to input a number of data points and then calculate the total of the data values. We provide three separate C# code examples to solve the same problem. One C# code example is using for loop and the two others are using while loop , and do while loop.

Example 1: C# code using for loop to sum all elements of an integer array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{

  sumValues();
  Console.ReadLine();


}

static void sumValues()
{


  int sum = 0,val,n, i;
  Console.Write("Enter number of data points:");
  n = int.Parse(Console.ReadLine());
  for (i = 0; i < n; i++)
 
{
   Console.Write("Values " + i+":");
   val = int.Parse(Console.ReadLine());
   sum += val;
}

 Console.WriteLine("Total:" + sum);

}

Example 2: C# code using while loop to sum all elements of an integer array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
  class Program
 
{

    static void Main(string[] args)
 
{
     sumValues();
     Console.ReadLine();

}

  static void sumValues()
 
{
    int sum = 0, val,n, i=0;
    Console.Write("Enter number of data points:");
    n = int.Parse(Console.ReadLine());
    while (i<n)
  
{
     Console.Write("Values " + i+":");
     val = int.Parse(Console.ReadLine());
     sum += val;
     i += 1;
   
}

   Console.WriteLine("Total:" + sum);

}

Example 3: C# code using do while loop to sum all elements of an integer array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
  class Program
 
{

  static void Main(string[] args)
 
{
    sumValues();
    Console.ReadLine();

}

  static void sumValues()
 
{
   int sum = 0, val,n, i=0;
   Console.Write("Enter number of data points:");
   n = int.Parse(Console.ReadLine());
  do
 
{
    Console.Write("Values " + i + ":");
    val = int.Parse(Console.ReadLine());
    sum += val;
    i += 1;
  }while (i < n);

 Console.WriteLine("Total:" + sum);

}





Comments

Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like