Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #pragma warning(disable: 4996)
- //#define _CRT_SECURE_NO_WARNINGS same use as above. for integer input to disable warnings/issues.
- int main(void) // void and you don't need to return an integer
- {
- //declare my variables up front
- int userInt, counter1, counter2 = 0, counter3 = 1, product1 = 0, product2 = 0, product3 = 0; // initialize everything but the user input and for loop (redundant)
- // get input
- printf("Enter an integer: ");
- scanf("%d", &userInt);
- // for loop
- for (counter1 = 0; counter1 <= userInt; ++counter1)
- {
- product1 += counter1;
- }
- // while loop
- while (counter2 <= userInt)
- {
- product2 += counter2++;
- }
- // do while loop
- do {
- product3 += counter3;
- counter3 += 1;
- } while (counter3 < userInt + 1);
- // print it all on separate lines for clarity
- printf("\n-------------------------for loop----------------------------\nThe sum of the integers from 1 to %d is %d\n", userInt, product1);
- printf("\n------------------------while loop---------------------------\nThe sum of the integers from 1 to %d is %d\n", userInt, product2);
- printf("\n-----------------------do while loop-------------------------\nThe sum of the integers from 1 to %d is %d\n", userInt, product3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement