0% found this document useful (0 votes)
10 views15 pages

Logical Questions

The document outlines various programming tasks in C, covering topics such as access control systems, online exam submissions, restaurant order processing, payroll systems, ATM simulations, number guessing games, password strength checking, and array manipulations. Each task includes specific requirements, example inputs and outputs, and hints for implementation. The tasks emphasize the use of control structures, functions, and data structures to solve practical problems.

Uploaded by

kebawa4655
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)
10 views15 pages

Logical Questions

The document outlines various programming tasks in C, covering topics such as access control systems, online exam submissions, restaurant order processing, payroll systems, ATM simulations, number guessing games, password strength checking, and array manipulations. Each task includes specific requirements, example inputs and outputs, and hints for implementation. The tasks emphasize the use of control structures, functions, and data structures to solve practical problems.

Uploaded by

kebawa4655
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/ 15

programming questions

1. Secure Access Control System

A company has implemented a multi-level access control system for employees. The access
is determined based on:

●​ Role: 1 (Admin), 2 (Manager), 3 (Employee), 4 (Guest)


●​ Time of Access: 9 AM - 6 PM (only Admin and Manager can enter anytime)
●​ Weekend Restriction: Only Admins can enter on weekends.
●​ Incorrect Password: After three failed attempts, the user is locked out.

Task:

Write a C program that:

1.​ Asks the user to input their role (1-4) and password.
2.​ Checks if the password is correct (for simplicity, assume password = 1234 for all
roles).
3.​ Validates whether the user can enter based on their role, time, and weekday/weekend.
4.​ If the user enters the wrong password three times, deny further access.

Example Input & Output:


Enter your role (1-4): 2
Enter the current hour (0-23): 18
Enter the day (1 for weekday, 0 for weekend): 1
Enter password: 1111
Incorrect password! Try again.
Enter password: 2222
Incorrect password! Try again.
Enter password: 1234
Access granted! Welcome, Manager.​

Hints:

●​ Use if-else statements for role-based conditions.


●​ Use a loop to track password attempts.
●​ Implement time-based and weekend restrictions.
2. Online Exam Submission System

A university has an online exam system with the following rules:

●​ A student can submit their exam only if:


○​ The exam timer hasn’t reached zero.
○​ They have answered at least 50% of the questions.
●​ If less than 50% of questions are answered, approval is needed.
●​ If a student logs in from two locations, they are disqualified.
●​ If the student attempts to submit more than twice, they are logged out.

Task:

Write a C program that:

1.​ Takes number of questions, answered questions, timer (minutes left), and login
attempt status (0 or 1 for multiple logins) as input.
2.​ Checks conditions and decides if the student can submit, needs approval, is
auto-submitted, or is disqualified.

Example Input & Output:​

Enter total number of questions: 10


Enter number of answered questions: 4
Enter remaining time (minutes): 5
Is there a multiple login detected? (1 for Yes, 0 for No): 0
Approval needed to submit.

Hints:

●​ Use if-else to check answered questions and timer status.


●​ Implement nested conditions for login attempts.
●​ Track submission attempts and restrict to two tries.
3. Restaurant Order Processing System (Switch-Case & If-Else)

A restaurant billing system allows customers to place orders using item codes. Each item
has a fixed price:

Item Code Item Price ($)

1 Burger 5.99

2 Pizza 8.49

3 Pasta 6.99

4 Coffee 2.99

5 Juice 3.49

●​ If the total bill is $20 or more, the customer gets a 10% discount.
●​ If the customer orders more than 5 items, they get a free coffee.
●​ If the total bill is $50 or more, they get a 15% discount.

Task:

Write a C program that:

1.​ Asks the user to select items (1-5) and quantity.


2.​ Calculates the total bill amount.
3.​ Applies discounts based on the total bill.
4.​ Checks if the customer qualifies for a free coffee.

Example Input & Output:


