0% found this document useful (0 votes)
13 views2 pages

Exercise 3

Uploaded by

Aniq Azman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Exercise 3

Uploaded by

Aniq Azman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercises 3(Looping) 5.

Describe the output that will be generated by each


of the following C segment program
1. convert the following mathematical expression to C
code using for loop a) int i=0,x=0;
a. while (i<20){
if (i%5==0){
x+=1;
1 printf(“%d”,x);
= 2
! ++i;
b. }
printf(“\nx=%d”,x);
= sin(2 )

b) int i=0,x=0;
2. State whether the following are true or false. If the for(i=1;i<10;++i){
answer is false, explain why. if (i%2==1){
a) The default case is required in the x+=i;
else
switch selection statement
x--;
b) The break statement is required in the printf(“%d”,x);
default case of a switch selection }
statement printf(“\nx=%d”,x);
c) The expression (x>y && a < b) is true
if either x > y is true or a < b is true. c) int i=0,x=0;
d) An expression containing the || operator is for(i=1;i<10;++i){
true if either or both of its operands is true. if (i%2==1){
x+=i;
3. Write a statement or a set of statements to else
accomplish each of the following task x--;
printf(“%d”,x);
continue;
a) Sum the odd integers between 1 and 99
}
using for statement. Assume the integer
printf(“\nx=%d”,x);
variables sum and count have been
defined. d) int i=0,x=0;
b) Print the integers from 1 to 20 using a while for(i=1;i<10;++i){
loop and the counter variable x. assume if (i%2==1){
that the variable x has been defined, but not x+=i;
initialized. Print only five integers per line. else
[hint: use the calculation x%5. When the x--;
value of this is 0, print a newline character, printf(“%d”,x);
otherwise print a tab character.] break;
}
printf(“\nx=%d”,x);
4. Write a switch statement that will examine the
e) int i=0,x=0;
value of char-type variable called color and print do{
one of the following message, depending on the if (i%3==0){
character assigned to color. x++;
printf(“%d”,x);
a) RED, if either r or R is assigned to color, }
b) GREEN, if either g or G is assigned to color ++i;
c) BLUE, if either b or B is assigned to color }while(i<10);
d) BLACK, if color is assigned any other character. printf(“\nx=%d”,x);

NBS sem 2 2017/2018


f) int i,j,x=0; break;
for(i=1;i<5;++i) default:
for(j=1;j<i;++j){ printf(“the number is not 1 or
x+=(i+j-1); 2\n”);
break;
printf(“%d”,x);
}
}
printf(“\nx=%d”,x);
d. The following code should print the values 1
to 10.
g) int i,j,k,x=0;
for(i=1;i<5;++i) n=1;
for(j=1;j<i;++j){ while(n<10)
k+=(i+j-1); printf(“%d”,n++);
if(k%3==0)
x+=k;
else 6. (sum a sequence of integers) write a program that
if(k%3==0) sums a sequence of integers. Assume that the first
x+=k-2; integer read with scanf specifies the number of
printf(“%d”,x);
values remaining to be entered. Your program
}
should read only one value each time scanf is
printf(“\nx=%d”,x);
executed. A typical input sequence might be
h) int i,j,k,x=0;
for(i=1;i<5;++i) 5 100 200 300 400 500
for(j=1;j<i;++j){
switch (i+j-1){ 7. (Avarage a sequence of integers) write a program
case -1: that calculates and prints the average of several
case 0: integers. Assume that last value read with scanf is
x+=1; the sentinel 9999. A typical input sequence might be
break;
case 1: 10 8 11 7 9 9999
case 2:
case 3: 8. (find the smallest) write a program that finds the
x+=2;
smallest of several integers. Assume that the first
break;
value read specifies the number of values remaining.
default:
x+=3;
} 9. (Calculating the sum of even numbers) write a
printf(“%d”,x); program that calculates and prints the sum of the
} even integers from 2 to 30.
printf(“\nx=%d”,x);
10. (factorial) Write a program that evaluates the
2. Find the error in each of the following code segment factorials of the integers from 1 to 5. Print the results
and explain how to correct it. in tabular format. What difficulty might prevent you
from calculating the factorial of 20?
a. x=1;
while(x<=10);
x++;
}

b. for(y=.1;y!=1.0;y+=.1)
printf(“%f\n”,y);

c. switch(n){
case 1:
printf(“the number is 1\n”);
case 2:
printf(“the number is 2\n”);

NBS sem 2 2017/2018

You might also like