Declare an integer variable size of 10 elements, input the elements. find the sum of the elements. display the elements. display sum

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int size, sum = 0;
Console.WriteLine("input 10 elements");
for (size = 0; size <= 9; size++)
{
sum = sum + size;
}
Console.WriteLine("The elements are");
}

}
}
``````

## All 21 Replies

i am not getting the correct solution after debugging. it only takes one element and display the wrong sum.

i have to give it tommorow so if u can help

You need to learn about arrays. I'm actually shocked that you left this assignment until the last minute if your knowledge is this basic.

1. Create an array
2. Iterate the array and take in your ten values, placing each one into the array at a different position
3. Sum all the numbers in the array
4. Print it all out

Advice: If you don't know how to do something, don't leave it until the last minute. That's just ridiculous. It won't go away if you ignore it o.O

ok please wait i will try and show

You don't need an array to sum numbers that are coming in sequentially, but you do need to accept those numbers properly in turn and accumulate them in the sum:

``````int sum = 0;

for (int i = 0; i < 10; i++)
{
}

Console.WriteLine("Sum: {0}", sum);
``````

it shows sum as 0. it does not display the sum

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)

{
int[] arr=new int[10];
int size,sum=0;
Console.WriteLine("input 10 elements");
for (size = 0; size <= 9; size++)
{

}
Console.WriteLine("the sum of elements");

for (size = 0; size <= 9; size++)
sum=sum+size;

}
}

}
``````
``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)

{
int[] arr=new int[10];
int size,sum=0;
Console.WriteLine("input 10 elements");
for (size = 0; size <= 9; size++)
{

}
Console.WriteLine("the sum of elements");

for (size = 0; size <= 9; size++)

sum=sum+size;

}
}

}
``````

this program shows no sum

That's because you don't output the sum...ever. In fact, you undo all of the work of the program by overwriting the sum at the end.

please i am doing my best and did many times. please can u correct my program and tell my mistake please

If I do your work for you, you won't learn. I've already told you your mistake (you don't output the sum and you unnecessarily overwrite the sum at the end). Please refer to an earlier post of mine in this thread where I provided a correct and working snippet. Modifying it to use an array shouldn't be too difficult.

`````` {
int[] arr=new int[10];
int size,sum=0;
Console.WriteLine("input 10 elements");
for (size = 0; size <= 9; size++)
{

}
Console.WriteLine("the sum of elements are");

for (size = 0; size <= 9; size++)
{

sum = sum + size;
}
Console.WriteLine("sum:{0}", sum);

}
``````

now i displayed still i dont know why its not displaying

`sum = sum + size;`

This is meaningless, it has nothing to do with summing the values in the array.

`sum = Convert.ToInt32(Console.ReadLine());`

This is counterproductive, it destroys whatever value was already stored by sum. Look very carefully:

``````Console.WriteLine("the sum of elements are");

for (size = 0; size < 10; size++)
{
sum += arr[size];
}

Console.WriteLine("sum:{0}", sum);
``````

it does not wait. the screen goes off. so i am using getch(); it shows error and even for systemn("pause"); so what shoul i use so that window should wait and show the sum

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)

{
int[] arr=new int[10];
int size,sum=0;
Console.WriteLine("input 10 elements");
for (size = 0; size <= 9; size++)
{

}
Console.WriteLine("the sum of elements are");

for (size = 0; size <= 9; size++)
{

sum += arr[size];

}
Console.WriteLine("sum:{0}", sum);

}
}

}

this is the code which i changed according to your program
``````

so i am using getch(); it shows error and even for systemn("pause");

Neither of those are valid in C#, they're C/C++ functions. How are you running this program? Anyway, if you must do it, use Console.ReadKey(false) to simulate getch().

On a side note, if you're not willing to read the documentation before trying random things and then asking for help, I'm not willing to help you fix the mess you create. Keep that in mind and RTFM as a first step in troubleshooting any problem.

this is the code which i changed according to your program

Works for me.

oks thanks :) i will be more neat next time

`Console.Readline();` by itself will pause the console.

On a side note. Whenever you get input from a user a good habit to get into is to, validate what the user inputs. In your case I would suggest the following:

`````` static void Main(string[] args)
{
int[] arr=new int[10];
int size,sum=0;
bool GoodNumber = false;
Console.WriteLine("input 10 elements");
for (size = 0; size <= 9; size++)
{
While(!GoodNumber)
{
if(!GoodNumber)
Console.Writeline("Input must be only numbers\nPlease try again");
}
}
Console.WriteLine("the sum of elements are: ");
for (size = 0; size <= 9; size++)
{
sum += arr[size];
}
Console.WriteLine("{0}", sum);
}
``````

Console.Readline(); by itself will pause the console.

Any input request will pause the console, but if your goal is to 1) not pollute the console's output and 2) accept any key press for continuing, then ReadLine() isn't ideal because it echoes the characters you type and requires the Enter key to finalize the request.

The canonical getch() is what you'd want, and conveniently enough .NET standardizes that behavior with Console.ReadKey() and a false parameter to disable echo.

commented: I cannot do other then rep you +14

hmm ok thank you so much

He needs an array because part of the assignment is to display the ten numbers that have been input. :)

commented: Good catch, I missed that part of the assignment. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.