In this C-program we will calculate the power of a number and print the result. The number will be taken from the user.color>
We can calculate the power of a number in two ways. In the first way, we will use a loop (while loop) and calculate it.
In the second way, we will calculate it with the help of a function , called "pow()".
First way: Calculate with loop
input:
The number(i.e : base) and the power(i.e : exponent)(i.e : 5,6,9 etc.)output:
The result of it's calculation.CODE---->
#include
#include
{
int a,b,r=1,n;
clrscr();
printf("Please, Enter a number (base):color>");
scanf("%dcolor>",&n);
printf("\nPlease, Enter it\'s power (exponent):color>");
scanf("%dcolor>",&a);
b=a;
while(a!=0color>)
{
r=r*n;
a--;
}
printf("\nThe result of %d^%d = %d color>",n,b,r);
getch();
}
color>
RESULT:color>
*Notecolor> : The above program will work only if the number and the exponent are positive integers.
Second way: Calculate with function pow()
input:
The number(i.e : base) and the power(i.e : exponent)(i.e : 5,6,9 etc.)output:
The result of it's calculation.CODE---->
#include
#include
#include
{
float a,r,n;
clrscr();
printf("Please, Enter a number (base):color>");
scanf("%fcolor>",&n);
printf("\nPlease, Enter it\'s power (exponent):color>");
scanf("%fcolor>",&a);
r=pow(ncolor>,acolor>);
printf("\nThe result of %f^%f = %f color>",n,a,r);
getch();
}
color>