How to power array values by 2 using Linq in C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        
        var result = array.Aggregate(Enumerable.Empty<double>(), (total, next) =>
                            total.Append(Math.Pow(next, 2)));

        foreach (var val in result) {
            Console.Write(val + ", ");
        }
    }
}
   
   
   
   
/*
run:
      
1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 
    
*/

 



answered Jul 6, 2023 by avibootz
...