Write a Program in C to evaluate raised to the power
![Write a Program in C to evaluate raised to the power raised to the power, C Program, Sumit Kar, Timus Rak, Power, Function, Loop, While, For](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd7JUeFcGURR9T3358Yzicn60HdqsMSKqXUO3-Sxfx4K5Sqc7UgUwbzWzXCCtqRS4lZ_KrO_J8XbTe1fxRFzDQ-ZO_BYWAYQLuUNCKozi7hAbEpi0FGxITpJhJtEywftgs9V5CX1CA6O8a/s1600/c-programming-tutorial.jpg)
/*Using While Loop*/
#include<stdio.h>
void main()
{
int a, b, i, p;
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
p=1;
i=1;
while(i<=b)
{
p = p * a;
i++;
}
printf("%d to the power %d is : %d",a,b,p);
}
/*Using For Loop*/
#include<stdio.h>
void main()
{
int a, b, i, p;
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
p=1;
for(i=1;i<=b;i++)
p = p * a;
printf("%d to the power %d is : %d",a,b,p);
}
/* Using Function */
#include <stdio.h>
#include <conio.h>
double power(int, int);/*PROTOTYPE*/
void main( )
{
int x,y;
printf("Enter value of a: ");
scanf("%d",&x);
printf("Enter value of b: ");
scanf("%d",&y);
printf("%d to the power %d is : %f\n", x,y,power (x,y));
getch();
}
double power (int x, int y) /*FUNCTION DEFINITION*/
{
double p;
p = 1.0 ; /* X to the power 0 */
if(y >=0)
while(y--) /* positive powers */
p *= x;
else
while (y++) /* negative powers */
p /= x;
return(p); /* returns double type */
}
Comments
Post a Comment