C MS1 Key
C MS1 Key
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
6. a=5 it's an assignment operator - the value 5 will be stored in the variable 'a' and the program
output is 5 for the variable.
A==5 '==' is used for comparison. It means if the value of A is equal to 5 then it will return
true as output.
7. The conditional operator or ternary operator in C is generally used when we need a short
conditional code such as assigning value to a variable based on the condition. The working of
the conditional operator in C is as follows:
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
10.
11. a.
Precedence of operators
The precedence of operators determines which operator is executed first if there is more than
one operator in an expression.
An example:
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
int x = 5 - 17* 6;
In C, the precedence of * is higher than - and =. Hence, 17 * 6 is evaluated first. Then the
expression involving - is evaluated as the precedence of - is higher than that of =.
Operators Precedence & Associativity Table
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
Associativity of Operators
The associativity of operators determines the direction in which an expression is evaluated.
For example,
b = a;
Here, the value of a is assigned to b, and not the other way around. It's because the
associativity of the = operator is from right to left.
Also, if two operators of the same precedence (priority) are present, associativity determines
the direction in which they execute.
example:
1 == 2 != 3
Here, operators == and != have the same precedence. And, their associativity is from left to
right. Hence, 1 == 2 is executed first.
The expression above is equivalent to:
(1 == 2) != 3
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
b.
Algorithm of factorial of a number
Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables:
i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate
fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Step 7: Stop
12. a.
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
if (m<p) {
Min = m;
} else {
Min = p;
}
} else {if (n<p) {
Min = n;
} else {
Min = p;
}}
printf("Min = %d",Min);
return 0;
}
13. a.
There are mainly two types of loops in C Programming:
Entry Controlled loops: In Entry controlled loops the test condition is checked before entering
the main body of the loop. For Loop and While Loop is Entry-controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the
loop body. The loop body will execute at least once, irrespective of whether the condition is
true or false. do-while Loop is Exit Controlled loop.
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
b.
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
14. a.
Algorithm to check if a given number is Palindrome or not
Step 1: Start
Step 2: Read the input number from the user
Step 3: Declare and initialize the variable reverse and assign input to a temp variable
tempNum=num
Step 4: Start the while loop until num !=0 becomes false
rem = num % 10
reverse*= 10 + rem
num = num / 10
Step 5 : Check if reverse == tempNum
Step 6: If it’s true then the number is a palindrome
Step 7: If not, the number is NOT a palindrome
Step 8: Stop
b.
C switch Statement
The switch statement allows us to execute one code block among many alternatives.
Syntax of switch...case
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding statements after the matching label are executed. For
example, if the value of the expression is equal to constant2, statements after case constant2:
are executed until break is encountered.
If there is no match, the default statements are executed.
Example:-
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING
MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01
ANSWER KEY
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
Output:
Value is 8
In the given program we have explain initialized a variable num with value 8.
A switch construct is used to compare the value stored in variable num and execute the block
of statements associated with the matched case.
In this program, since the value stored in variable num is eight, a switch will execute the case
whose case-label is 8. After executing the case, the control will fall out of the switch and
program will be terminated with the successful result by printing the value on the output
screen.