0% found this document useful (0 votes)
22 views7 pages

Wjfa CZ0 IHc 4 R NB6 G

Uploaded by

kashishgrewal24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Wjfa CZ0 IHc 4 R NB6 G

Uploaded by

kashishgrewal24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Roll No…………………..

Dr B R Ambedkar National Institute of Technology, Jalandhar


B Tech (All Branches) – 2nd Semester
CSFC0101, Computer Programming
Mid-Semester Examination, March 2025
Duration: 02 Hours Max. Marks: 30 Date: 17 March
2025

Question 1 2 3 4 5 6 7
Number
Marks 5 5 4 4 4 4 4
CO No. 1 1 1 1 1 1 1
Cognitive Level R Ap C C Ap Ap An
Note:
1. Attempt all the Seven questions available on Three pages.
2. Draw neat and clean diagrams.
3. Write all the possible steps in your solutions.
4. If you are assuming something, then clearly mention it.
5. Calculators are not allowed.

1. Answer the following questions. [1x5]


a) Write eight different types of constants.
Decimal: 10, Floating-point: 3.14, Character: ‘a’, String:
“Hello”, Hexadecimal: 0x1A, Octal: 034, Binary: 0b01,
Unsigned: 345U ---------------------------------------------(1)
b) Declare a floating-point variable and assign a double constant
to it.
float a; -----------------------------------------------------(0.5)
a=0.5; -----------------------------------------------------(0.5)
c) What happens when you put a (;) immediately after an if
condition?
When we put a semicolon (;) immediately after an if
condition in C, it creates an empty statement, meaning that
the if statement does nothing. The next statement after the
if statement will execute unconditionally, regardless of
whether the if condition is true or false. -------------------(0.5)

#include <stdio.h>
int main() {
int x = 5;
if (x > 0);// Empty statement! The if condition does
nothing.
printf("Positive\n"); // This executes unconditionally!
return 0;
} --------------------------------------------------------------(0.5)
d) Differentiate between a Compiler and an Interpreter.
Compiler Interpreter
Translates the entire Translates and
source code into executes the code line
machine code before by line.
execution.
Faster (because the Slower (as it
entire program is translates line by line
compiled before during execution).
execution).
Shows all errors after Stops execution at the
compilation. first error and reports
it immediately.
Creates a separate No separate file,
executable file (e.g., executes the code
.exe). directly.

e) What are the limits of << and >> operators?


 If the shift amount is greater than or equal to the bit-
width of the data type, the behavior is undefined. ---(0.5)
 Left shifting a signed integer may cause an overflow,
leading to undefined behavior. ---------------------------(0.5)
2. Write one-line expressions for the following statements in C. Note:
Do not use any loops or conditional and branching statements. [5]
a) Multiply a Number by 15 without using *, /, and + operators.
y = (x << 4) - x; ----------------------------------------------------(1)
b) Check if two numbers differ by exactly one bit.
x^y && (x^y & (x^y)-1)==0 --------------------------------------(1)
c) Determine if a number is positive, zero, or negative without
using ternary, relational, and arithmetic operators. If the
number is positive, zero, or negative, then print 1, 0, and -1,
respectively.
printf(“%d”, (num >> 31) | (!!num)); --------------------------(1)
d) Compute the remainder when x is divided by 2^n without
using % operator, where x and n are inputs.
remainder = x & ((1 << n) - 1); ----------------------------------(1)
e) Determine if two integers have the same sign without using
ternary, relational, and arithmetic operators.
!((a ^ b) >> 31) ---------------------------------------------------(1)

3. A menu-driven bank account management system is to be developed.


This system allows customers to manage their account balance
through deposits and withdrawals. The account starts with an initial
balance of ₹10,000. The system should present the menu with Deposit
Money, Withdraw Money and Exit options. In the case of a deposit
operation, the customer needs to enter the deposit amount, which
would be added to the balance amount. The updated balance will be
displayed. In the case of a withdrawal operation, the customer will
enter the withdrawal amount. The system will check if there is a
sufficient balance. If the withdrawal exceeds the balance, print an error
message and cancel the withdrawal. If the withdrawal is valid, subtract
the amount from the balance and display the updated balance.
Negative or zero values need to be rejected in both cases. If the
customer chooses to exit, exit the program and print the final balance
along with a thank you message. [4]
#include <stdio.h>

