Programming Problems Bank
Programming Problems Bank
Author Name
February 28, 2024
Abstract
This document contains a compilation of C programming questions
covering various topics, including sequential structure, selection structure,
loops, arrays, functions, pointers, strings, structures, and file handling.
Contents
1 Introduction 2
2 Sequential Structure 3
2.1 Easy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Expert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Selection Structure 7
3.1 Easy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3 Expert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.4 Nested-If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Loops 15
4.1 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5 Arrays 21
6 Functions 21
7 Pointers 21
8 Strings 21
9 Structures 21
10 File Handling 21
1
1 Introduction
Author Name Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce
at justo vitae lacus congue vehicula. Proin ullamcorper neque eu tellus dictum,
in consequat augue sagittis. Pellentesque habitant morbi tristique senectus et
netus et malesuada fames ac turpis egestas. Integer tincidunt bibendum ligula,
ac tincidunt turpis sollicitudin et.
2
2 Sequential Structure
2.1 Easy
1. Write a program that will show the following outputs simply by using cout
statements.
Figure 1: Shapes
2. Write a program that calculates the accurate answer to the following ex-
pression:
3. Write a program that inputs values for all used variables and write the
equivalent C++ expressions. Display the resulting values of p,z, and s
respectively with suitable messages.
• p = 2(1 + b)
• z = 2(p/q)2
• s= 1
2mv 2
4. Write a program that inputs a character and displays its ASCII code.
Sample Output:
Enter Character: a
The ASCII value of character a is 97
5. Write a program that inputs weights in pounds and converts them into
equivalent kilograms.
(Hint: Kg = p/2.2046)
6. Write a program that inputs the coordinate values of two points x and y
into variables. The program should automatically calculate and display
the distance between these two points using the distance formula given
below:
p
Distance = (x2 − x1 )2 + (y 2 − y 1 )2
3
7. Write a program to input temperature in Fahrenheit and convert and
display it into Celsius. The conversion formula is given below:
C = 59 (F − 32)
8. Input an Even & Odd number through the keyboard, Multiply Even with
5 and Odd with 3 and subtract the result from 1000, Print the difference.
9. Write a program to input the radius of a circle and find the diameter,
circumference, and area of that circle and display it.
10. Write a program that input four integers a, b, c and d from user and cal-
culate the following: 6a2 − 2b2 + 5ab + 3bc − d
11. Write a program to input two numbers and calculate and display their
sum, difference, product, quotient, and remainder.
12. Write a program that inputs the height, weight, and length of a cubic.
Calculate the volume of that cubic object.
(Hint: Volume of cube V = length * weight * height)
13. Write a program that accepts distance in Km from the user and converts
it into meter & cms.
14. Write a program that inputs an alphabet and should display the next five
characters on the output screen.
15. Write a program that inputs a number from the user and displays its
square and cube.
16. Write a program that inputs a number from the user and prints its table.
2.2 Average
1. Write a program that inputs your age in years. It converts it into months,
weeks, days, hours, and minutes.
4
6. Two numbers are input in A and B. Write a program to swap the numbers
and display them by two different strategies i.e., by using some additional
variable and without it.
7. Write a program to input a 3-digit number. It should calculate and display
the sum of the squares of its digits.
8. A car can travel 22.6 km on 1 liter of fuel. Write a program that inputs
petrol in liters and displays how far the car can be covered.
9. Write a program that converts temperature from Centigrade to Fahren-
heit.
10. Write a program that allows the user to enter two fractional values and
print out their sum.
Sample Output:
Enter the first fraction in the form of a/b: 3/2
Enter the second fraction in the form of c/d: 2/3
3/2 + 2/3 = 13 / 6
11. Write a program that takes input in three variables a,b, and c. The
program should find the root r1 and r2 of the quadratic equation using
the following formula:
√
−b± b2 −4ac
x= 2a
5
1 1 1
F = P + Q
19. Write a program that takes marks obtained by a student in five different
subjects to find out the aggregate marks and percentage.
20. What is the value of each after evaluation? [Hint: Use only a single
statement of cout]
• 4 − 3 + 23%8/4 ∗ 4
• 9 − 8.0 + 7%6/5.0 ∗ 4.0
• 2%2 + 2 ∗ 2 − 2/2
21. Write a C++ program that inputs a 4-digit value from the user (for ex-
ample 6382) and displays a result with an increment of 1 in each digit (i.e.
7493).
22. Write a program that asks the user to type the price without tax of one
kilogram of tomatoes, the number of kilograms you want to buy and the
tax in percent units. The program computes and displays the total price
including taxes.
23. Write a program that asks the user it input the amount in dollars and
current currency rate. The program should convert it into rupees using a
given currency rate.
2.3 Expert
1. Write a program that inputs values for all used variables and write the
equivalent C++ expressions. Display the resulting value of d with a suit-
able message.
2. Write a program for the distance (in km) between two cities, the vehicle’s
speed, and the distance covered in one liter of fuel. The program should
calculate the time required to reach the destination. it also tells the user
how much fuel is required to reach the destination.
3. Write a program that inputs times in seconds in a long variable. The
program should compute the number of hours, minutes, and seconds and
display them on the console screen.
Output screen:
Time in seconds: 18725
5 Hours 12 Minutes 5 Seconds
4. When a brick is dropped from a tower, it falls faster and faster until it
hits the earth. The speed v is given by
6
v = ( 12 ) × (g ∗ t2 )
Here v is the speed in feet per second, t is the time in seconds, and g is
32.174. Write a program that will take several seconds as input and will
display the speed. The program should run like this:
Number of Seconds: 5.4
Speed of the brick: 469.092 feet per second
5. Create a program that calculates your daily driving cost so that you can
estimate how much money could be saved by carpooling. The program
should input the following information and display the user’s cost per day
of driving to work:
3 Selection Structure
3.1 Easy
1. Write a program that inputs two numbers and it decides whether they are
equal or not.
2. Write a program that inputs three numbers from the user and displays
which number is greater.
3. Write a program that inputs a number from the user and should decide
whether the given number is even or odd.
7
8. Write an algorithm to input any number between 1 and 7 inclusive. If
1 is input, the output should be Monday, for 2 the output should be
Tuesday, and so on. For invalid input, the message “wrong input” should
be displayed.
9. Write a program that inputs 6 numbers and the program should find the
largest and smallest number among these numbers. Use suitable prompt
messages.
10. Write a program that inputs a number and should translate it into words
in descriptive form. For example: if the user enters 15 the output should
be ”Fifteen”. (Note: tries to cover first 20 integer numbers)
11. Writing a program that accepts a character and determines whether the
character is a lowercase letter or uppercase letter. A lowercase letter is
any character that is greater than or equal to ’a’ and less than or equal
to ’z’. If the entered character is a lowercase letter, display the message
”Entered character is the lowercase letter” OR simply display the message
”Entered character is the uppercase letter” or else ”Entered character is
not alphabet”.
12. Write a program that inputs temperature and displays a message as fol-
lows:
Temperature Message
Greater than 35 Hot day
Between 25 and 35 (inclusive) Pleasant Day
Less than 25 Cool day
3.2 Average
1. Write a program to input an integer from 1 to 10. Using the Switch-case
control structure, output the number in descriptive form.
For example,
if the input is 7, the output should be ”Seven”
if the input is 4, the output should be ”Four”
if the input is 9, the output should be ”Nine” etc.
8
2. Write a program that asks the user to input the previous and present read-
ings of your electric meter. The program should automatically calculate
the total number of units consumed. The unit price should be applied as
per the following defined policy.
3. The current year and the year in which the employee joined the organiza-
tion are input. If the number of years for which the employee has served
the organization is greater than 3 then a bonus of Rs. 20000 is given to
the employee. if the no. of years served is greater than 3 then the bonus
should be output otherwise the program should not do anything.
4. Write a program that asks the user to input a year and determines whether
the year is a leap year or not. Remember a year is a leap year if it is
divisible by four, except that any year divisible by 100 is a leap year only
if it is divisible by 400.
5. In a company an employee is paid as under:
If his basic salary is less than Rs. 15000, then HRA = 10% of basic salary
and DA = 90% of basic salary. If his salary is either equal to or above
Rs.15000, then HRA = Rs. 5000 and DA = 98% of basic salary. If the
employee’s salary is input, write a program to find his gross salary.
6. Write a program that recommends the number of calories a person should
eat each day. Calories are units of energy found in all foods. Base your
recommendation on the person’s weight and whether the person has an
active or sedentary(inactive) lifestyle. if the person is sedentary, that
person’s activity factor is 13. If the person is active, that person’s activity
factor is 15. Multiply the activity factor by the person’s weight to get
the recommended number of calories. Let’s start your program using the
following assumptions:
9
• Print out your results on the screen.
7. Write a program to get three numbers from the user for integer variables
a,b and c. if a is not zero, the program should determine whether it is the
common divisor of b and c or not.
8. Write a program that inputs year and month. It displays the number of
days in the month. For example, if the user enters 2012 in the year and 2
in a month, the program should display: February 2012 has 29 days.
9. Write a program for the sales commission lab that accepts the first name
of a salesperson and their total sales for the month. Sales commissions
are calculated as follows:
10. Write a program that asks the user to enter a choice for conversations as
shown below: The program should then require a value to convert and
Figure 2: Menu
show the converted value on the console. For example, if the user wants
to convert the miles into kilometers. It should enter 3 in choice and then
enter the value to be converted say 10. The output should be 16.09 (as 1
mile = 1.609km) (Hint: Consider the following metric rules)
11. Write a C++ program that determines whether the roots of a quadratic
polynomial are real or imaginary. If the roots are real, find them and
10
assign them to variables root1 and root2. If no real roots exist, print the
message ”no real roots”
12. A bank in your town updates its customers’ accounts at the end of each
month. The bank offers two types of accounts: savings and current ac-
counts. Every customer must maintain a minimum balance. If a cus-
tomer’s balance falls below the minimum balance, there is a service charge
of $10.00 for savings accounts and $25 for checking accounts. If the bal-
ance at the end of the month is at least the minimum balance, the account
receives interest as follows:
• Savings accounts receive 4% interest.
• Checking accounts with balances of up to $5000 more than the min-
imum balance receive 3% interest;otherwise, the interest is 5%
Write a program that reads a customer’s account number (int type), ac-
count type (char s for savings, c for checking), minimum balance that
the account should maintain, and current balance. The program should
then output the account number, account type, current balance, and an
appropriate message.
13. Write a program of a BMI calculator application that reads the user’s
weight and height and then calculates and displays the user’s body mass
index. The formula for calculating BMI is:
• BM I = W eight/(Height ∗ Height)
Note: (Height is in meters)
Also, the application should display the following information from the
scale provided below:
14. Write a program that calculates the shopping bill of the customer. The
user enters the price of an item and the quantity of the item. If the
customer’s bill is $1000 or more the customer receives a 5% discount.
15. Write a program that calculates the pay a user has earned. The user
enters the number of hours he worked. If the number of hours worked by
the user is less than or equal to 40 the user receives $10 for every hour
worked. If the hours worked exceed 40 then the user receives $12 for every
work over 40.
11
16. Write a program that gets 3 angles from the user and determines whether
the triangle is valid or not. If it is a valid, triangle then determine if it is
a right angle, equilateral, or isosceles triangle.
17. Write a program that prompts the user to input the x-y coordinate of a
point in a Cartesian plane. The program should then output a message
indicating whether the point is at the origin, located at the x or y-axis, or
appears in a particular quadrant.
18. Write a program that reads in two values from the user
• Manufacturing cost of product
• Selling price of a product
And determines and prints whether a profit or a loss is made. If profit is
greater than 10% calculate and display a bonus of 1% from profit to the
employee.
Expected Output:
12
Enter manufacturing cost of product: 700
Enter selling price of product: 1100
Profit = 400
36% profit is made from this product
The bonus for employees is 4
3.3 Expert
1. Write a program to determine the cost of an automobile insurance pre-
mium, based on the driver’s age and the number of accidents that the
driver has had. The basic insurance charge is $500. There is a surcharge
of $100 if the driver is under 25 and an additional surcharge for accidents
would be applied as per the following policy:
2. Write a program that asks the user to input marks of 6 subjects. The
program should compute total marks, average, GPA, and CGPA as per
UOL policy.
3. Write a value of each variable after the statement is executed. You can
check your answer by coding this function and using cout statements after
completing the table:
3.4 Nested-If
1. A car insurance company determines your premium. The user provides
the requested information and the program determines the insurance pre-
mium. The insurance premium is based on the user’s gender, age, and
number of traffic tickets he has received (numTix). The formula for de-
termining the insurance premium is:
2. Write a program that inputs the salary of an employee from the user and
applies the tax deductions as per a defined policy. The program should
compute the tax amount and display the tax and net salary of the user.
The defined tax deduction policy is as follows:
• No tax should be applied for the employees having 7500 or below
salary
13
i j k btype btype2
int i = 0;
int j = 1;
int k = 5;
bool btype = true;
bool btype2 = f alse;
btype = 100 < 7 && !(5 > 10) || !btype2 &&(j == 0);
btype = 11 > 5 && (j = 9) || (i = 4);
btype = 10 < 4 || (i = 1) && (j = 7);
btype = (i = 100) || (j = 0) && (k = 12);
btype = (k = 0) && (j = 1) || (i = 13);
btype = (k = 1) && (btype2) || (j = 2);
btype = (k = 3) && (j = 5) || (i = 0) && btype2 = true);
btype = (j = 8) || (k = 9) && (i = 1) || btype2 = f alse);
btype = (k = 0) && (j = 5) || (i = 0) && btype2 = true);
i = j = (k = 5) && (11 > 5);
14
• A- Grade If the average marks are more than or equal to 78%
• B+ Grade If the average marks are more than or equal to 74%
• B Grade If the average marks are more than or equal to 70%
• B- Grade If the average marks are more than or equal to 68%
• C+ Grade If the average marks are more than or equal to 64%
• C Grade If the average marks are more than or equal to 60%
• C- Grade If the average marks are more than or equal to 56%
• D Grade If the average marks are more than or equal to 50%
• F Grade If the average marks are less than 50%
4 Loops
1. Write a program that inputs an upper-limit number from the user and
generates a Fibonacci series up to that limit using a loop. Use suitable
separators and tabs to display the series properly.
2. Implement a program to find the factorial of a number using a loop.
3. Write a C++ program to check if a given number is a prime number. Also,
prompt whether the user wants to continue (Y/N). If the user enters Y
the program should provide a chance to test a number, otherwise exits.
15
16. Implement a program to find the sum of the series: 1+1/2+1/3+...+1/N .
17. Write a C++ program to find the LCM (Least Common Multiple) of two
numbers.
18. Create a program to generate Floyd’s Triangle.
19. Implement a program to find the square root of a number without using
the sqrt function
20. Write a C++ program to count the number of digits in a given number.
21. Write a program that displays all possible odd numbers from 1 to 100 also
calculate and display the sum of all these odd integers. Also, draw the
flowchart of the program.
22. Write a program that displays all possible even numbers from the given
range and also calculates and displays the sum of all these even integers
using a single loop structure. Draw the flowchart of the program.
23. Write a program that inputs the number of students in the class. It then
inputs the marks of each student and displays the highest and second-
highest marks among these students.
24. Write a program that inputs two numbers from the user and displays the
first number raised to the power of the second number.
25. A fabulously wealthy but unthinking king agreed to give a beggar one cent
and double the amount for 24 days. Using this information, write, run,
and test a C++ program that displays how much the king must pay the
beggar on each day. The output of your program should appear as follows:
Day Amount Owed
1 1
2 2
3 4
. .
. .
24 .
26. Convert a binary number to decimal. For this problem, you can input the
total number of digits constituting the binary number input the binary
digits one by one from the user, and print its decimal equivalent in the
end.
27. Write a program that accepts an upper limit from the user and it should
display the following series up to the upper limit specified in a n variable.
16
28. Write a program that accepts an upper limit from the user and it should
display and compute the sum of the following series up to the upper limit
specified
x + x2 + x3 + x4 + x5 + .........xn
31. Write a program that prints the ASCII table (from 33 to 256) on the
screen. Remember:
Character ASCII Code
! 33
” 34
# 35
32. Write a program that computes the cost of a long-distance call. The
following rate schedule is used for calculating the cost of a call.
(a) Any call started between 8:00 AM and 6:00 PM, Monday through
Friday, is billed at the rate of Rs. 24/ - per minute.
(b) Any call starting before 8:00 AM or after 6:00 PM, Monday through
Friday, is charged at the rate of Rs. 15/- per minute.
(c) Any call started on a Saturday or Sunday is charged at the rate of
Rs. 9/- per minute.
In your program, you will ask the user to provide input for three things.
(a) The day of the week
(b) The time the call started
(c) The length of the call in minutes
The day of the week will be read as a single digit with the following con-
ventions:
1 2 3 4 5 6 7
Mon Tue Wed Thr Fri Sat Sun
For the time the call started, the user will enter the time in 24-hour nota-
tion. So 1:30 pm is entered as 13:30. You can assume that the user only
enters valid inputs.
The length of the call shall be input as an int. You can assume the user
17
rounds the length of the call to a whole number. The user always enters
a valid integer value here.
The following is what your program should print out.
Sample output:
35. Design an ATM software, that will display a menu to the user when exe-
cuted given below:
18
1. Cash Withdrawal
2. Balance Transfer
3. Balance inquiry
You will ask the user to enter the number of corresponding options like
1,2, or 3. And will operate accordingly. If the user enters an invalid op-
tion then an error message will be displayed. The program will ask the
user again and again until the user quits. Remember users can’t withdraw
an amount that is greater than the current balance. And you don’t need
to worry about what account number is entered in the second option at
present. You can set the initial balance of Rs. 1000.
An example is shown below:
ATM
1. Cash Withdrawal
2. Balance Transfer
3. Balance inquiry
19
2. Write a program that asks the user to input two numbers in four vari-
ables x,y,m, and n. Now computer and properly display the following
summation.
n X
X m
result = (xk + y j ) (1)
j=0 k=0
The player then inputs his guess and the program responds by displaying
one of the following messages:
Game Termination:
If the user guesses the number right, ask the user if he or she wants to ”Play
again (enter Y or N)”, if the user selects ’Y’ the game should restart. If
the players could not guess until the 5th attempt the program terminates
with the game lost message.
4. Create Tic-Tac-Toe game starts with painting a nice blank tic-tac-toe grid.
Use court statements to print the grid. Once your grid is drawn nicely,
implement game logic. You will need to maintain the following variables
in your code:
• 9 char variables to fill the grid’s cells
• One int variables to switch between Player 1 and Player 2.
• Two int variables for user input. One for the row number and one
for the column number of the grid.
Player 1 should play with X’s and player 2 should play with O’s. Player
1 should always start first. When your program asks for user input, it
20
should determine whether or not the input is valid. If the input is invalid,
then the player should prompted again. The player should be prompted
for input again and again until he. or she enters a valid input. Examples
of invalid inputs are:
• A number less than 1 or greater than 3. Entering 5 for the row
number makes no sense in tic tac toe.
• An input for a space on the grid that is not empty.
After every run, an updated grid should be printed on the screen some
examples of the grid are:
Your program should also detect wins and draws at the end of every
turn. You should code these using suitable if-conditions. The game should
terminate when a player wins or if the game ends in a draw. A message
should also be displayed at the end of the game, starting with whether
player 1 won player 2 won, or whether the game was drawn. A running
example is as follows:
5 Arrays
6 Functions
7 Pointers
8 Strings
9 Structures
10 File Handling
21