Hello C# Corner members,
I have to write a program that sums up whole numbers with loop function.
So it needs to do these:
1. ask the user for how many whole numbers to add
2. lets the user give the numbers. Read each of the numbers into the program.
3.sum up the numbers and show the results.
Here is the visual of what it should look like (The orange writings input by user) :
----------------------------------------------------------------------------------------------------------------------------------
******Summation of whole numbers***********
Number of values to sum? : 3
Please give the value #1(whole number) : 500
Please give the value #2(whole number) : -250
Please give the value #3(whole number) : 150
--------------------------------------------------------------
The sum is 400
------------------------------------------------------------------------------------------------------------------------------------
So far, I have these:
private int numOfInput;
private int sum;
public void Start()
{
WriteProgramInfo();
ReadInput();
SumNumbers();
ShowResults();
}
private void ReadInput()
{
Console.Write("Number of values to sum? ");
numOfInput = int.Parse(Console.ReadLine());
Console.WriteLine(); }
private void WriteProgramInfo()
{
Console.WriteLine("\n\n <>");
Console.WriteLine(" Using a for-statement\n");
Console.WriteLine();
}
private void SumNumbers()
{
int index;
int num = 0;
for (index = 0; index < numOfInput; index++)
{//Calculation part here I Think?
}}
private void ShowResults()
{
Console.WriteLine("---------------------------\n");
Console.WriteLine("The sum is \t{0}", sum);
}
}
}
Ramesh MaruthiPosted Sep 23, 2014, 10:53 AM
{
static void Main(string[] args)
{
Program p = new Program();
p.Start();
}
private int numOfInput;
private int sum;
public void Start()
{
WriteProgramInfo();
ReadInput();
SumNumbers();
ShowResults();
}
private void WriteProgramInfo()
{
Console.WriteLine("******Summation of Whole Numbers******");
Console.WriteLine(" Using a for-statement\n");
Console.WriteLine();
}
private void ReadInput()
{
Console.Write("Number of values to sum? ");
numOfInput = int.Parse(Console.ReadLine());
Console.WriteLine();
}
private void SumNumbers()
{
for (int index = 0; index < numOfInput; index++)
{
Console.Write("Please give the value#{0}(whole number) :", index+1);
int a1 = int.Parse(Console.ReadLine());
sum += a1;
}
}
private void ShowResults()
{
Console.WriteLine("---------------------------\n");
Console.WriteLine("The sum is \t{0}", sum);
Console.Read();
}
}
Ramesh MaruthiPosted Sep 24, 2014, 6:29 PM
Song LeePosted Sep 24, 2014, 10:36 AM