# Include Int Main (Int X 3, y 0 (X & Y) ? Printf ("True ") : Printf ("False ") (X && Y) ? Printf ("True ") : Printf ("False ") Return 0 )
# Include Int Main (Int X 3, y 0 (X & Y) ? Printf ("True ") : Printf ("False ") (X && Y) ? Printf ("True ") : Printf ("False ") Return 0 )
Enrolment No:
Solution A (2 marks)
Page 1 of 8
Solution
#include <stdio.h>
int main()
{
/*
• Declare a variable named ‘marks’ of type ‘double’. 6 CO3
• Read the value of ‘marks’ from user.
• Prints one the following messages based upon the value of ‘marks’
(i) Prints “Grade A” if ‘marks’ is greater than 90
(ii) Prints “Grade B” if 80 < marks <=90
(iii) Prints “Grade C” if 70 < marks <=80
(iv) prints “Grade Fail” if marks <=70
Page 2 of 8
*/
}// End of Main
int main() {
// Declare a variable named 'marks' of type 'double'. (1 Marks
double marks;
// Prints one of the following messages based on the value of 'marks'. (4 marks)
if (marks > 90) {
printf("Grade A\n");
} else if (marks > 80 && marks <= 90) {
printf("Grade B\n");
} else if (marks > 70 && marks <= 80) {
printf("Grade C\n");
} else {
printf("Grade Fail\n");
}
return 0;
}
Q B2 Write an algorithm to check whether a given number is prime or not and draw the
corresponding flowchart.
6 CO1
Solution (i) For correct algorithm (3 Marks)
(ii) For Correct Flowchart (3 Marks)
Q B3 A flowchart is given (shown below) to find and display the square and cube of a
positive number. The execution must be terminated if a negative number is entered.
6 CO1
Page 3 of 8
The above flowchart has some errors which are indicated with (1), (2), (3) and (4).
Answer the following questions to give the correct result.
(a) What will be the correct representation of the box in (1)?
(b) What will be the correct logic for the variable N in (2)?
(c) Out of the flow lines (3) and (4), which one is undesirable?
(d) When all the corrections are made, what will be the output, if the input value is
taken as 12?
Solution (a) The correct representation of the box in (1) is a parallelogram or input/output
box. (1.5 Marks)
(b) The logic for the variable N in (2) will be - If N>0? (1.5 Marks)
(c) Flow line (4) is undesirable. (1.5 Marks)
(d) The output will be 144, 1728. (1.5 marks)
Page 4 of 8
of swapping two numbers.
a. Identify and correct the error in the loop condition that may lead to
accessing memory beyond the array bounds.
b. Debug any logical errors preventing the correct calculation of the product of
the first five prime numbers.
c. Explain why the original loop condition was incorrect and how your
correction addresses the issue.
d. Suggest an alternative loop condition that would also work correctly for
iterating through the array.
Complete the missing logic in the code to display the product of the first five prime
numbers correctly.
Page 5 of 8
Solution (a) The original loop condition i < sizeof(primes) was incorrect. The corrected
condition is i < sizeof(primes) / sizeof(primes[0]), which gives the number
of elements in the array. (2 marks)
(b) The loop logic was correct after fixing the loop condition. (1 marks)
(c) The original loop condition was incorrect because it compared the loop
index i with the total size in bytes of the array. The correction divides the
total size by the size of one array element, ensuring that the loop iterates
over each element. (1 marks)
(d) An alternative loop condition could be i < 5, which directly specifies the
number of elements in the array. This would work correctly for iterating
through the array, given that we know there are five elements. (2 marks)
Q C2
Write a C program that accomplishes the following tasks step by step:
a. Declare three 2-D arrays named "K1," "K2," and "K3" of type "int" with
dimensions 2*2.
b. Read the values for the 2-D arrays "K1" and "K2."
c. Multiply the corresponding elements of "K1" and "K2" and store the results
in the 2-D array "K3."
d. Display the contents of the resulting 2-D array "K3."
Ensure the program is structured to perform these tasks sequentially and provide
the code for each step.
Page 6 of 8
(c) for (int i = 0; i < 2; ++i) ( 3 Marks)
{
for (int j = 0; j < 2; ++j)
{
K3[i][j] = K1[i][j] * K2[i][j];
}
}
Page 7 of 8
Q D2 Consider a scenario where you are tasked with developing a program in C to
manage employee inventory. The program should utilize structures to handle
employee information. The program should perform the following tasks:
printf("\nEmployee 2 Details:\n");
printf("Employee ID: %d\n", employee2.Employee_ID);
printf("First Name: %s\n", employee2.First_Name);
printf("Salary: %.2f\n", employee2.Salary);
Page 8 of 8