You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 line
398 B

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. char * buf;
  5. int sum_to_n(int num)
  6. {
  7. int i,sum=0;
  8. for(i=1;i<=num;i++)
  9. sum+=i;
  10. return sum;
  11. }
  12. void printSum()
  13. {
  14. char line[10];
  15. printf("enter a number:\n");
  16. fgets(line, 10, stdin);
  17. if(line != NULL)
  18. strtok(line, "\n");
  19. sprintf(buf,"sum=%d",sum_to_n(atoi(line)));
  20. printf("%s\n",buf);
  21. }
  22. int main(void)
  23. {
  24. printSum();
  25. return 0;
  26. }