You are on page 1of 10

Programming with C Language

Tutorial 2

1. // is used before writing comments in a C program.

Purpose of writing comments is for the developers(programmers) to identify different stages of the
program much easily.

2. ‘Main’ function.

3. Scanf is used to accept input values.

4. Yes

5.

(a) valid

(b) invalid because identifier names cannot be started with a number

(c) invalid because a dash symbol cannot be used to separate words in an identifier

(d) invalid because keywords cannot be used as an identifier

(e) invalid because a $ symbol cannot be used in an identifier

(f) valid

(g) invalid because spaces cannot be kept between words

(h) invalid because dash symbol cannot be used

(i) valid

(j) invalid because a number cannot be used to start an identifier name and also dash symbols cannot
be used

6.
a) False, \n must be used to begin in a new line.
b) False, because compiler ignores comments.
c) True

Lakvindu Yapa – 26598


d) True
e) True
f) False because c language is case sensitive.
g) False, you can use the line break command \n to begin in a new line
7.
*
**
***
****
*****
8.
a) %d and &value
b) There are no three variables declared and also \n should be written before the inverted
commas closes
c) As c language is case sensitive capital S cannot be used in scanf and also the & symbol is
missing infront of the variable.
d) 3 variables are shown instead of 2 and also a symbol cannot be used in a variable name
e) Comma should be present after the inverted commas close and also a + symbol cannot be
present in a variable and letter ‘f’ is not present in printf
f) ‘P” cannot be capital in printf as c language is case sensitive and also inverted commas are
not closed correctly.
9.
a) 2 is printed
b) Nothing printed
c) X= is printed
d) X=2 is printed
e) Nothing is printed
f) Nothing is printed
g) Nothing is printed
h) Nothing is printed
i) Cursor goes to the next line

Lakvindu Yapa – 26598


10.
a) False, operators are not evaluated from left to right, it is evaluated according to the
precedence and associativity rules.
b) True
c) False, it doesn’t perform any calculation, it will just get printed
d) False, it is evaluated according to precedence and associativity rules.
e) False, h22 is correct

Lakvindu Yapa – 26598


Tutorial 3

Q1.
• x=x+1
• x++
• ++x
• x+=1

Q2.
a) Z=(x++) + y
b) x*=2
c) x=x*2
d) if(count>10) printf(“count is greater than 10”);
e) total=(--x) - total
f) total=total+x--
g) q=q/d
q/=d
h) printf(“%.2f”,123.4567)
Output is 123.45

i) printf(“%.3f”,3.14159);
Output is 3.141

Q3.
a) scanf(“%.d”,&x);
b) scanf(“%d”,&y);
c) i=1;
d) power=1;
e) power=power*x;
f) i=i+1;
g) while ( i <= y)
h) printf(“%d”,power);

Lakvindu Yapa – 26598


Tutorial 4
1) numNeighbors=4 should be recorrected as numNeighbors==4

There is no () in the if statement.

{} should be present in the if condition and end before else.

2) Output :
No, I’m here!

No, actually, I’m here!


3) Consider the following if statement, where doesSignificantWork,
makesBreakthrough, and nobelPrizeCandidate are all boolean variables:

if (doesSignificantWork)

if (makesBreakthrough) nobelPrizeCandidate =

true;

else

nobelPrizeCandidate = false;

else if (!doesSignificantWork) nobelPrizeCandidate =

false;

4)
– If character variable taxCode is ’T’, increase price by adding the taxRate percentage
of price to it.

If(taxCode==’T’)

Price = price+ (price*taxRate);

– If integer variable opCode has the value 1, read in double values for X and Y and
calculate and print their sum.

double x,y,sum;

Lakvindu Yapa – 26598


If(opcode=1) printf(“Enter the two
numbers); scanf(“%lf
%lf”,&x,&y);
sum = x +y;

printf(“sum is

%lf”,sum);

– If integer variable currentNumber is odd, change its value so that it is now 3 times
currentNumber plus 1, otherwise change its value so that it is now half of currentNumber
(rounded down when currentNumber is odd).
if(currentNumber%2==1)
currentNumber=(currentNumber*3)+1;
else
currentNumber=currentnumber*0.5;

– Assign true to the boolean variable leapYear if the integer variable year is a leap year.
(A leap year is a multiple of 4, and if it is a multiple of 100, it must also be a multiple of
400.)

Lakvindu Yapa – 26598


– Assign a value to double variable cost depending on the value of integer variable distance
as follows:

Distance Cost

----------------------------------- ----------

0 through 100 5.00

More than 100 but not more than 500 8.00

More than 500 but less than 1,000 10.00

1,000 or more 12.00

double cost;
if(distance> 0 && distance<100)
cost=5.00; else if(distance> 100
&& distance<500)
cost=8.00;
else if(distance>500 && distance<1000)
cost=10.00;
else(distance== 1000 && distance>1000)
cost=12.00;

Lakvindu Yapa – 26598


Tutorial 5

Switch
W

While Loop
1)

Lakvindu Yapa – 26598


2)

Do While Loop
1)

2)

Lakvindu Yapa – 26598


For Loop
1)

2)

Lakvindu Yapa – 26598

You might also like