dd inputs in runtime and add, print total using params key

Mar 17 2014 7:22 AM
Please Check the following code
 
Here I want to add inputs in runtime and add them and print total at the end using Params Keyword 
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParamsDemo
{
class DemoTest
{
public int Add(params int[] adds)
{
int sum = 0;
for (int i = 0; i < adds.Length; i++)
{
sum += adds[i];
}
return sum;
}
}
class Demo2
{
static void Main(string[] args)
{
DemoTest test = new DemoTest();
int size;
//In Static Method
/*
* int total = test.Add(25, 33, 88, 99, 44, 55);
Console.WriteLine("Result Obtained is "+total);*/
//In Dynamical Method
Console.WriteLine("Enter the No of Inputs to add");
size = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the {0} inputs",size);
for (int i = 0; i < size; i++)
{
Console.ReadLine();
}
int total = test.Add(size);
Console.WriteLine("Total Obtained "+total);
}
}
}

Answers (1)