fork download
  1. #include <stdio.h>
  2.  
  3. /* no prototype needed, but add them here if desired */
  4. int Triangular_Number (int);
  5.  
  6. //*********************************************************************
  7. //
  8. // Function: Triangular_Number
  9. //
  10. // Description: A triangular number can be generated by the
  11. // the following formula:
  12. //
  13. // Triangular number = n (n + 1) / 2
  14. //
  15. // This function will return the triangular number
  16. // calculated based it being passed an integer value
  17. //
  18. // Parameters: theNumber - An integer value to start the
  19. // calculation of the Triangular Number.
  20. //
  21. // Returns: The Triangular Number that is calculated
  22. //
  23. //*********************************************************************
  24.  
  25. int Triangular_Number (int theNumber)
  26. {
  27.  
  28. int tri_num; /* triangular number to be calculated */
  29.  
  30. /* Calculate the Triangular Number */
  31. tri_num = (theNumber * (theNumber + 1))/2;
  32.  
  33. /* return to the calling function */
  34. return(tri_num);
  35.  
  36. } /* end Triangular_Number */
  37.  
  38.  
  39. int main (void)
  40. {
  41.  
  42. int n; /* a number to calculate a triangular number */
  43. int tri_num; /* The triangular number calculated from n */
  44.  
  45. printf ("TABLE OF TRIANGULAR NUMBERS from 5 to 50 by 5\n\n");
  46. printf (" n Triangular Number\n");
  47. printf ("--- -----------------\n");
  48.  
  49. tri_num = 0;
  50.  
  51. /* Calculate and print triangular numbers from 5 to 50 in steps of 5 */
  52. for (n = 5; n <= 50; n += 5)
  53. {
  54.  
  55. printf (" %-5i %-5i\n", n, Triangular_Number(n));
  56. }
  57.  
  58. return (0); /* indicate successful completion */
  59.  
  60. } /* end main */
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
TABLE OF TRIANGULAR NUMBERS from 5 to 50 by 5

 n  Triangular Number
--- -----------------
 5     15   
 10    55   
 15    120  
 20    210  
 25    325  
 30    465  
 35    630  
 40    820  
 45    1035 
 50    1275