Question
We have provided a source file, exercises.c (see the src directory) with several loop exercises for you to complete as outlined in the comments embedded
Here is apartialoutput example for an input value of n = 10. Submit your solution to the webhandin and use the webgrader to verify your full program results:
10 13 16 19
3 2 1 0 -1 -2 -3
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1, 4, 9, 16, 25, 36, 49, 64, 81, 100
Sum of 1 thru 10 = 55
Sum of 1^2 thru 10^2 = 385
Sum of even numbers 1 thru 10 = 30
/** * Author(s): * Date: * * This is a series of exercises to introduce loop * control structures. * */ #include #include #include
int main(int argc, char **argv) {
if(argc != 2) { fprintf(stderr, "Usage: %s n ", argv[0]); exit(1); }
int n = atoi(argv[1]);
// A simple for loop that prints 0 thru 9 for(int i=0; i10;> printf("%d ", i); }
//1. Rewrite the given for loop as an equivalent while loop for(int i=10; i20;> printf("%d ", i); } printf(" ");
//2. Rewrite the given while loop as an equivalent for loop int k = 3; while(k > -4) { printf("%d ", k); k--; } printf(" ");
//3. Print numbers 1 thru n separated by a comma except for // the last one; example: 1, 2, 3, 4, 5
//4. Print squares of numbers 1 thru n separated by a comma // except for the last one; example: 1, 4, 9, 16, 25
//5. Compute the summation of numbers 1 thru n and print // the result; example: 1 + 2 + 3 + 4 + 5 = 15
//6. Compute the summation of squares of numbers 1 thru n // and print the result; example: 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55
//7. Compute the summation of even numbers 1 thru n and print // the result; example (n=5): 2 + 4 = 6
//8. Write a solution to the following variation of the FizzBuzz // problem. Pring out integers 1 thru n, one to a line, except // that if the integer is divisible by 7 print "Foo" instead. // If the integer is divisible by 11 print "Bar" instead. If // the integer is divisible by both 7 and 11, print "FooBar" // instead.
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started