Function Power Local Variables - C# Programming Exercise

In this C# exercise, you are asked to write a function named "Power" that calculates the result of raising an integer number to another positive integer number. The function must return another integer with the result of the power calculation. For example, calling Power(2,3) should return 8, as 2 raised to the power of 3 equals 8. It is important to note that you must use a repetitive structure, such as for or while, to perform this calculation instead of using the Math.Pow function. This exercise will help you practice using loops in C# and understand how to manually compute powers using a repetitive structure.

 Category

Functions

 Exercise

Function Power Local Variables

 Objective

Write a C# function named "Power" to calculate the result of raising an integer number to another (positive integer) number. It must return another integer number. For example. Power(2,3) should return 8.

Note: You MUST use a repetitive structure, such as "for " or "while", you cannot use Math.Pow.

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Function to calculate the power of a number raised to another number
    // It uses a for loop to multiply the base number by itself 'exponent' times
    public static int Power(int baseNumber, int exponent)
    {
        // Local variable to store the result of the power calculation
        int result = 1;

        // Repetitive structure (for loop) to multiply the base number by itself 'exponent' times
        for (int i = 0; i < exponent; i++)
        {
            result *= baseNumber; // Multiply result by baseNumber on each iteration
        }

        // Return the result of the power operation
        return result;
    }

    // Main method where the Power function is called
    public static void Main()
    {
        // Declare two integers: base and exponent
        int baseNumber = 2;
        int exponent = 3;

        // Call the Power function and print the result
        Console.WriteLine("The result of {0} raised to the power of {1} is: {2}", baseNumber, exponent, Power(baseNumber, exponent));
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function recursive power

    In this C# exercise, you are asked to write a function that calculates the result of raising one integer to another integer using recursion. For example, raising 5 to...

  •  Function Fibonacci

    In this C# exercise, you are asked to write a program that uses recursion to calculate a number in the Fibonacci series. The Fibonacci series is a seque...

  •  Function modify a letter in a string

    In this exercise of C#, you will learn how to create a function named ChangeChar that modifies a letter at a specific position (starting from 0) in a st...

  •  Function IsPrimeTarea

    In this exercise of C#, you will learn how to create a function named IsPrime that receives an integer number and returns true if the number is p...

  •  Function Parameters of Main, Sum

    In this exercise of C#, you will learn how to create a program named sum that receives two integer numbers from the command line and displays their sum. This t...

  •  Function SumDigits

    In this exercise of C#, you will learn how to create a function named SumDigits that receives a number and returns the result of summing its digits. Thi...