» » » C programming power function example program

  • 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

  1.  #include<stdio.h>
  2. int main(){
  3.   int power,num,i=1;
  4.   long int sum=1;
  5.   printf("Enter a number:\n ");
  6.   scanf("%d",&num);
  7.   printf("\nEnter power:\n ");
  8.   scanf("%d",&power);
  9.   while(i<=power){
  10.             sum=sum*num;
  11.             i++;
  12.   }
  13.   printf("\n%d to the power %d is: %ld",num,power,sum);
  14.   getch();
  15. }

  Output:


power of number in c



  Program #2 : Write a c program to find power of a number using function recursion


  1.  #include<stdio.h>
     
  2. void powerofnumber(int power, int num);
  3. int main(){
  4.   int power,num,i=1;
  5.   long int sum=1;
  6.   printf("Enter a number:\n ");
  7.   scanf("%d",&num);
  8.   printf("\nEnter power:\n ");
  9.   scanf("%d",&power);
  10.   powerofnumber(power,num);
  11.   return 0;
  12. }
  13.  
  14. void powerofnumber(int power, int num){
  15.      int i=1,sum=1;
  16.      while(i<=power){
  17.             sum=sum*num;
  18.             i++;
  19.   }
  20.   printf("\n%d to the power %d is: %ld",num,power,sum);
  21.  
  22. }
 

Output:


  1. Enter a number:
  2.  23
  3.  
  4. Enter power:
  5.  3
  6.  
  7. 23 to the power 3 is: 12167

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu