Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Programming Problems Bank

Uploaded by

ua154405
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Programming Problems Bank

Uploaded by

ua154405
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

C Programming Questions 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:

(1 + 8/2 + (7 ∗ 3 + 49/7)/4 − 2)/2

Display your output with suitable output prompt messages to illustrate


your work.

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.

2. Write a program to input a 5-digit whole number and display it in reverse


order. For example, if the input is 53721, its output will be 12735.
3. Write a program to input a 5-digit whole number and display the sum of
its digits. For example, if the input is 53721, its output will be 18.
4. Write a program to input a number and display its additive and multi-
plicative inverse.
5. Jamal’s basic salary is input. His dearness allowance is 40% of his basic
salary, and his house rent allowance is 20% of his basic salary. Write a
program to calculate and display his gross salary.

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

12. Get a five-digit number as input and reverse the numbers.


13. Write a program that takes a five-digit number as input and prints the sum
and product of its digits. (Like 25364 yields 2+5+3+6+4 and 2*5*3*6*4
).
14. Calculate the Area of a circle with radius as input through the keyboard.
15. Determine the area of the cylinder by taking height & diameter from the
user.
2 × π × radius × (height + radius).
16. Write a program that calculates the arc length of a convex lens by taking
the radius of arc and angle made by arc(length = radius × angle)
17. Write a program that calculates the final Velocity of an object by taking
the following inputs from the user.
vi = initialvelocity, a = acceleration, t = timespan, f ormulavf = vi + at
18. Write a program that calculates the Focal length of a convex lens by tak-
ing the following inputs from the user. P = distance of object from lens,
Q = distance of image from lens, F = Focal Length

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.

−b±b2 −4ac ba b(b−1)a2


d= 2a + (1 + b)a + 1 + 1! + 2!

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:

• Total miles driven per day


• Cost per gallon of gasoline
• Average miles per gallon
• Parking fees per day
• Tolls per day

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.

4. Suppose two whole numbers are input. Write an algorithm to determine


whether one is a multiple of the other.
5. If an item’s cost price and selling price are input, write an algorithm to
determine whether the seller has made a profit or incurred a loss. Also,
determine how much profit he made or loss he incurred.

6. While purchasing certain items, a discount of 10% is offered if the quantity


purchased is more than 100. If quantity and price per item are input, write
a program to calculate and display the total expenses.
7. Write a program to find the roots of the quadratic equation. The coef-
ficients a, b, and c of the equation are input. The program should not
allow a = 0, and the discriminant b2 –4ac should be non-negative for the
real roots.

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

13. Write a program that implements basic Windows calculator operations.


Use suitable prompt messages to make the output more user-friendly.
14. Write a program in C++ to input a single character and print a message
it is a vowel if it is a vowel otherwise print the message it is a consonant.
Use the if-else structure and OR (||) operator only.

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.

Units Consumed Per Unit Cost


1 - 100 4.86
101 - 200 6.57
201 - 350 9.21
351 - 800 14.81
801 - 999 19.25
1000 - 2500 24.58
More than 2501 34.12

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:

• Having the user enter their weight, as a floating point number;


• Having the user enter whether they have an active or sedentary
lifestyle, as a character ’A’ for active or ’S’ sedentary;
• Use an if-else selection statement to compute the appropriate recom-
mended calories for the selected lifestyle.

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:

Monthly Sales Commission Rate


1.00 - 1000.00 2%
1001.00 - 5000.00 5%
over 5000.00 10%

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)

1 inch = 2.54 centimeters


1 gallon = 3.785 liters
1 mile = 1.609 kilometers
1 pound = .4536 kilograms

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:

• Underweight: less than 18.5


• Normal: between 25 and 29.9
• Overweight: between 25 and 29.9
• Obese: 30 or greater

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.

• Equilateral triangle: Triangle having two equal angles.


• Isosceles triangle: Triangle having all three equal angles.
• Right Triangle: A triangle having one angle equal to 90 degrees.

Enter First Angle: 90 Enter First Angle: 60


Enter Second Angle: 120 Enter Second Angle: 60
Enter Third Angle: 120 Enter Third Angle: 60
Triangle is invalid Triangle is Isosceles

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.

Figure 3: Cartesian plane

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:

No of Accidents Accident Surcharge


1 50
2 125
3 225
4 375
5 575
6 or more No insurance

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);

Gender Age Premium


Male Under 21 1500 + 200 * numTix
Male 21 to 29 1200 + 100 * numTix
Male Over 29 1000 + 100 * numTix
Female Under 21 1200 + 200 * numTix
Female 21 and over 1000 + 100 * numTix

• 5% tax should be applied if the salary is more than or equal to 7501


and less than 15000
• 12% tax should be applied if the salary is more than or equal to 15000
and less than 25000
• 18% tax should be applied if the salary is more than or equal to 25000
and less than 50000
• 24% tax should be applied if the salary is more than or equal to 50000
and less than 150000
• 27% tax should be applied if the salary is more than or equal to
150000 or above
3. Write a program that inputs marks obtained by a student in 6 differ-
ent subjects and calculates the average marks of the students. Now the
program should automatically decide the grade of the student as per the
following grading policy:
• A+ Grade If the average marks are more than or equal to 85%
• A Grade If the average marks are more than or equal to 80%

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.

4. Implement a program to print Pascal’s Triangle up to a certain number


of rows.
5. Create a program to find the sum of digits of a given three-digit number.
6. Write a C++ program to reverse a number using a loop.

7. Implement a program to check if a string is a palindrome.


8. Write a program to print the multiplication table of a given number.
9. Create a program to find the GCD (Greatest Common Divisor) of two
numbers.

10. Implement a program to check if a number is an Armstrong number.


11. Write a C++ program to find the roots of a quadratic equation.
12. Create a program to print the pattern: 1, 22, 333, 4444, ...

13. Implement a program to find the sum of natural numbers up to N.


14. Write a program to check if a number is a perfect number.
15. Create a program to calculate the power of a number using a loop.

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.

1 3 9 27 81 ... ... ... n

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

1! + 2! + 3! + 4! + ... ... ... Upper limit specified

29. Write a program to continuously input positive numbers to keep continu-


ing or enter zero to stop entering data. Now, the program should find and
display the largest number among all entered numbers. Use appropriate
prompt messages to illustrate the workings of the program.
30. Write a program that asks the user to input two numbers in two variables
x and n. Now find and properly display the sum of the following series.

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.

Enter the day of your call: 1


Enter the time your call started in 24-ht notation: 1415
Enter the length of your call in minutes: 20
The cost of your call is Rs. 480
Do you want to do another calculation? (1 for Yes and 0 to quit): 1
Enter the day of your call: 12
Bad input
Enter the day of your call: -2
Bad input
Enter the day of your call: 7
Enter the time your call started in 24-ht notation: 1230
Enter the length of your call in minutes: 40
The cost of your call is Rs. 360
Do you want to do another calculation? (1 for Yes and 0 to quit): 0
program terminates
33. Write a program to calculate the sum of the following series:
1 2 3 4 5 19
1 + 3 + 5 + 7 + 9 + ... + 37

34. A person invest $1000.0 in a saving account yielding 5% interest. Assum-


ing that all interest is left on deposit in the account. Calculate and print
the amount of money in the account at the end of each year for 10 years.
Use the following formula for determining these amounts.

a = p(1 + r)n where


p = original investment
r = annual interest rate
n = number of years
a = amount on deposit at the end of the nth year.

Sample output:

Year Amount on Deposit


1 1050.00
2 1102.50
3 1157.63
...
10 1628.89

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

Enter Your Selection: 1


Enter the amount to withdraw: 5555
Insufficient Funds
Do you want to do another transaction (y/n): y
ATM
1. Cash Withdrawal
2. Balance Transfer
3. Balance inquiry

Enter Your Selection: 1


Enter the amount to withdraw: 500
Your new balance is Rs. 500.00
Do you want to do another transaction (y/n): y
ATM
1. Cash Withdrawal
2. Balance Transfer
3. Balance inquiry

Enter Your Selection: 2


Enter the amount to transfer: 100
Enter the recipient’s account: 0214856
Your new balance is Rs. 400 Do you want to do another transaction (y/n):
n

4.1 Nested Loops


1. Used nested for loops to generate the patterns.

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

3. Develop a C++ program to play the ”Guess My Number” game. Your


program chooses the number to be guessed by selecting an integer at ran-
dom in the range of 1 to 100. The program then shows:

I have a number between 1 and 100


Can you guess my number?
You have a maximum of five chances.
Enter your guess here:

The player then inputs his guess and the program responds by displaying
one of the following messages:

1. Too low. Try again.


2. Too high. Try again.
3. Excellent! You guessed the number!

Game Play Rules:


If the player’s guess is incorrect, your program should loop until the player
finally gets the number right. Your program should keep telling the player
too high or too low (message no. 1 or 2 given above) to help the player
guess the correct answer. Also, show the number of tries that the user has
made in each step.

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

You might also like