Suppose there is a sequence of numbers [0,1,2,3,4..n] . How can it be set, after which each number should be raised to a certain degree (for example, the second)?
Those. the screen should display a response of the form [0,1,4,9...] .

I understand this is working with arrays?

  • This is a work with cycles, and on msdn is a ready-made example. Search Math.Pow - Arkadiy

2 answers 2

The result can be displayed on the screen without using an array. Namely, iterate through successive numbers in a cycle, raise them to the desired degree and display the result immediately on the screen.

For exponentiation, the Math.Pow method is Math.Pow .

 int n = 10; int power = 2; for (int i = 0; i < n; i++) { Console.Write(Math.Pow(i, power) + " "); } 

If the resulting numbers are needed for something else, then it really makes sense to store them in an array:

 int n = 10; int power = 2; double[] result = new double[n]; for (int i = 0; i < n; i++) { result[i] = Math.Pow(i, power); } Console.WriteLine(string.Join(" ", result)); 
     //Π—Π°Π΄Π°Ρ‘Ρ‚Π΅ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ Π² Π²ΠΈΠ΄Π΅ массива цСлочислСнных ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ…, инициализируя массив ΠΏΡ€ΠΈ объявлСнии var numberArray = new int[] { 0,1,2,3,4,5,6 }; //Ρ‚.ΠΊ. массив Ρ€Π΅Π°Π»ΠΈΠ·ΡƒΠ΅Ρ‚ IEnumerable, ΠΌΠΎΠΆΠ½ΠΎ Π²ΠΎΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒΡΡ Ρ†ΠΈΠΊΠ»ΠΎΠΌ foreach foreach (var number in numberArray) { //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π² консоль Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ ΠΈΠ· ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ, ΠΏΡ€Π΅Π΄Π²ΠΎΡ€ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ возводя Π΅Ρ‘ Π²ΠΎ Π²Ρ‚ΠΎΡ€ΡƒΡŽ ΡΡ‚Π΅ΠΏΠ΅Π½ΡŒ ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ Math.Pow() Console.Write(Math.Pow(number, 2) + " "); } 
    • well and how to work with large numbers? I thought it would just be possible to do it like this var numberArray = new int[] { 0, 1, 2, 3, 4, 5, 6 }; foreach (var number in numberArray) { Console.Write(BigInteger.Pow(number, n) + " "); } var numberArray = new int[] { 0, 1, 2, 3, 4, 5, 6 }; foreach (var number in numberArray) { Console.Write(BigInteger.Pow(number, n) + " "); } var numberArray = new int[] { 0, 1, 2, 3, 4, 5, 6 }; foreach (var number in numberArray) { Console.Write(BigInteger.Pow(number, n) + " "); } where n is a large number, but this is not the case, it gives an error that it cannot be converted from "System.Numerics.BigInteger" to "int" . Forgive me for asking maybe such stupid questions, but I'm just not good at it (( - Cocaine Snow
    • It is enough to replace int with long everywhere. - klutch1991