C Programming

Practise Exercise 5: while loops and if else

1. The statement which prints the integer values 1 to 10 on the screen, is

	12345678910

Statement 1
	count = 1;
	while( count <= 10 ) {
	   printf("%d", count);
	   count = count + 1;
	}
Statement 2
	count = 1;
	while( count <= 10 ) {
	   printf("%d", &count);
	   count = count + 1;
	}
Statement 3
	count = 1;
	while( count < 10 ) {
	  printf("%d\n", count);
	  count = count + 1;
	}
Statement 4
	count = 1;
	while( count <= 10 ) {
	  printf("%d\n", count);
	  count = count + 1;
	}

2. The statement which reproduces the following output, is

	1
	22
	333
	4444
	55555

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

3. The statement that compares the value of an integer called sum against the value 65, and if it is less, prints the text string "Sorry, try again", is

Statement 1
	if( sum < "65" )
	   printf("Sorry, try again" );
Statement 2
	if( sum <= 65 )
	   printf("Sorry, try again" );
Statement 3
	if( 65 == sum )
	   printf("Sorry, try again" );
Statement 4
	if( sum < 65 )
	   printf("Sorry, try again" );

4. The statement that compares total for equality to good_guess, and if equal prints the value of total, and if not equal prints the value of good_guess, is

Statement 1
	if( total < good_guess )
	   printf("%d", total );
	else
	   printf("%d", good_guess );
Statement 2
	if( total == good_guess )
	   printf("%d", good_guess );
	else
	   printf("%d", total );
Statement 3
	if( total = good_guess )
	   printf("%d", total );
	else
	   printf("%d", good_guess );
Statement 4
	if( total == good_guess )
	   printf("%d", total );
	else
	   printf("%d", good_guess );

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