C program for power of an integer

```c
#include <stdio.h>
void main () {
int i;
int p;
int q;
int a;
printf("saisir l'entier");
scanf("%d", &a);
printf("saisir la puissance");
scanf("%d", &p);
if (p == 0)
q = 1;
else {
q = 1;
for (i = 1; i <= p; i++)
q = q * a;
}
printf("%d\n", q);
system("pause");
}
```</stdio.h>

13 answers

  1. Contributor
    Good evening,
    You can simplify the algorithm.
    In fact, just need to set the variable q to 1 and use a simple while loop. There's no need to consider the case where the exponent is zero.
    For example:
     #include <stdio.h> int main(void) { int a; int p; int q; printf("enter the integer: "); fflush(stdout); scanf("%d",&a); printf("enter the power: "); fflush(stdout); scanf("%d",&p); q=1; while(p--) q*=a; printf("%d\n",res); return 0; } 

    If you have any questions about the rest of the code, feel free to ask.
    Best regards,
    --

    Google is your friend
    12
    1. ```c
      #include <stdio.h>
      #include <math.h>
      int puiss(int x, int y)
      {
      int q;
      int i = 0;
      if (y == 0)
      q = 1;
      else
      {
      q = 1;
      while (i < y)
      {
      q = q * x;
      i++;
      }
      }
      return q;
      }
      int main()
      {
      int a;
      int p;
      int waw;
      printf("****Bonjour malek****");
      printf("comment cv pas !");
      printf("\n saisir l'entier x :\n ");
      scanf("%d", &a);
      printf("\n saisir la puissance p : \n ");
      scanf("%d", &p);
      /*calcule de puissance*/
      waw = puiss(a, p);
      printf("\n la puissance de %d ** %d est : %d \n", a, p, waw);
      return 0;
      }
      ```</math.h></stdio.h>
      1