Fall'23 - Mid2 - Solution
Fall'23 - Mid2 - Solution
Q1 20
Sr Code Answer
.
a. for (int i = 1; i <= 3; ++i) *
{
for (int j = 1; j <= i; ++j) *
{ **
for (int k = 1; k <= j; ++k)
cout << '*';
*
cout << endl;
**
}
cout << endl; ***
}
b. for (int i = 1; i <= 4; i++) 0111
{ 1011
for (int j = 1; j <= 4; j++) 1101
{ 1110
if (i == j)
{
cout << "0 ";
}
else
{
cout << "1 ";
}
}
cout << endl;
}
int i = 0;
int j = 5;
while ( i < 10 && j > 0 )
{
c. cout << i * j;
0
j = i - j;
i++;
}
int main()
{
int firstNum = 28;
int secondNum = 25;
cout << (firstNum = 38 - 7) << endl; 31
cout << (firstNum <= 75) << endl; 1
d. cout << (firstNum > secondNum + 10) << 0
endl; 1
cout << (firstNum >= 3 * secondNum - 100) 0
<< endl;
cout << (secondNum - 1 == 2 * firstNum) <<
endl;
}
National University of Computer and Emerging Sciences
Department of Computer Science Chiniot-Faisalabad Campus
int main()
{
Question Skipped due to
int r,a,b,c;
ambiguous statement.
e. r = (a>b?(a>c?5:7):(b>c?9:10));
Marks adjusted in above
cout<<r;
parts.
return 0;
}
Q2 15
Sum =
// Part-2: Factorial of X
int fact = 1;
for (int j = 1; j <= x; j++)
{
fact = fact * j; -----------
}
system("pause");
return 0;
}
b. Suppose that classStanding is a char variable, gpa and dues are double variables. Write a
switch expression that assigns the dues as following: If classStanding is ‘f’ or ‘F’, the dues
are 20000.00; if classStanding is 's' or ‘S’, (if gpa is at least 3.75, the dues are 15000.00;
otherwise dues are 10000.00); if classStanding is 'j' or ‘J’, (if gpa is at least 3.75, the dues are
9000.00; otherwise dues are 11000.00); if classStanding is 'n' or ‘N’, (if gpa is at least 3.75,
the dues are 5000.00; otherwise dues are 7500.00). (Note that the code 'f' stands for first year
students, the code 's' stands for second year students, the code 'j' stands for juniors, and the
code 'n' stands for seniors.) 10
char classStanding;
double gpa, dues;
switch (classStanding)
{
case 'f':
case 'F':
dues = 20000.0;
break;
case 's':
case 'S':
if (gpa >= 3.75)
//and above gpa
dues = 15000.0;
else
dues = 10000.0;
break;
case 'j':
case 'J':
if (gpa >= 3.75)
dues = 9000.0;
else
dues = 11000.0;
break;
case 'n':
case 'N':
if (gpa >= 3.75)
dues = 5000.0;
else
dues = 7500.0;
2 Mid Term
nd
Fall-2023 Page 5 of 8
National University of Computer and Emerging Sciences
Department of Computer Science Chiniot-Faisalabad Campus
break;
}
c. Write down the following scenario using conditional (ternary) operator. The grade
allocated to the students
5 = 2.5 * 2
i. if (x + y > 15)
y = x;
else if (x == y)
y = 2 * y;
else
y = 2 * x;