100% found this document useful (1 vote)
98 views

C Programming Tutorial 03 Question

The document contains questions about programming concepts like loops, conditional statements, operators, and sequences. It asks the reader to identify errors, rewrite code using different structures, determine output, and evaluate conditional expressions based on operator precedence.

Uploaded by

Nurfauza Jali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
98 views

C Programming Tutorial 03 Question

The document contains questions about programming concepts like loops, conditional statements, operators, and sequences. It asks the reader to identify errors, rewrite code using different structures, determine output, and evaluate conditional expressions based on operator precedence.

Uploaded by

Nurfauza Jali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

TMF 1414 INTRODUCTION TO PROGRAMMING

Tutorial 3

1. What is the difference between a while loop and a do-while loop ?

2. What is wrong with the following code ?


while ( sales >50)
printf(Your sales are very good this month.\n);
printf(You will get a bonus for your high sales\n);

3. How many times does this printf() print ?

int a=0;
do
{
printf(careful\n);
a++;
}
while(a>5);

4.What is printed to the screen in the following section of code ?

int a=1;

while (a<4)
{
printf("This is the outer loop\n");
a++;

while (a<=25)
{
break;
printf("This prints 25 times\n");}
}
}

5. Find the error in each of the following (Note: there may be more than one error):

a. The following code should output the odd integers from 999 to 1:

for ( x = 999; x >= 1; x += 2 )


printf( "%d\n", x );
TMF 1414 INTRODUCTION TO PROGRAMMING

b. The following code should output the even integers from 2 to 100:

counter = 2;
Do
{
if ( counter % 2 == 0 )
printf( "%u\n", counter );
counter += 2;
}
While ( counter < 100 );

c. The following code should sum the integers from 100 to 150 (assume total is
initialized to 0):

for ( x = 100; x <= 150; ++x );


total += x;

6. Rewrite the following code, using a switch statement :

if (num==1)
{ printf(alpha);}
else if (num ==2)
{printf(beta);}
else if (num ==3)
{printf(gamma);}
else
{printf(other);}

7. Can the following conditional operator be rewritten as an if-else statement? If so, rewrite
it as one.
ans = (a == b)? c + 2 : c + 3;

8. State which values of the control variable x are printed by each of the following for
statements:

a) for ( x = 2; x <= 13; x += 2 )


printf( "%u\n", x );

b) for ( x = 5; x <= 22; x += 7 )


printf( "%u\n", x );

c) for ( x = 3; x <= 15; x += 3 )


printf( "%u\n", x );

d) for ( x = 1; x <= 5; x += 7 )
printf( "%u\n", x );

e) for ( x = 12; x >= 2; x -= 3 )


printf( "%u\n", x );
TMF 1414 INTRODUCTION TO PROGRAMMING

9. Write for statements that print the following sequences of values:

a. 1, 2, 3, 4, 5, 6, 7
b. 3, 8, 13, 18, 23
c. 20, 14, 8, 2, -4, -10
d. 19, 27, 35, 43, 51

10. Using the precedence table, determine whether each of the following statements
produces a True or False result. After this, you should really appreciate the abundant
use of parentheses!
a. 5 = = 4 + 1 | | 7 * 2! = 12 1 && 5 = = 8 / 2
b. 8 + 9! = 6 1 | | 10 % 2! = 5 + 0
c. 17 1 > 15 + 1 && 0 + 2 = 1 = = | | 4! =1
d. 409 * 0! = 1 * 409 + 0 | | 1 + 8 * 2 >= 17

You might also like