0

I'm learning how to program using c#. I'm really new to this. My question is I'm trying to create an array that shows 10 numbers. I want my code to check which numbers below 10 are divisible for 3 or 5 and sum the total. I've tried to use the .Sum() function but says int doesn't contain a definition for Sum. I've put using System.Linq on my program. Does anyone have an idea how to make this sum happens?

        {

            int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int sum = 1 + 2;
            

            foreach (int n in numbers)
            {
                if (n % 3 == 0 || n % 5 == 0)
                {
                    int total = n.Sum();
                     Console.WriteLine(total);
                }
                else
                {
                    Console.WriteLine("not divisible");
                }
            }`
   
2
  • 1
    Looks like a FizzBuzz sort of assignment. It's not clear what exactly led you to the code you have now. If you want the sum of the whole array, why would you try to compute it and print it during the loop over the array elements? If you are allowed to use LINQ (which Sum() is a part of), then you may want, instead of the loop, a call to Where(). But it's not entirely clear what the final goal is here, nor what kind of help would be best for this apparent homework assignment. Commented Jul 28, 2021 at 23:53
  • Hi @Yorda, please remember to accept an answer and upvote if it solves the problem. Commented Jul 30, 2021 at 8:13

3 Answers 3

1

So your problem is that you are trying to call .Sum() on a variable n which is of type int (you define it here: foreach (int n in numbers), and that is not a method.

Using LINQ you could do something like:

var numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var total = numbers.Where(n => n % 3 == 0 || n % 5 == 0).Sum();
Sign up to request clarification or add additional context in comments.

1 Comment

.Sum() is a LINQ extension method defined here: learn.microsoft.com/en-us/dotnet/api/…
0

If I understand it correct and using your code this is what you want.

        int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int sum = 1 + 2;
        int total=0;

        foreach (int n in numbers)
        {
            if (n % 3 == 0 || n % 5 == 0)
            {
                int total += n;
            }
            else
            {
                Console.WriteLine("not divisible");
            }
        }
        Console.WriteLine(total);

I moved the printing out to after the foreach so you get one result when it is done

Comments

0

Everybody forgot that array should contain 10 numbers but some numbers values maybe greater than 10. Only number values below 10 should be checked. So a right answer should be

var numbers = new int[] { 1, 9, 5, 6, 8, 10,4, 15, 25, 3};
var total = numbers.Where( n => (n<10) && ( n % 3 == 0 || n % 5 == 0)).Sum();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.