Advertisement
Mary_99

powers of any natural number

Dec 2nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int power1 (int, int);
  4. int main(void)
  5. {
  6.     int w, x, n;
  7.     printf("Program calculates the power of integer number for the natural exponent\n\n");
  8.     printf("Give the powered number and the exponent");
  9.     scanf("%d %d", &x ,&n);
  10.     w = power1(x,n);
  11.     printf("Number %d square %d to %d\n ", x, n, w);
  12.     return 0;
  13. }
  14. int power1(int x, int n)
  15. {
  16.     int z,m,y;
  17.     z = x;
  18.     y = 1;
  19.     m = n;
  20.     while(m!=0)
  21.     {
  22.         //x^n = u*z^m and m>0
  23.         if(m%2==1) y= y*z;
  24.         m=m/2;
  25.         z=z*z;
  26.     }
  27.     //y=x^n
  28.     return y;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement