100% found this document useful (2 votes)
184 views12 pages

Practice Exercises For C++

The document contains tasks related to conditional statements, looping, arrays, and functions in C++. Task 1-2 involve if-else conditional statements to calculate speeding fines and employee allowance/rent based on salary. Task 3-5 involve looping - Task 3 uses do-while to calculate square numbers, Task 4 prints a table using a while loop, and Task 5 uses nested for loops to print a pattern. Tasks 6-17 involve one-dimensional arrays - reading/writing elements, calculating sums/products, merging/modifying arrays. Tasks 18-21 demonstrate functions - calculating Fibonacci numbers, grade calculation with switch, a basic calculator with switch, and grace marks calculation with switch.

Uploaded by

Abdullah
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
100% found this document useful (2 votes)
184 views12 pages

Practice Exercises For C++

The document contains tasks related to conditional statements, looping, arrays, and functions in C++. Task 1-2 involve if-else conditional statements to calculate speeding fines and employee allowance/rent based on salary. Task 3-5 involve looping - Task 3 uses do-while to calculate square numbers, Task 4 prints a table using a while loop, and Task 5 uses nested for loops to print a pattern. Tasks 6-17 involve one-dimensional arrays - reading/writing elements, calculating sums/products, merging/modifying arrays. Tasks 18-21 demonstrate functions - calculating Fibonacci numbers, grade calculation with switch, a basic calculator with switch, and grace marks calculation with switch.

Uploaded by

Abdullah
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/ 12

PART#1 (IF-ELSE CONDITIONAL STATEMENT)

Task#1:

Write a C++ program, which ask for actual speed and speed limit of a car from user, and calculate the
over limit. Over limit should meet the following condition. Also draw a flowchart of the program as well.

 If over limit greater than or equal to 25, then charge a fine of $215.
 If over limit is less than 25 but not 0, then there is no charge for that.

Task#2:

Write a C++ Program, which takes the basic salary of an employee as input, and calculate the allowance,
house rent and gross salary. And display the gross salary of the employee by meeting following
conditions. Also draw flowchart of the program as well.

 If the basic salary is greater than or equal to $4500, employee will get 15% allowance of the basic
salary and 12% house rent of the basic salary.
 If the basic salary is less than $4500 but greater than $1500, employee will get 10% allowance of
the basic salary and 5% house rent of the basic salary.
 If the basic salary is less than $1500, employee won’t get any allowance or house rent

.
PART#2 (LOOPING)

Task#3 (Perform using Do While Statement)

The square of an integer n is normally calculated by multiplying n by itself, that is, n 2 = n * n.


Calculate n2 is by adding the first n odd integers as shown. Resultant screen must show the
following output pattern.

1 + 3 + 5 = 9 = 32
1 + 3 + 5 + 7 = 16 = 42
1 + 3 + 5 + 7 + 9 = 25 = 52
Task##4

Write a program segment (including a while loop) that reads one number into a variable named n and
reads another number into a variable called maxPower. Then your statements should raise n to each
power 0, 1, 2, ..., maxPower, and print a table like the following (suppose n is 2 and maxPower is 4)

Task#5 (Nested for loop)

Write a C++ program that generates the output as following:

SSSSS*
SSSS**
SSS***
SS****
S*****
PART#3 (ARRAY-ONE DIMENSIONAL)

Task#6 (OPTIONAL, ‘+5’ Marks)

Write a program that finds the total expenses for a week. There can be multiple items for a single day and
the numbers of items are unknown. The inner loop should iterate for the number of items for each day and
should terminate when there are no more items for that day. The outer loop should execute for the number
of days in a week. Take the item cost as input from the user and calculate the total after each day. Display
this total. Repeat this process for all week days. Calculate the weekly expenditure and display the week
total at the end. Output screen is shown below.

(HINT: Use Do While, for, and nested if-else Statement) to solve.


Task#7 (Array-One Dimensional)

Write a program to enter three integers and output the smallest integer using Array. Output Screen is
shown below.

Task#8
C++ program ask to the user to enter the array size, then ask to enter the element of the
array to store the elements in the array, then finally display the array element on the screen:

Task#9

Read N One Dimensional Array Elements, and calculate the Sum and Product of all
elements and print the sum and product.
Task#10

read 10 elements of integer type using One Dimensional Array and replace all EVEN
elements by 0 and ODD by 1.
Task#11

read Two One Dimensional Array of same data type (integer type) and merge them into
another One Dimensional Array of same type.
Task#12

read two One Dimensional Array and create third One Dimensional Array by adding and
subtracting elements of inputted two One Dimensional Array elements.

Task#13
Write a c++ program to find a number from array elements.
Task#14
Write a C++ program to swap adjacent elements of one dimensional array.
Task#15
Write a c++ program to find occurrence of an element in one dimensional
array.

Task#16
Write a c++ program to delete given element from one dimensional array.
Task#17

Part(4)

TASK#18:

A Fibonacci number is a set in which each number is the sum of the previous two
numbers. The

series begins
0, 1, 1, 2, 3, 5, 8, 13, 21, ---

Write a C++ program that calculates and prints the next three numbers of this
series. Use only three

variables, fib1, fib2, and fib3

TASK#19:

Write a program using Switch that prepares marks sheet of a student. Take the
marks of five subjects as

input and find the total marks, percentage and the grade. Display all the
information. Follow the following

rules for assigning the Grades:

Total Marks Grade

Greater than or equal to 90 but less than 100 A

Less than 90 but greater than or equal to 80 B

Less than 80 but greater than or equal to 70 C

Less than 70 but greater than or equal to 60 D

Less than 60 F

TASK#20:

Make a four function calculator using SWITCH statement. Read in two numbers
and a mathematical

operator. Add the two numbers if the operator ‘+’ is entered. Subtract the two
numbers if operator ‘-‘ is

entered and so on. Display the calculated result. If any character other than + , - ,
*, / is entered, display
proper error

message.

TASK#21:

Write a program to find the grace marks for a student USING SWITCH. The user
should enter the class

obtained by the student and the number of subjects he has failed in.

than 3, then

he does not get any grace. If the number of subjects he failed in is less than or
equal to 3

then the grace is of 5 marks per subject.

nt gets second class and the number of subjects he failed in is


greater than 2,

then he does not get any grace. If the number of subjects he failed in is less than
or equal to

2 then the grace is of 4 marks per subject.

d the number of subjects he failed in is greater


than 1, then

he does not get any grace. If the number of subjects he failed in is equal to 1 then
the grace

is of 5 marks per subject

You might also like