Advertisement
balrougelive

Untitled

Aug 8th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #pragma warning(disable: 4996)
  3. //#define _CRT_SECURE_NO_WARNINGS same use as above. for integer input to disable warnings/issues.
  4.  
  5. int main(void) // void and you don't need to return an integer
  6. {
  7. //declare my variables up front
  8. int userInt, counter1, counter2 = 0, counter3 = 1, product1 = 0, product2 = 0, product3 = 0; // initialize everything but the user input and for loop (redundant)
  9.  
  10. // get input
  11. printf("Enter an integer: ");
  12. scanf("%d", &userInt);
  13.  
  14. // for loop
  15. for (counter1 = 0; counter1 <= userInt; ++counter1)
  16. {
  17. product1 += counter1;
  18. }
  19.  
  20. // while loop
  21. while (counter2 <= userInt)
  22. {
  23. product2 += counter2++;
  24. }
  25.  
  26. // do while loop
  27. do {
  28. product3 += counter3;
  29. counter3 += 1;
  30. } while (counter3 < userInt + 1);
  31.  
  32. // print it all on separate lines for clarity
  33. printf("\n-------------------------for loop----------------------------\nThe sum of the integers from 1 to %d is %d\n", userInt, product1);
  34. printf("\n------------------------while loop---------------------------\nThe sum of the integers from 1 to %d is %d\n", userInt, product2);
  35. printf("\n-----------------------do while loop-------------------------\nThe sum of the integers from 1 to %d is %d\n", userInt, product3);
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement