0% found this document useful (0 votes)
217 views3 pages

Mid Sem 190209

The document contains multiple coding questions and problems to solve involving loops, conditionals, functions, and basic C programming concepts. The respondent is asked to find errors in code snippets, answer conceptual questions about code outputs, write programs to calculate math problems, and use various loop and conditional structures.

Uploaded by

tagaya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views3 pages

Mid Sem 190209

The document contains multiple coding questions and problems to solve involving loops, conditionals, functions, and basic C programming concepts. The respondent is asked to find errors in code snippets, answer conceptual questions about code outputs, write programs to calculate math problems, and use various loop and conditional structures.

Uploaded by

tagaya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Part A: Answer all the questions.

Find the error in each of the following code segment and explain how to correct it.
(note: there may be more than one error per statement):

1. scanf ("%d%d", &number, number2);


2. if ( c<7);
printf ("c is less than 7\n");
3. if (c=>7)
printf ("c is equal to or less than 7\n");
4. scanf ("d",value);
5. printf ( "the product of %d and %d"\n,x,y);
6. firstNumber + secondNumber = sumOfNumbers
7. if (number => largest)
largest == number;
8. */ program to determine the largest of three integers*/
9. Scanf ("%d", anInteger);
10. printf ("remainder of % divided by % is \n",x,y,x%y);
11. if (x=y);
printf (%d is equal to %d\n",x,y);
12. print ("the sum is %d\n," ,x+y);
13. printf ("the value you entered is : %d\n,&value);
14. while ( c<=5) {
product *=c;
++c;
15 scanf ("%.4f",&value);
16 if (gender == 1)
printf ("woman\n");
else;
printf ("man\n");
17. x=1
while ( x <= 10);
x++;
}
18. for ( y = 0.1; y !=1.0; y +=0.1)
printf ( "%f\n",y);
19. switch (n) {
case 1:
printf ("the number is 1\n");
case 2:
printf ("the number is 2\n");
break;
default :
printf ("the number is not 1 or 2\n");
break;
}
20. The following code should print the values 1 to 10.
n = 1;
while ( n<10)
printf ("%d", n++);

1
Part B:Answer any 3 of the 6 questions.

1. Write a program that ask the user to enter two numbers, obtains the two
numbers from the user and prints the sum, product, and different of the two
numbers.

2. What, if anything, print when each of the following statements is performed?


If nothing prints, then answer "nothing.". Assume x=2 and y=3.
a) printf ("%d%d",&x,&y);
b) printf ("%d", x +x);
c) printf ( " \n");
d) /*printf ("x+y = %d",x+y);*/

3. What does the following code printf?


printf ( "*\n**\n***\n****\n*****\n");

4. What is wrong with the following while repetition statement (assume z has
value 1000), which is supposed to calculate the sum of the integers from 100
down to 1;
while ( z >=0)
sum +=z;

5. Write a single pseudocode statement that indicates each of the following:


a) Display the message " enter two numbers".
b) Assign the sum of variables x,y and z to variable p.
c) The following condition is to be tested in an if...else selection
statement: The current value of variable m is greater than twice the
current value of variable v.
6. Write for statement 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

2
Part C: Answer any 2 of the 4 questions.

1. Write a program that reads in the radius of a circle and prints the circle's
diameter, circumference and area. Use the constant value 3.14159 for Π.
Perform each of these calculation inside the printf statements and use the
conversation specifier %f.

2. The simple interst on a loan is calculated by the formula

interest = principal *rate *days /365;

the preceding formula assumes that rate is the annual ineterest rate, and
therefore includes the division by 365 (days). Develop a program that will
input principal,rate and days for several loans, and will calculate and
display the simple interest for each loan, using the preceding formula. Here is a
sample input/output dialog.

Enter loan principal (-1 to end) : 1000.00


enter interest rate: 0.1
enter term of the loan in days: 365
The interest charge is RM100.00

Enter loan principal (-1 to end) : 1000.00


enter interest rate: 0.08375
enter term of the loan in days: 224
The interest charge is RM51.40

3. What does the following program print?

#include <stdio.h>
int main()
{
int counter;
int grade;
int total;
int average;
total = 0;
counter = 1;

while ( counter <= 10 ) {


printf( "Enter grade: " );
scanf( "%d", &grade );
total = total + grade;
counter = counter + 1;
}
average = total / 10;
printf( "Class average is %d\n", average );
return 0;
}

4. By using for repetition statement. Write a program that calculates and prints
the sum of the even integers from 2 to 30.

You might also like