1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include <stdio.h>
int EnterNum(void)
{
int res;
int num;
printf("\n\t\t Enter a number: ");
fflush(stdout);
res = scanf("%i", & num);
if(res != 1)
num = 0;
return num;
}
int CountForward(void)
{
int i, num, count;
for(i = 0; i <= num; i++)
count = printf("\n %i", i);
}
int CountBackwards(void)
{
int i, num, count;
for(i= num; i >= 0; i--)
count = printf("\n %i", i);
}
int CountEven(void)
{
int i, num, count;
for(i = 0; i <= num; i = i + 2)
count = printf("\n %i", i);
}
int CountOdd(void)
{
int i, num, count;
for(i = 1; i <= num; i = i + 2)
count = printf("\n %i", i);
}
int main(void)
{
int i, num, count, choice;
printf("\t\t1. Count forward\n");
printf("\t\t2. Count backwards\n");
printf("\t\t3. Show even numbers\n");
printf("\t\t4. Show odd numers\n");
printf("\t\t5. Quit\n\n");
printf("\t\t Enter your choice: ");
fflush(stdout);
scanf("%i", & choice);
if(choice == 1)
{
num = EnterNum();
printf("counting forward from 1 to %i", num);
count = CountForward();
}
if(choice == 2)
{
num = EnterNum();
printf("counting backwards from %i to 1", num);
count = CountBackwards();
}
if(choice == 3)
{
num = EnterNum();
printf("Displaying odd numbers from 1 to %i", num);
count = CountOdd();
}
if(choice == 4)
{
num = EnterNum();
printf("Displaying even numbers from 1 to %i", num);
count = CountEven();
}
if(choice == 5)
{
return 0;
} |
Partager