C Programming

PRACTISE EXERCISE 4

for loops

1. The statement which prints out the values 1 to 10 on separate lines, is

Statement 1

	for( count = 1; count <= 10; count = count + 1)
	   printf("%d\n", count);
Statement 2
	for( count = 1; count < 10; count = count + 1)
	   printf("%d\n", count);
Statement 3
	for( count = 0; count <= 9; count = count + 1)
	   printf("%d ", count);
Statement 4
	for( count = 1; count <> 10; count = count + 1)
	   printf("%d\n", count);


2. The statement which produces the following output is, (hint: use two nested for loops)

	1
        22
        333
        4444
        55555

Statement 1
	for(a = 1; a <= 5; a = a + 1) {
	   for( b = 1; b <= 5; b = b + 1)
	     printf("%d", b);
	   printf("\n");
	}
Statement 2
	for( a = 1; a <= 5; a = a + 1) {
	   for( b = 1; b <= a; b = b + 1)
	      printf("%d", a);
	   printf("\n");
	}
Statement 3
	for( a = 1; a <= 5; a = a + 1) {
	   for( b = a; b <= 5; b = b + 1)
	      printf("%d", b);
	   printf("\n");
	}
Statement 4
	for( a = 1; a <= 5; a = a + 1) {
	   for( b = 1; b < a; b = b + a)
	      printf("%d", b);
	   printf("\n");
	}

3. The statement which sums all values between 10 and 100 into a variable called total is, assuming that total has NOT been initialised to zero.

Statement 1
	for( a = 10; a <= 100; a = a + 1)
	   total = total + a;
Statement 2
	for( a = 10; a < 100; a = a + 1, total = 0)
	   total = total + a;
Statement 3
	for( a = 10; a <= 100, total = 0; a = a + 1)
	   total = total + a;
Statement 4
	for( a = 10, total = 0; a <= 100; a = a + 1)
	   total = total + a;

4. The statement that prints out the character set from A-Z, is

Statement 1
	for( a = 'A'; a < 'Z'; a = a + 1)
	   printf("%c", a);
Statement 2
	for( a = 'a'; a <= 'z'; a = a + 1)
	   printf("%c", &a);
Statement 3
	for( a = 'A'; a <= 'Z'; a = a + 1)
	   printf("%c", a);
Statement 4
	for( a = 'Z'; a <= 'A'; a = a + 1)
	   printf("%c", a);

©Copyright B Brown. 1984-1999. All rights reserved.