Calculate of XN by successive multiplications of two natural numbers X and N entered on the keyboard.
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
void main()
{
int X, N;
int I;
double RESULT;
do{
printf("Enter X : ");
scanf("%d", &X);
}while (X<0);
do
{
printf("Entrez N : ");
scanf("%d", &N);
}while (N<0);
/* for N=0, the result is X^0=1 */
for (RESULT=1.0, I=1 ; I<=N ; I++)
RESULT*=X;
/* Attention: for X=0 and N=0 , 0^0 is undefined */
if (N==0 && X==0)
printf("zero exponant zero is undefined !\n");
else
printf("Result : %d ^ %d = %.0f\n", X, N, RESULT);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Calculate the sum of digits of an integer using recursion