Practice Problems

Loops

Notice when you get compile errors (when gcc gives you an error)
vs. errors that crash the program (run-time errors)
vs. logical errors (compile and run fine but problem with your code)

  1. What happens when you put a semi-colon after the while ?
    		while( x < 10 );
    		{
    			printf( "%d", x );
    		}
    	
  2. Which loops execute the body of the loop zero or more times?
  3. Which loops execute the body of the loop one or more times?
  4. Write a loop that prints the values between 1 and 10 backwards.
  5. Write a loop that prints the values divisible by 5 between 1 and 100.
  6. Convert the following loop in to a for loop
    		x = 0;
    		while( x < 10 )
    		{
    			printf( "%d\n", x );
    			x++;
    		}
    	
  7. Convert the following loop in to a for loop
    		scanf( "%d", &x );
    		while( x != 9999 )
    		{
    			printf( "%d\n", x );
    			scanf( "%d", &x );
    		}
    	
  8. What gets printed in the following code?
    		for( int x=20; x>=1;x-- )
    		{
    			for( int y=x; y>=1;y--)
    				printf( "%3d",x);
    			printf("\n");
    		}
    	
  9. Write a program that prints the following:
    	1 2 3 4 5 6 7 8 9
    	1 2 3 4 5 6 7 8
    	1 2 3 4 5 6 7
    	1 2 3 4 5 6
    	1 2 3 4 5
    	1 2 3 4
    	1 2 3
    	1 2
    	1
    	
  10. Write a program that prints the following:
    	==========
    	*        *
    	*        *
    	*        *
    	*        *
    	*        *
    	==========
    	
  11. Write a program that takes one values: the number of rows
    Then based on that number, print the pattern
    	*
    	**
    	***
    	****
    	
  12. Write a guessing game program where the computer randomly selects a number between 1 and 100, and continually prompt the user for a number until they get it correct.
    Add comments whether or not the number is higher or lower.
  13. Write a program that continues to prompt the user for more numbers until the user enters the value -1.
    Then print out the highest value, lowest value, and average of all the numbers.
  14. What does the following program do? (Enter, compile, and run it.)
         /* EX2-4.C  from Teach Yourself C in 21 Days */
         #include <stdio.h>
        
         main()
         {
             int ctr;
        
             for( ctr = 65; ctr < 91; ctr++ )
                 printf("%c", ctr );
        
             return 0;
         }
         /* end of program */
    	
  15. What is the final value of x when the code int x; for(x=0; x < 10; x++) { } is run?
    	A. 10
    	B. 9
    	C. 0
    	D. 1
    
  16. When does the code block following while(x < 100) execute?
    	A. When x is less than one hundred
    	B. When x is greater than one hundred
    	C. When x is equal to one hundred
    	D. While it wishes
    
  17. Which is not a loop structure?
    	A. for
    	B. do while
    	C. while
    	D. repeat until
    
  18. How many times is a do while loop guaranteed to loop?
    	A. 0
    	B. Infinitely
    	C. 1
    	D. Variable
    
  19. What would the equivalent code, using a while loop, be for the example
    	for(i = 0; i < 10; i = i + 1)
    		printf("i is %d\n", i);
    
  20. (trick question) What would this code print?
    	int i;
    
    	for(i = 0; i < 3; i = i + 1)
    		printf("a\n");
    		printf("b\n");
    
    	printf("c\n");
    
  21. Type in and run this program:
    #include <stdio.h>
    
    int main()
    {
    	int i;
    
    	printf("statement 1\n");
    	printf("statement 2\n");
    	for(i = 0; i < 10; i = i + 1)
    		{
    		printf("statement 3\n");
    		printf("statement 4\n");
    		}
    	printf("statement 5\n");
    
    	return 0;
    }
    
    This program doesn't do anything useful; it's just supposed to show you how control flow works--how statements are executed one after the other, except when a construction such as the for loop alters the flow by arranging that certain statements get executed over and over. In this program, each simple statement is just a call to the printf function.

    Now delete the braces {} around statements 3 and 4, and re-run the program. How does the output change? (See also question 8 above.)

  22. What is the output of this program? Type in and run this program:
    #include <stdio.h>
    
    int main()
    {
    	int i, j;
    
    	printf("start of program\n");
    
    	for(i = 0; i < 3; i = i + 1)
    		{
    		printf("i is %d\n", i);
    		for(j = 0; j < 5; j = j + 1)
    			printf("i is %d, j is %d\n", i, j);
    		printf("end of i = %d loop\n", i);
    		}
    
    	printf("end of program\n");
    
    	return 0;
    }
    
  23. Type in and run this program:
    #include <stdio.h>
    
    int main()
    {
    	int day, i;
    
    	for(day = 1; day <= 3; day = day + 1)
    		{
    		printf("On the %d day of Christmas, ", day);
    		printf("my true love gave to me\n");
    		for(i = day; i > 0; i = i - 1)
    			{
    			if(i == 1)
    				{
    				if(day == 1) printf("A ");
    				else	printf("And a ");
    				printf("partridge in a pear tree.\n");
    				}
    			else if(i == 2)
    				printf("Two turtledoves,\n");
    			else if(i == 3)
    				printf("Three French hens,\n");
    			}
    		printf("\n");
    		}
    
    	return 0;
    }
    
    The result (as you might guess) should be an approximation of the first three verses of a popular (if occasional tiresome) song.

    a. Add a few more verses (calling birds, golden rings, geese a-laying, etc.).

    b. Here is a scrap of code to print words for the days instead of digits:

    	if(day == 1)
    		printf("first");
    	else if(day == 2)
    		printf("second");
    	else if(day == 3)
    		printf("third");
    
    Incorporate this code into the program.

    c. Here is another way of writing the inner part of the program:

    		printf("On the %d day of Christmas, ", day);
    		printf("my true love gave to me\n");
    		if(day >= 3)
    			printf("Three French hens,\n");
    		if(day >= 2)
    			printf("Two turtledoves,\n");
    		if(day >= 1)
    			{
    			if(day == 1) printf("A ");
    			else	printf("And a ");
    			printf("partridge in a pear tree.\n");
    			}
    
    Study this alternate method and figure out how it works. Notice that there are no else's between the if's.
  24. Write a program to find out (the hard way, by counting them) how many of the numbers from 1 to 10 are greater than 3. (The answer, of course, should be 7.) Your program should have a loop which steps a variable (probably named i) over the 10 numbers. Inside the loop, if i is greater than 3, add one to a second variable which keeps track of the count. At the end of the program, after the loop has finished, print out the count.
  25. Write a program to compute the average of the ten numbers 1, 4, 9, ..., 81, 100, that is, the average of the squares of the numbers from 1 to 10.

    If you keep track of the sum in a variable of type int, and divide by the integer 10, you'll get an integer-only approximation of the average (the answer should be 38). If you keep track of the sum in a variable of type float or double, on the other hand, you'll get the answer as a floating-point number, which you should print out using %f in the printf format string, not %d. (In a printf format string, %d prints only integers, and %f is one way to print floating-point numbers. In this case, the answer should be 38.5.)

  26. Write a program to print the numbers between 1 and 10, along with an indication of whether each is even or odd, like this:
    	1 is odd
    	2 is even
    	3 is odd
    	...
    
    (Hint: use the % operator.)
  27. Write a program to print the first 7 positive integers and their factorials. (The factorial of 1 is 1, the factorial of 2 is 1 * 2 = 2, the factorial of 3 is 1 * 2 * 3 = 6, the factorial of 4 is 1 * 2 * 3 * 4 = 24, etc.) - Why only the first 7?
  28. Write a program to print the first 10 Fibonacci numbers. Each Fibonacci number is the sum of the two preceding ones. The sequence starts out 0, 1, 1, 2, 3, 5, 8, ...