4/4/08

Simple Counter in C

This is a very simple program in C. User is asked for a number. If user gives a number from 1 to 20 then the program prints all the numbers from 1 to the number the user inputed.
If user give a number out of range program asks again for a valid number.
This program is here so reader can understand better how this little program is written in MIPS code. The same in MIPS code is here.

#include <stdio.h>

int main() {
int x,i;
do{
printf("Please give an integer from 1 to 20 : ");
scanf("%d",&x);
}
while( x>20 || x<1 );
for (i=1;i<=x;i++) {
printf("%d\n",i);
}
exit(0);
}

No comments: