can someone tell me what is wrong with this code.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char input[5]; /* uers's input */
int r_input,count,t_num=0; /* converted intput, counter and tri number */
printf("What number would you like the triangular of? ");
scanf("%d", &input); /* caputre data */
r_input = atoi(input); /* validate input */
/* calculate triangular number
* which is bascically factorial
* ex: 7 + 6 + 5 + 4 + 3 + 2 + 1
*/
for(count = 0; count <=r_input; count++)
{
t_num = t_num + count;
printf("Count = %d \t T_num = %d\n", count, t_num);
}
printf("Finished!\n");
system("pause");
return 0;
}
when i run it, it seems the loop stays at 0? what am i doing wrong?
I removed the atoi function and it worked fine
Code:
#include <stdio.h>
/* #include <stdlib.h> */
int main()
{
/* char input[10]; uers's input */
int r_input,count,t_num=0, input; /* converted intput, counter and tri number */
printf("What number would you like the triangular of?\n");
scanf("%d", &input); /* caputre data */
/* r_input = atoi(input); validate input */
/* calculate triangular number
* which is bascically factorial
* ex: 7 + 6 + 5 + 4 + 3 + 2 + 1
*/
for(count = 0; count <=input; count++)
{
t_num = t_num + count;
printf("Count = %d \t T_num = %d\n", count, t_num);
}
printf("Finished!\n\a");
system("pause");
return 0;
}
What am i doing wrong?
while i'm at. Is the triangular number not the same thing as a factorial.
Thank you.