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

CAPTCHA image
KrishnaMohan comment

 KrishnaMohan

Check this also for c# interview questions
http://skillgun.com/csharp/interview-questions-and-answers


2014-07-23 07:39:00
uzmirah comment

 uzmirah

Can you show a simple example of a program which user input is prompted for 2 numbers, also prompting the user input for different math functions , using the while loop, showing the end result for input sums. so for each addition, subtraction, division, multiplication answers are displayed.


2014-06-03 18:44:00



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.