in c# write a method in a mathclass that accepts variable number of integers.method should find the sum of all integers parsed and display the result.write a client program to call the method with variable number of integers.
1
Expert's answer
2012-11-06T10:12:25-0500
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace MathclassApplication { class Mathclass { public int[] numbers = new int[100]; public int number; public void inputNumber(int number) { this.number = number; for (int i = 0; i < number; i++) { Console.Write("Enter number "+ (i+1)+": "); numbers[i] = int.Parse(Console.ReadLine()); } } private double getSum() { double sum = 0; for (int i = 0; i < number; i++) { sum += numbers[i]; } return sum; }
public void ShowResult() { Console.Write("Thee sum is: " + getSum().ToString()); }
} }
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace MathclassApplication { class Program { static void Main(string[] args) { int number = 0; Mathclass Mathclass = new Mathclass(); Console.Write("Enter number of numbers: "); number = int.Parse(Console.ReadLine()); Mathclass.inputNumber(number); Mathclass.ShowResult(); Console.ReadLine(); } } }
Comments
Leave a comment