Enter item code (1-5): 2
Enter quantity: 3
Enter item code (1-5): 3
Enter quantity: 2
Enter item code (1-5): 4
Enter quantity: 1

Total bill before discount: $36.94


10% Discount applied.
Final bill: $33.24​
Hints:

●​ Use a switch-case for item selection.


●​ Use if-else conditions for discount logic.
●​ Use a loop to allow multiple item selection.
Programs on Looping Statement

4. Employee Payroll System (For Loop & If-Else)

A company calculates employee salaries based on the following conditions:

●​ Employees work for 5 days a week, with 8 hours per day as standard working
hours.
●​ If an employee works more than 8 hours in a day, the extra hours are considered
overtime.
●​ Overtime pay is 1.5 times the regular hourly rate.
●​ If an employee works for less than 5 days, a 20% salary deduction is applied.
●​ The hourly rate is $20 per hour.

Task:

Write a C program that:

1.​ Takes the number of days worked and daily working hours as input.
2.​ Uses a for loop to calculate the total salary, including overtime pay.
3.​ Applies deductions if the employee worked less than 5 days.

Example Input & Output:


Enter number of days worked: 5
Enter daily working hours (5 values): 8 9 10 8 7

Total salary before deduction: $860


Final salary: $860

Enter number of days worked: 4


Enter daily working hours (4 values): 8 9 10 6

Total salary before deduction: $580


20% deduction applied.
Final salary: $464

Hints:

●​ Use a for loop to iterate through daily hours.


●​ Check for overtime hours and calculate extra pay.
●​ Apply salary deduction if required.
5. ATM Withdrawal Simulation (While Loop & Break Condition)

An ATM allows customers to withdraw money under the following conditions:

●​ The maximum withdrawal limit is $500 per transaction.


●​ The ATM only dispenses $10, $20, $50, and $100 bills.
●​ A user can attempt withdrawal up to 3 times before the ATM locks them out.
●​ If the user enters an amount that cannot be dispensed with available bills, they are
asked to retry.

Task:

Write a C program that:

1.​ Uses a while loop to allow up to 3 attempts for withdrawal.


2.​ Checks if the amount is within the allowed range (≤ $500).
3.​ Ensures the amount can be dispensed using available bills.
4.​ If the user fails 3 times, they are locked out.

Example Input & Output:


Enter withdrawal amount: 530
Invalid amount! Try again.
Enter withdrawal amount: 275
Invalid amount! Try again.
Enter withdrawal amount: 500
Transaction successful! Dispensed: 5 x $100

Enter withdrawal amount: 55


Invalid amount! Try again.
Enter withdrawal amount: 62
Invalid amount! Try again.
Enter withdrawal amount: 23
Invalid amount! You are locked out.

Hints:

●​ Use a while loop to track attempts.


●​ Check if the entered amount is divisible by available denominations.
●​ Use break when a valid amount is entered.
6. Number Guessing Game (Do-While Loop & Random Number Generation)

A simple number guessing game where the user must guess a randomly generated number
between 1 and 100.

●​ The user gets unlimited attempts but must guess within 10 tries to win.
●​ After each guess, the program should give hints:
○​ "Too high!" if the guess is greater than the target number.
○​ "Too low!" if the guess is lower than the target number.
●​ If the user fails within 10 attempts, they lose the game.

Task:

Write a C program that:

1.​ Uses a do-while loop to keep asking for guesses.


2.​ Ends when the user guesses correctly or exceeds 10 attempts.
3.​ Displays appropriate messages after each attempt.

Example Input & Output:

Guess a number (1-100): 50


Too high! Try again.
Guess a number (1-100): 30
Too low! Try again.
Guess a number (1-100): 40
Congratulations! You guessed it right in 3 attempts.

Guess a number (1-100): 80


Too high! Try again.
Guess a number (1-100): 70
Too high! Try again.
(After 10 attempts)
Sorry! You couldn't guess the number. The correct number was 45.

Hints:

●​ Use rand() function from stdlib.h to generate the random number.


