Write a program which calculates expression $$N ^ N$$ where $$N$$ is an integer value filled by the user.
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
void main()
{
int N;
int I;
double RESULT;
do
{
printf("Entrez N : ");
scanf("%d", &N);
}while (N<0);
/* for N=0, the result is N^0=1 */
for (RESULT=1.0, I=1 ; I<=N ; I++)
RESULT*=N;
/* Attention: N=0 , 0^0 is undefined */
if (N==0)
printf("zero exponant zero is undefined !\n");
else
printf("Result : %d ^ %d = %.0f\n", N, N, RESULT);
return 0;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Function sorting an array of strings