Has to be written in C#! Write a function integerPower(base, exponent) that uses
ID: 3788155 • Letter: H
Question
Has to be written in C#! Write a function integerPower(base, exponent) that uses a loop and returns the value of
base exponent
For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer and that base is an integer. If exponent is 0, it returns 1. If exponent is negative, it returns -1 and at the same time displays an error message: “The exponent value cannot be negative.”
Do NOT use C# built-in function POW to get the value.
Explanation / Answer
#include <stdio.h>
#include <conio.h>
int integerPower(int , int);
int main()
{
int base,exponent,result;
clrscr();
printf("Enter the base Value ");
scanf("%d",&base);
printf("Enter the Exponent Value ");
scanf("%d",&exponent);
result = integerPower(base,exponent);
printf(" Exponent value is %d",result);
getch();
return 0;
}
int integerPower(int a , int b)
{
int expo = 1,i;
if( b == 0)
{
return 1 ;
}
else if( b < 0)
{
printf("The exponent value cannot be negative.");
return -1;
}
else
{
for( i = 0 ; i < b ; i++)
{
expo = expo * a;
}
return expo;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.