Lab 2-1 (ElementaryProgramming)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSF12003: PROBLEM SOLVING AND COMPUTER PROGRAMMING

LAB 2-1 (ELEMENTARY PROGRAMMING)

1. State whether each of the following is true or false. If false, explain why.

a. Comments cause the computer to print the text enclosed between /* and */
when the program is executed.
b. All variables must be defined before they are used.
c. All variables must be given a type when they are defined.
d. C considers the variables number and NuMbEr to be identical.
e. The remainder operator (%) can be used only with the integer operands.
f. The arithmetic operators *, /, %, + and – all have the same level of precedence.

2. Assume that you want to include this comment “This is Lab 2 Assignment” in your
program. How to write it?

3. Determine which of the following are valid identifiers. If invalid, explain why.

a. M1+M2 f. cost per kg


b. A,b g. 1count
c. File_1 h. void
d. main i. AvErAgE
e. sum j. 123

4. Write a single C statement (with semicolon ;) to accomplish each of the following.

a. Define the variables c, thisVariable, q76354 and number to be of type int.


b. Declare the variable payment and valued to 120.
c. Define the variables balance and salary to be of type float.

5. Write appropriate declarations and assign the given initial values for each of the
variables.

a. Integer variables : a = 10, b = -1


b. Floating variables : x = 0.01, y = 0.25, z = 10.678
c. Character variables : aks1 = ‘a’, aks2 = ‘z’

6. Identify and correct the errors in the following program.

#include stdio.h

Void main() {

Int a,b,c;
float x,y,z;

A = 10;
b = 5;
c = A / b + 2;

x = 5.2;
y = 6.8;
z = x % y;
}
CSF12003: PROBLEM SOLVING AND COMPUTER PROGRAMMING
LAB 2-1 (ELEMENTARY PROGRAMMING)

7. Write, compile and run the following program. What is the output generated?

/* This program is to print Welcome to Faculty of Informatics */

#include <stdio.h>

void main() {

printf(“Welcome to\n”);
printf(“Faculty of Informatics,\n”);
printf(“Universiti Sultan Zainal Abidin\n”);
}

8. The following are the equations in algebra. Write appropriate arithmetic expression for
each of the following below.

a. g = ab(c+d)
e-f

b. p = x+ y+z
m

c. q= 1
m(x2 + y)

9. Suppose a, d, i and n are integer variables that have been assigned the values a = 4, d
= 7, i = 9 and n = 12. Determine the value of each of the following arithmetic
expressions.

a. d=d+7
b. a=a-5
c. n=n*9
d. i=i%5
e. n=n/2

10. State the order of evaluation of the operators in each of the following C statements and
show the value of x after each statement is performed.

a. x=7+3*6/2–1
b. x=2%2+2*2–2/2

11. State the following expression either is true (1) or false (0) if the value of x is given
below.

((x <= 20) && (x – 7) != 0) || (x>9)

a. 8
b.7
c. 11

You might also like