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