Before I begin, I have to apologize again for leaving without any thanks to the previous threads I posted.
Moving on, here's a code for factorial notation using for statement.
#include<stdio.h>
main(){
int factorial = 1, num, i, x;
printf("\n\n\n\n\t\t\tEnter a number from 1 to 10:\t");
scanf("%d", &num);
if(num >= 1 & num <= 10)
{
for(i = 1; i < num; i++)
{
factorial = factorial * i;
printf("%d x ", i);
}
x = factorial * num;
printf("%d = %d", num, x);
}
else
{
printf("ERROR! PLEASE TRY AGAIN");
}
getch();
}
As you can see, the program is fine. My friend and I experimented into this statement. Now we were to rewrite the program using while statement. We came up with this;
#include<stdio.h>
main(){
int factorial = 1, num, i, x;
printf("\n\n\n\n\t\t\tEnter a number from 1 to 10:\t");
scanf("%d", &num);
if(num >= 1 & num <= 10)
{
while(i < num)
{
factorial = factorial * i;
printf("%d x ", i);
}
x = factorial * num;
printf("%d = %d", num, x);
}
else
{
printf("ERROR! PLEASE TRY AGAIN");
}
getch();
}
Surely, I know that for statement houses the format "for (initialize; condition; evaluate)" while the while statement has "while (condition). Now the weird part takes place, when we enter 1, it appeared like this
1x1x1x1x1 = 0.
We even tried different numbers only to arrive at the same conclusion. Is there anything wrong in the positioning?