Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to find the cube of elements in a list
This program demonstrates how to find the cube of each element in a list using the Select() method with a lambda expression. The Select() method transforms each element in the collection by applying a specified function.
Syntax
Following is the syntax for using Select() method with lambda expression −
collection.Select(x => expression)
For calculating the cube of each element −
list.Select(x => x * x * x)
Using Select() Method to Calculate Cube
The Select() method applies a transformation function to each element in the list. In this case, we use a lambda expression c => c * c * c to calculate the cube of each element −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> list = new List<int> { 2, 4, 5, 7 };
Console.WriteLine("Elements...");
// Display initial list
foreach (int n in list)
Console.WriteLine(n);
// Calculate cube of each element
IEnumerable<int> res = list.Select(c => c * c * c);
Console.WriteLine("Cube of each element...");
foreach (int n in res)
Console.WriteLine(n);
}
}
The output of the above code is −
Elements... 2 4 5 7 Cube of each element... 8 64 125 343
Using Math.Pow() Method
Alternatively, you can use the Math.Pow() method to calculate the cube −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> numbers = new List<int> { 3, 6, 8, 10 };
Console.WriteLine("Original numbers:");
foreach (int num in numbers)
Console.WriteLine(num);
// Using Math.Pow to calculate cube
var cubes = numbers.Select(x => (int)Math.Pow(x, 3));
Console.WriteLine("Cubes using Math.Pow:");
foreach (int cube in cubes)
Console.WriteLine(cube);
}
}
The output of the above code is −
Original numbers: 3 6 8 10 Cubes using Math.Pow: 27 216 512 1000
Converting to List
Since Select() returns an IEnumerable, you can convert the result to a list using ToList() method −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> originalList = new List<int> { 1, 2, 3, 4, 5 };
// Convert result to List
List<int> cubesList = originalList.Select(x => x * x * x).ToList();
Console.WriteLine("Original: " + string.Join(", ", originalList));
Console.WriteLine("Cubes: " + string.Join(", ", cubesList));
Console.WriteLine("Count of cubes: " + cubesList.Count);
}
}
The output of the above code is −
Original: 1, 2, 3, 4, 5 Cubes: 1, 8, 27, 64, 125 Count of cubes: 5
Conclusion
The Select() method with lambda expressions provides a clean and efficient way to transform elements in a collection. You can calculate cubes using simple multiplication or the Math.Pow() method, and convert the result to a list when needed for further operations.
