To find Power of given number {using loops}

 Using for loop

#include<stdio.h>
#include<conio.h>
void main()
{
int n=1,i,x,y;
printf("enter numbers x = ");
scanf("%d",&x);
printf("enter numbers y = ");
scanf("%d",&y);
for(i=1;i<=y;i++)
{
n=n*x;
}
printf("power = %d",n);
getch();
}

Output

enter numbers x = 4
enter numbers y = 5
power = 1024

------------------------------------------------------------

Using while loop 

#include<stdio.h>
#include<conio.h>
void main()
{
int n=1,i,x,y;
printf("enter numbers x = ");
scanf("%d",&x);
printf("enter numbers y = ");
scanf("%d",&y);
while(y!=0)
{
n=n*x;
--y;
}
printf("power = %d",n);
getch();

Output 

enter numbers x = 5
enter numbers y = 4
power = 625

Comments