●​ Use a do-while loop to keep asking for input.
●​ Track attempts and end the game after 10 tries
7. Password Strength Checker (For Loop & Character Validation)

A company requires strong passwords with the following rules:

●​ The password must be at least 8 characters long.


●​ It must contain at least one uppercase letter, one lowercase letter, one number, and
one special character (@, #, $, %, &, !, etc.).
●​ The program should keep asking for a password until a valid one is entered.

Task:

Write a C program that:

1.​ Uses a for loop to validate each character of the entered password.
2.​ Uses a while loop to keep asking for a password until a valid one is entered.

Example Input & Output:


Enter a password: pass123
Weak password! It must contain at least one uppercase letter and one special character.
Enter a password: Pass@word1
Strong password! Registration successful.

Hints:

●​ Use a for loop to check each character in the string.


●​ Use flags (int hasUpper = 0;) to track required conditions.
●​ Use while(1) loop to keep asking until a valid password is entered.
Functions with Arrays:​
Question 8: Array Rotation using Functions

A company maintains a list of employees based on their joining order. However, due to a
shift change, the list needs to be rotated.

Task:

●​ Write a function rotateArray(int arr[], int n, int k, char direction)


that rotates an array of n elements k times.
●​ If direction == 'L', rotate left.
●​ If direction == 'R', rotate right.
●​ Call this function from main(), input an array from the user, and display the
rotated array.

Example:

Input: arr[] = {1, 2, 3, 4, 5}, k = 2, direction = 'L'


Output: {3, 4, 5, 1, 2}

Input: arr[] = {1, 2, 3, 4, 5}, k = 3, direction = 'R'


Output: {3, 4, 5, 1, 2}

Constraints:

●​ Use modulus (%) to handle cases where k > n.


●​ Use functions for modularity.
Question 9: Frequency Count of Array Elements Using Functions

A teacher wants to analyze student test scores. You are given an array of test scores, and your
task is to find how many times each unique score appears.

Task:

●​ Write a function countFrequency(int arr[], int n) that prints each unique


number and its frequency.
●​ Do not use built-in libraries (like map or unordered_map).
●​ Call the function from main().

Example:
Input: arr[] = {90, 85, 90, 78, 85, 85, 92}
Output:
90 → 2 times
85 → 3 times
78 → 1 time
92 → 1 time

Constraints:

●​ You must use a secondary array to store counted elements.


●​ No duplicate printing (i.e., 85 should be counted only once).

Question 10: Matrix Row and Column Sum using Functions

A developer needs a system to quickly calculate the sum of each row and column in a 2D
matrix.

Task:

●​ Write a function rowSum(int mat[][SIZE], int rows, int cols) that


calculates and prints the sum of each row.
●​ Write another function colSum(int mat[][SIZE], int rows, int cols) that
calculates and prints the sum of each column.
●​ Call both functions from main(), inputting the matrix from the user.

Example:
Input:
Matrix =
1 2 3
4 5 6
7 8 9

Output:
Row Sums: 6, 15, 24
Column Sums: 12, 15, 18

Constraints:

●​ The program should work for any NxM matrix, not just 3x3.
●​ The functions must not modify the original matrix.

Question 11: Swapping Pointers Without Using a Temporary Variable

You need to swap two integer pointers without using a temporary variable.

Task:

●​ Write a function swapPointers(int *a, int *b) that swaps two integer pointers
without using a third pointer.
●​ Call the function from main() and print the values before and after swapping.

Example:

int x = 10, y = 20;


int *ptr1 = &x, *ptr2 = &y;

Before Swap: ptr1 → 10, ptr2 → 20


After Swap: ptr1 → 20, ptr2 → 10

Constraints:

●​ Do not use temp or extra variables.


●​ Use pointer manipulation (* and &).
Question 12: Dynamic Memory Allocation & Array Reversal Using Pointers

Write a program that dynamically allocates an array, fills it with user input, and then
reverses it using only pointers.

Task:

●​ Allocate memory for an array of n integers using malloc().


●​ Use a function reverseArray(int *arr, int n) to reverse the array without
using indexing (arr[i])—only pointer arithmetic.
●​ Print the array before and after reversal.

Example:
Input: arr[] = {1, 2, 3, 4, 5}
Output:
Before: 1 2 3 4 5
After: 5 4 3 2 1

Constraints:

●​ No use of arr[i], only pointer arithmetic (*(arr + i)).


●​ Use malloc() and free().
Question 13: Function Pointer for Sorting in Ascending/Descending Order

Write a program that sorts an array in either ascending or descending order, depending
on the function pointer passed.

Task:

●​ Implement two functions:


○​ int ascending(int a, int b) → returns 1 if a > b
○​ int descending(int a, int b) → returns 1 if a < b
●​ Write a function void sort(int *arr, int n, int (*cmp)(int, int)) that
sorts the array based on the comparison function pointer.
●​ In main(), allow the user to choose ascending or descending sorting.

Input: arr[] = {5, 2, 9, 1, 6}, order = 'D'


Output: {9, 6, 5, 2, 1} (Descending)

Input: arr[] = {5, 2, 9, 1, 6}, order = 'A'


Output: {1, 2, 5, 6, 9} (Ascending)

Constraints:

●​ Use function pointers to decide sorting order.


●​ No built-in sorting functions (qsort, sort()).

Question 14: Nested Structures for Employee Database

A company stores employee details, including personal information and job details,
using nested structures.

Task:

●​ Define a structure Employee with:


○​ name (string)
○​ id (int)
○​ salary (float)
○​ A nested structure JobDetails containing:
■​ designation (string)
■​ experience (int)
●​ Write a function void display(Employee emp) to print all details of an
employee.
●​ Take input for N employees and display their details.

Example:
Input:
Name: John Doe, ID: 101, Salary: 50000, Designation: Manager, Experience: 5 years

Output:
Employee ID: 101
Name: John Doe
Salary: 50000
Designation: Manager
Experience: 5 years

Constraints:

●​ Use nested structures.


●​ Dynamically allocate memory for name and designation using malloc().

Question 15: Structure Array for Student Grade Management

A university tracks student scores and determines their grades based on the average
score.

Task:

●​ Define a structure Student with:


○​ name (string)
○​ roll_no (int)
○​ marks (array of 5 subjects)
○​ average (float)
○​ grade (char)
●​ Write a function void calculateGrade(Student *s) to compute the average
marks and assign a grade:
○​ A: average >= 90
○​ B: average 80-89
○​ C: average 70-79
○​ D: average 60-69
○​ F: average < 60
●​ Take input for N students, calculate grades, and display results.
Example:

Input:
John, Roll: 101, Marks: {85, 90, 78, 92, 88}

Output:
Name: John
Roll No: 101
Average: 86.6
Grade: B

Constraints:

●​ Use an array of structures.


●​ Use pointer notation to access structure members inside functions

Question 16: Pointer to Structure & Dynamic Memory Allocation

A hospital maintains patient records dynamically.

Task:

●​ Define a structure Patient with:


○​ name (string)
○​ age (int)
○​ disease (string)
○​ admission_date (string)
●​ Use dynamic memory allocation to create an array of N patients.
●​ Write a function void displayPatients(Patient *p, int n) to print details.
●​ Take input for N patients, display all records, and free memory.

Example:
Input:
Patient 1: John, Age: 45, Disease: Flu, Admission Date: 12-01-2024
Patient 2: Alice, Age: 30, Disease: Fever, Admission Date: 15-01-2024

Output:
Patient Name: John
Age: 45
Disease: Flu
Admission Date: 12-01-2024

Patient Name: Alice


Age: 30
Disease: Fever
Admission Date: 15-01-2024

Constraints:

●​ Use malloc() to dynamically allocate structure memory.


●​ Use a pointer to structure in the function.

You might also like