int main() {
int balance = 10000, amount, choice;

while (1) {
// Display menu
printf("\n=== Bank Account Management ===\n");
printf("1. Deposit Money\n");
printf("2. Withdraw Money\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
// Deposit money
printf("Enter deposit amount: ");
scanf("%d", &amount);
if (amount > 0) {
balance += amount;
printf("Deposit successful. Updated balance: ₹%d\n",
balance);
} else {
printf("Invalid deposit amount. Must be greater
than zero.\n");
}
break;

case 2:
// Withdraw money
printf("Enter withdrawal amount: ");
scanf("%d", &amount);
if (amount <= 0) {
printf("Invalid withdrawal amount. Must be greater
than zero.\n");
} else if (amount > balance) {
printf("Insufficient balance! Withdrawal
failed.\n");
} else {
balance -= amount;
printf("Withdrawal successful. Updated balance:
₹%d\n", balance);
}
break;

case 3:
// Exit program
printf("Thank you for using our service. Final balance:
₹%d\n", balance);
return 0;

default:
printf("Invalid choice! Please select a valid
option.\n");
}
}
}
4. Write a program in C that computes the income tax to be deposited by
a salaried person to the Income Tax Office. [4]
Slab no. Salary in Rupees (Lakh) Tax
1 Less than 4,00,000 0%
2 Between 4,00,000 and 8,00,000 5%
3 Between 8,00,000 and 12,00,000 10%
4 Between 12,00,000 and 16,00,000 15%
5 Between 16,00,000 and 20,00,000 20%
6 Between 20,00,000 and 24,00,000 25%
7 Greater than 24,00,000 30%
Any employee whose salary is less than 12,00,000 must give zero tax.
Suppose the salary of an employee is 12,75,000, then the total tax will
be (Rs. 71250) computed as 0 + 20000 (i.e., slab 2) + 40000 (i.e., slab
3) + 11250 (i.e., remaining of slab 4).
#include <stdio.h>

int main() {
double salary, tax = 0;

printf("Enter your annual salary in Rupees: ");


scanf("%lf", &salary);
if (salary < 1200000) {
tax = 0;
} else if (salary >= 1200000 && salary < 1600000) {
tax = (400000 * 0.05) + (400000 * 0.10) + (salary -
1200000) * 0.15;
} else if (salary >= 1600000 && salary < 2000000) {
tax = (400000 * 0.05) + (400000 * 0.10) + (400000 * 0.15) +
(salary - 1600000) * 0.20;
} else if (salary >= 2000000 && salary < 2400000) {
tax = (400000 * 0.05) + (400000 * 0.10) + (400000 * 0.15) +
(400000 * 0.20) + (salary - 2000000) * 0.25;
} else { // Salary >= 2400000
tax = (400000 * 0.05) + (400000 * 0.10) + (400000 * 0.15) +
(400000 * 0.20) + (400000 * 0.25) + (salary - 2400000) * 0.30;
}

printf("Total tax to be deposited: Rs. %.2lf\n", tax);

return 0;
}
5. (a) Compute the floating-point number from the following 32-bits that
are represented by IEEE 32-bit single precision 754 floating-point
representation. [4]
0 10000111 11011000000000000000000
(−𝟏)𝟎 × 𝟐(𝟏𝟎𝟎𝟎𝟎𝟏𝟏𝟏)𝟐−𝟏𝟐𝟕 × 𝟏. (𝟏𝟏𝟎𝟏𝟏𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎)𝟐
--------(1)
= (−𝟏)𝟎 × 𝟐𝟏𝟑𝟓−𝟏𝟐𝟕 × 𝟏. 𝟖𝟒𝟑𝟕𝟓 ------(0.5)
= (−𝟏)𝟎 × 𝟐𝟖 × 𝟏. 𝟖𝟒𝟑𝟕𝟓 ------(0.5)
= 𝟒𝟕𝟐-
(b) Represent 29.625 using IEEE 32-bit single precision 754 floating-
point representation. Show all the calculations explicitly.

𝟐𝟗. 𝟔𝟐𝟓
= (−𝟏)𝟎 × 𝟐𝟒 × 𝟏. 𝟖𝟓𝟏𝟓𝟔𝟐𝟓 ------(0.5)
= (−𝟏)𝟎 × 𝟐𝟏𝟑𝟏−𝟏𝟐𝟕 × 𝟏. 𝟖𝟓𝟏𝟓𝟔𝟐𝟓 ------(0.5)
= (−𝟏)𝟎 × 𝟐(𝟏𝟎𝟎𝟎𝟎𝟎𝟏𝟏)−𝟏𝟐𝟕 × 𝟏. 𝟏𝟏𝟎𝟏𝟏𝟎𝟏𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎 -------(1)

6. Convert the following number to base 6. [4]


a) (345)11 b) (116)7 c) (721)9 d) (432)5
(1524)6 (142)6 (2414)6 (313)6

7. Write the output of the following code snippets with proper


justifications. Answers without a proper justification will incur zero
marks. [4]
a) b)
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int x = 10; int a = 0, b = 5;
switch (x / 2 - 5) { float x = 0.1;
case 0: if(a++ && ++b)
printf("Zero "); printf("First\n");
break; else
case 1: if(b++ || a++)
printf("One "); printf("Second\n");
break; printf("%d %d\n", a, b);
case 5 / 5: if(x + 0.2 == 0.3)
printf("Five "); printf("Equal");
break; else
default: printf("Not Equal");
printf("Default "); return 0;
} }
return 0;
}
Second
Error: Switch case cannot have
16
two cases with the same constant
Not Equal
expressions.
c) d)
#include<stdio.h> #include <stdio.h>
void main(){ int main()
int w = 4, x = 3, y = 2, z = 1; {
w = x-- - ++y * z++ / w + (w & 1); int i = 5, j = 5;
printf("%d %d %d %d", w, x, y, z); while (i--) {
} while (j--) {
printf("%d%d ", i, j);
3232 }
}
return 0;
}Infinite loop

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy