mid sem 190209

  • Upload
    tagaya

  • View
    245

  • Download
    0

Embed Size (px)

Citation preview

  • 8/14/2019 mid sem 190209

    1/3

    Part A: Answerall 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 ( c7)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

  • 8/14/2019 mid sem 190209

    2/3

    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 whilerepetition statement (assume z hasvalue 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 thecurrent value of variable v.

    6. Write forstatement 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

  • 8/14/2019 mid sem 190209

    3/3

    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, andtherefore includes the division by 365 (days). Develop a program that will

    input principal,rate and days for several loans, and will calculate anddisplay the simple interest for each loan, using the preceding formula. Here is a

    sample input/output dialog.

    3. What does the following program print?

    4. By using for repetition statement. Write a program that calculates and prints

    the sum of the even integers from 2 to 30.

    3

    Enter loan principal (-1 to end) : 1000.00enter interest rate: 0.1enter term of the loan in days: 365The interest charge is RM100.00

    Enter loan principal (-1 to end) : 1000.00enter interest rate: 0.08375enter term of the loan in days: 224The interest charge is RM51.40

    #include int main(){

    int counter;int grade;int total;int average;total = 0;counter = 1;

    while ( counter