Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C-program to calculate power of a number



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
color> void main()color>
{
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();
}



Don't just read, write it, run it.....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
color> void main()color>
{
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();
}



Don't just read, write it, run it.....color>

RESULT:color>





This post first appeared on ProgramJoy.blogspot.com, please read the originial post: here

Share the post

C-program to calculate power of a number

×

Subscribe to Programjoy.blogspot.com

Get updates delivered right to your inbox!

Thank you for your subscription

×