- C program to find power of a number using function.
- Write a c program to find power of a number using function recursion
Program #1 : Write a c program to find power of a number without using function recursion
- #include<stdio.h>
- int main(){
- int power,num,i=1;
- long int sum=1;
- printf("Enter a number:\n ");
- scanf("%d",&num);
- printf("\nEnter power:\n ");
- scanf("%d",&power);
- while(i<=power){
- sum=sum*num;
- i++;
- }
- printf("\n%d to the power %d is: %ld",num,power,sum);
- getch();
- }
Output:
Program #2 : Write a c program to find power of a number using function recursion
- #include<stdio.h>
- void powerofnumber(int power, int num);
- int main(){
- int power,num,i=1;
- long int sum=1;
- printf("Enter a number:\n ");
- scanf("%d",&num);
- printf("\nEnter power:\n ");
- scanf("%d",&power);
- powerofnumber(power,num);
- return 0;
- }
- void powerofnumber(int power, int num){
- int i=1,sum=1;
- while(i<=power){
- sum=sum*num;
- i++;
- }
- printf("\n%d to the power %d is: %ld",num,power,sum);
- }
Output:
- Enter a number:
- 23
- Enter power:
- 3
- 23 to the power 3 is: 12167
scvzssvzx
ReplyDelete