Filip785
Filip785

Reputation: 101

How to multiply all elements from array?

I am beginning to learn C# and am writing a program that will first ask a user to enter a list of numbers. When the user finishes entering the input, I would like to square every number the user provided in the input. An example of user input is 2 3 5.

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program
{
    class Third
    {
        public static void Main()
        {
            Console.WriteLine("Enter how much numbers");

            int howMuch = int.Parse(Console.ReadLine());
            int[] num = new int[howMuch];
            int sum = 0;

            for (int i = 0; i < num.Length; i++ )
            {
                sum = num[i] * num[i]; // this is what i did but it does not work?
            }

            Console.WriteLine(sum);
            Console.ReadLine();
        }
    }
}

Specifically, I would first like the user input to be captured in the numbers array. And then I would like to square each number in the num array that was created. What's wrong with my program?

Upvotes: 0

Views: 6523

Answers (6)

jbutler483
jbutler483

Reputation: 24559

This might be a LITTLE better, although there is still quite a bit of tidying up to do!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program
{
class Third
{

    public static void Main()
    {

        Console.WriteLine("Enter how many numbers");

        int howMuch = int.Parse(Console.ReadLine());

        int[] num = new int[howMuch];

        int sum = 1;
        for(int i=0;i<num.Length;i++) //allows you to fill the array
        {
           Console.WriteLine("Please enter an integer");
            sum *= int.Parse(Console.ReadLine()); // this will now multiply the value to sum, as your comment suggests
        }

        Console.WriteLine(sum);

        Console.ReadLine();

    }

}
}

EDIT

sum *= num[i];

should do what you want!

Upvotes: 0

Yann
Yann

Reputation: 988

I've assumed that the sum that you want to do (if the input is 2,3,5) is 2*3*5. If this is the case, then naming your variable sum is a little misleading as that would tend to imply 2+3+5.

The for loop where you multiply the numbers had the line

sum = num[i]*num[i];

Which, following with the example, when i == 0, would do sum = 2*2, and then overwrite it as you increment the loop, so sum would end at being 25 (5*5), and discount all other values.

If you did want to sum the squares of the numbers, initializing sum to 0 and then using the line

sum += num[i] * num[i];

would work. Having said that, unless you specifically need to store it for any reason, processing the values when they are read would be better, as the program would have 1 fewer for loop.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program
{
class Third
{

public static void Main()
{

    Console.WriteLine("Enter how much numbers");

    int howMuch = int.Parse(Console.ReadLine());

    int[] num = new int[howMuch];
    for(int i = 0; i < howMuch; ++i)
    {
        //This is assuming that the user will enter an int value.
        //Ideally, verify this using int.TryParse or something similar.
        num[i] = int.Parse(Console.ReadLine());
    }

    int sum = 1; //Set to 1 so that the PRODUCT is not always zero

    for (int i = 0; i < num.Length; i++ )
    {
        sum *= num[i]; //Multiply the value
    }

    Console.WriteLine(sum);

    Console.ReadLine();

}

}

If the input is 2, 3, 5, the value in sum will be 30

Upvotes: 0

vasja
vasja

Reputation: 4792

Your code does not initialize the array - I added

Console.WriteLine("Enter number " + (i + 1));
        num[i] = int.Parse(Console.ReadLine());

for that.

Also corrected the summarisation: sum += ...

for (int i = 0; i < num.Length; i++ )
{
    Console.WriteLine("Enter number " + (i + 1));
    num[i] = int.Parse(Console.ReadLine());
    sum += (num[i] * num[i]);
}

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101732

First, you need to get input from user and fill the array:

for (int i = 0; i < num.Length; i++)
{
     //TODO: Look into int.TryParse method to validate user input
     num[i] = int.Parse(Console.ReadLine());
}

And instead of overwriting sum use sum += num[i] * num[i] in your second loop. Or if you are looking for the multiplication of all numbers just use sum = sum * num[i]; and start sum from 1.

Upvotes: 1

Wyatt Earp
Wyatt Earp

Reputation: 1823

Take a look at the math that you are doing in the loop.

sum = num[i] * num[i];

Each time through, you're setting sum equal to the square of the indexed integer.

Taking your example of 2, 3, 5, the first time through the loop, you will set sum = 2 * 2 (or 4), the second time through, you'll set sum = 3 * 3 (or 9) and the last time it will be sum = 5 * 5 (or 25). What you really want is 2 * 3 * 5, right?

All you would need to do is to initialize int sum = 1, and change the statement in your loop to be:

sum = sum * num[i];

This will yield sum = 1 * 2 the first time through, sum = 2 * 3 the second time through, and sum = 4 * 5 the third time through.

Upvotes: 0

Shouvik Roy
Shouvik Roy

Reputation: 54

replace the code in Main() with this :-

    Console.WriteLine("Enter how much numbers");

    int howMuch = int.Parse(Console.ReadLine());

    int[] num = new int[howMuch];

    Console.WriteLine("Enter numbers");

    int sum = 0;

    for (int i = 0; i < num.Length; i++ )
    {
        num[i] = int.Parse(Console.ReadLine());
        sum += num[i] * num[i]; // this is what i did but it does not work?
    }

    Console.WriteLine(sum);

    Console.ReadLine();

Upvotes: -1

Related Questions