Answer to Question #187129 in C# for angelo r reyes

Question #187129
Write a function-oriented program that scans a number n and then
output the sum of the powers from 1 to n. Thus, if the input is 3, the output should be 14
because: 11
+ 22
+ 33
= 1 + 4 + 9 = 14
1
Expert's answer
2021-05-01T08:13:13-0400
using System;


namespace Questions
{
    class Program
    {
        public static int SumOfPowers(int n)
        {
            int sum = 0;
            for (int i = 1; i <= n; i++)
                sum += i * i;
            return sum;
        }
        static void Main()
        {
            Console.Write("n = ");
            int n = int.Parse(Console.ReadLine());
            int sumOfPowers = SumOfPowers(n);


            Console.WriteLine("\nOutput: " + sumOfPowers);
            Console.ReadKey();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS