A Good Example Of A Loop in C

#include <stdio.h>

typedef int Number;

int main()
{ 
    Number   NumberEntered = 0;
    Number   PowerIdx = 0;


    printf("\nChoose a number for the calculation: ");
    scanf("%d", &NumberEntered);    

    //Loop to calculate NumberEntered to different exponents
    
    for (PowerIdx = NumberEntered; PowerIdx > 0; PowerIdx--)
    {  
        printf("%d to the 4th = %d", PowerIdx, (PowerIdx * PowerIdx * PowerIdx * PowerIdx));
        printf(", cubed = %d", (PowerIdx *PowerIdx * PowerIdx));
        printf(", squared = %d", (PowerIdx * PowerIdx));
        printf(", to the 1st = %d\n", PowerIdx);
    }
    
    printf("Program Over.\n");
    return 0;
}

Sample output:

Choose a number for the calculation: 4
4 to the 4th = 256, cubed = 64, squared = 16, to the 1st = 4
3 to the 4th = 81, cubed = 27, squared = 9, to the 1st = 3
2 to the 4th = 16, cubed = 8, squared = 4, to the 1st = 2
1 to the 4th = 1, cubed = 1, squared = 1, to the 1st = 1
Program Over.


This page was modified by Tanya Douglas at: Sunday, 25-Mar-2012 18:12:09 CST.
Copyright: Department of Computer Science, University of Regina of Regina.


[CS Department]