In Visual Studio, start a new C# Windows form project and call it doingMath. On the form, put a button, a label and four textBoxes. Name the label myanswer, the button Run, and call the textBoxes num1, num2, num3 and num4. Instantiate an array of type double called myNumbers. When we did this in the lecture, it was done as integers: int[] myarray = new int[4]; The only thing here that changes is the type is to be "double". Double is a large decimal number. double[] myarray = new double[4]; Put the values from the textboxes in the array. For example myNumber[0] = Convert.ToDouble(num1.Text); Now use a for loop to sum the array and display the final total in myAnswer
copy and paste the entire code
Here is the solution
controls:-
1 label --------------- name : myanswer (text--------"Answer")
4 textboxes ---------------- names (num1,num2,num3,num4)
1 button ------------------- name run (text ---- "run")
code behind the button (run)
// your code start here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace additions
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Run_Click(object sender, EventArgs e)
{
// array declaration
double[] myarray = new double[4];
// sum to collect the total
double sum = 0;
// accepting values in array from textboxes
myarray[0] = Convert.ToDouble(num1.Text);
myarray[1] = Convert.ToDouble(num2.Text);
myarray[2] = Convert.ToDouble(num3.Text);
myarray[3] = Convert.ToDouble(num4.Text);
// looping over to access the values of the array
// and the calculate the total
for (int i = 0; i < myarray.Length; i++)
{
sum += myarray[i];
}
// finally displaying the output to lable myanswer
myanswer.Text = "Answer:" + sum.ToString();
}
}
}
// Code ends here
Here is the sample output:-
here is the screenshot of the program:-
Thank You.
Get Answers For Free
Most questions answered within 1 hours.