Data Structure and Algorithms: Assignment # 1
Data Structure and Algorithms: Assignment # 1
Assignment # 1
It should be clear that your assignment will not get any credit if the assignment is copied (partial or full)
from any source (websites, forums, students etc.).
Assignment Task
You are required to write the programs of following problems in handwritten form. You are also
required to attach the screenshot of the output of each program that you practiced on any compiler.
You need to submit the assignment before deadline Monday, 13 Dec at 08:00PM.
PROBLEM 1
Write a program in C++ to display N terms of natural number and their sum. Write a function to
perform this task. A function should accept N terms as parameter and then perform the further tasks
inside function.
Sample Output:
Input a number of terms: 7
The natural numbers upto 7th terms are:
1234567
The sum of the natural numbers is: 28
PROBLEM 2
Write a program in C++ to check whether a number is prime or not. Write a function to perform this
task. A function should accept a number as parameter and then display a proper message whether this
number is prime or not.
Sample Output:
Input a number to check prime or not: 13
The entered number is a prime number.
PROBLEM 3
Write a program in C++ to find prime number within a range. Write a function to perform this task. The
function should accept the range values (initial and final value) in parameter and then perform further
task.
Sample Output:
Input number for starting range: 1
Input number for ending range: 100
The prime numbers between 1 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
The total number of prime numbers between 1 to 100 is: 25
PROBLEM 4
Write a program in C++ to find the factorial of a number. Write a function to perform this task. The
function should accept a number in parameter and return the factorial of given number. Display the
returned value in main function.
Sample output:
Input a number to find the factorial: 5
The factorial of the given number is: 120
PROBLEM 5
Write a program in C++ to find the sum of digits of a given number. Write a function to perform this
task. A function should accept a number in parameter and then display the sum of its digits.
Sample Output:
Input a number: 1234
The sum of digits of 1234 is: 10
PROBLEM 6
Write a program in C++ to display the first n terms of Fibonacci series. Write a function to perform this
task. The function should accept N in parameter and then display Fibonacci series upto N.
Sample Output:
Input number of terms to display: 10
Here is the Fibonacci series upto to 10 terms:
0 1 1 2 3 5 8 13 21 34
PROBLEM 7
Write a program that takes a number from user and pass it to a function. That function should return
the cube of the number.
PROBLEM 8
Write a program that inputs two number from user and pass it to a function. That function should
return the power of number as first number raised to power second number.
PROBLEM 9
Write a program that performs the addition of minimum 2 numbers and maximum 10 numbers. The
program should have a user-defined function for this purpose. That function will accept max 10
numbers and min 2 numbers in parameter and then return their sum. For example if I pass 2
parameters to this function, it should return sum of two digits. If I pass 7 digits, it should return sum of
7 digits and vice versa.
Hint: Use Default Parameters
PROBLEM 10
Write a program that takes two numbers from user and pass them to a function. That function should
swap their values. If I print those numbers in main function after calling the SWAP function, their
swapped values should be displayed.
PROBLEM 11
Write a program that inputs 10 elements in a 1D array. The program should then print the sum, max
and min value in the array.
PROBLEM 12
Write a program that takes 10 elements in a 1D array and pass it to a function that should return max
value in that array. Then pass this returned value to another function that should return true if that
value is a prime number and false if it is composite.
PROBLEM 13
Write a program that inputs 10 elements in an array. The program should print the number of prime
numbers in the array.
PROBLEM 14
Write a program that inputs 10 elements in an array. The program should print factorial of each
number stored in the array.
PROBLEM 15
Write a program that inputs 10 elements in an array. You are required to reverse the elements of array
and then print them. (Element at 0 index should be exchanged with the element at 9 index and vice
versa)
PROBLEM 16
Write a class “Circle” with one data member radius. Write following member functions:
A constructor that assigns the data members with any initial value. (mostly zero)
set_radius() that accepts a value in parameter and assigns to radius
get_area() that calculate and return area
get_circum() that calculate and return circumference
The program should create two objects of class and input radius for these objects. The program should
display area for first object and circumference for second object.
PROBLEM 17
Write a class “Book” with three data members BookID, Pages and Price. It also contains following
member functions:
A constructor that assigns the data members with any initial value. (mostly zero)
The get() is used to input values
The show() is used to display values
The set() is used to set values of data members using parameters
The getPrice() is used to return value of price
The program should create two objects of class and input values for these objects. The program should
display the details of most costly book.
PROBLEM 18
Write a class “Array” that contains an array of integers to store five values. It also contains following
member functions.
A constructor that assigns all the elements of array with zero value.
The Fill() function is used to fill the array with values from user
The Display() function is used to display the values of array
The Max() function shows the maximum value in the array
The Min() function shows the minimum value in the array
All member functions should be defined outside the class. Create an object in main function and make
use of all member functions.