Functions: Practical No.68
Functions: Practical No.68
Practical No.68
Task: Write a program that accept a number from the user as an input and calculates the
square of a given number using function.
Practical No. 69
Task: Write a program that by defining a function “even_odd”, to test whether a given
integer is even or odd. Pass an integer value as an argument to a function.
Practical No. 70
Task: Write a program that accepts three numbers from the user as an input and display
minimum of three numbers using function.
Practical No. 71
Task: Write a program that calculates the area of the Ring using a single function.
Practical No. 72
Task: Write a function integerPower ( base, exponent ) that returns the value of
base exponent
Practical No. 73
Task: Write an integer number is said to be a perfect number if the sum of its factors,
including 1 (but not the number itself), is equal to the number.
Practical No. 74
Task: A positive integer is entered through the keyboard. Write a function to obtain the
prime factors of this number.
For example: Prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.
Practical No. 75
Task: Write a function that takes an integer value and returns the number with its digits
reversed.
For example, given the number 7631, the function should return 1367.
Practical No. 76
Task: The greatest common divisor (GCD) of two integers is the largest integer that evenly
divides each of the numbers. Write a function gcd that returns the greatest common divisor
of two integers.
Practical No. 77
Practical No. 78
Task: Write a program using function to calculate the factorial value of any integer entered
by the user as an input.
Practical No. 79
Task: A 5-digit positive integer is entered through the keyboard, write a function to
calculate sum of digits of the 5-digit number:
Practical No. 80
Task: Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a
Fibonacci sequence the sum of two successive terms gives the third term. Following are the
first few terms of the Fibonacci sequence:
Practical No. 81
Task: Write a C++ program that uses an inline function circleArea to prompt the user for
the radius of a circle and to calculate and print the area of that circle.
Practical No. 82
Task: Write a C++ program that uses an inline function pound_kg to prompt the user for
the weight in pound and to calculate and print the equivalent in kilogram.
Practical No. 83
Task: Write a program by defining a function swap to exchange the values by passing
argument by reference to the function.
Practical No. 84
Task: Computers are playing an increasing role in education. Write a program that will help
an elementary school student learn multiplication. Use rand to produce two positive one-
digit integers. It should then type a question such as:
The student then types the answer. Your program checks the student's answer. If it is
correct, print "Very good!", and then ask another multiplication question. If the answer is
wrong, print "No. Please try again." and then let the student try the same question again
repeatedly until the student finally gets it right.
Practical No. 85
Task: Write a program that plays the game of “guess the number” as follows: Your program
chooses the number to be guessed by selecting an integer at random in the range 1 to 1000.
The program then types:
The player then types a first guess. The program responds with one of the following:
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 to help
the player “zero in” on the correct answer.
Practical No. 86
Task: Write a complete C++ program with the two alternate functions specified below, of
which each simply triples the variable count defined in main. Then compare and contrast
the two approaches. These two functions are
a) Function tripleCallByValue that passes a copy of count call-by-value, triples the copy
and returns the new value.
Practical No. 87
Task: Write a C++ program that demonstrates the concept of function overloading by
calculating the cube of any number. The function cube calculate cube of different data type
having same or different parameter.
Practical No. 88
Task: Write a C++ program that demonstrates the concept of default argument in
calculating the volume of the box. This function calculate volume of the box 4 time having
default values at first and changing them until having no default vales in last call.
Arrays
Array: An Array is a set of contigious memory locations i.e. all the elements of an array are stored
contigiously / continuously in memory. All the elements of array have same name as that of array
but have different indexes or subscripts. So every element of array is referenced by
1. Array Name
2. Corresponding index or Subscript
That’s why array is also known as "subscripted variable". Moreover the length of array is always
fixed (defined at the time of declaration) and all the elements of an array have same data type e.g.
int, float, long etc...
Note that the index of an array starts from 0 (instead of 1) and ends at N-1 where N is the size of
array. i.e. an array of 5 variables will have its indexes from 0 to 4 as a[0],a[1],a[2],a[3] and a[4].
Practical No.88
Task: Write a program that uses an array of five elements. It just accepts the elements of
array from the user and displays them in the same order using for loop.
Practical No.89
Task: Write a program that uses an array of five elements. It just accepts the elements of
array from the user and displays them in the reverse order using for loop.
Practical No.90
Task: Write a program that uses an array of 8 elements entered by user and then find out
total number of odd and even values entered by the user in a one dimensional array.
Practical No.91
Task: Write a program to enter the data in two linear arrays, add the two arrays and store
the sum in third array.
Practical No.92
Task: Write a program that calculates the sum of square of numbers stored in an array of
10 elements.
Practical No. 93
Task: Write a program that accepts five elements from a user in an array and find their
maximum and minimum.
Practical No. 94
Task: Write a program that should accepts five elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array its
index and contents (value) is displayed, else (not found) a massage “Element not found in
an array” is displayed. (Using Linear /Sequential Search Algorithm)
Practical No. 95
Task: Write a program that should accepts five elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array its
all (apparent) and total number of occurrences are displayed, else (not found) a massage
“Element not found in an array” is displayed.
Practical No. 96
Task: Write a program that should accepts 10 elements from the user in an array and search
a particular element (entered by the user) in it. If the element is found in an array its index
is displayed, else (not found) a massage “Element not found in an array” is displayed.
(Using Binary Search Algorithm)
Practical No. 97
Task: Write a program that accepts ten elements from a user in an array and sorts them in
ascending order using:
1. Bubble sort
2. Selection sort
Practical No. 98
Task: Write a program that accepts ten elements from a user in an array and sorts them in
Descending order.
Practical No. 99
Task: Write a program that accepts ten elements from a user in an array and sorts first five
elements in ascending order and rest of the five elements in descending order using bubble
sort.
Practical No. 100
Task: Write a program that accepts two arrays (A and B) of ten elements each from the user
and merges them in a single array (c) of twenty elements.
Task: Write a program that takes values from user to fill a two-dimensional array (Matrix)
having two rows and three columns and display the values in row column format.
Task: Write a program that takes values from user to fill a two-dimensional array (Matrix)
having three rows and three columns and display it’s contents and also display the flipping
the same matrix (i.e. reversing the row order) using functions.
Task: Write a program that should accepts five elements from the user in a 2D-array and
search a particular element (entered by the user) in it. If the element is found in an array its
indexes are displayed, else (not found) a massage “Element not found in an array” is
displayed (Using Linear /Sequential Search Algorithm).
Task: Write a program that takes values from user to fill a 2 two-dimensional array (Matrix)
having three rows and three columns and then add these two matrixes and store the result
in another matrix and display all matrixes values in row column format (Addition of two
matrixes).
Task: Write a program that takes values from user to fill a 2 two-dimensional array (Matrix)
having any order and then multiply these two matrixes if possible (i.e. Checking multiply
rule first) and store the result in another matrix and display all matrixes values in row
column format (Multiplication of two matrixes).
Task: Write a program that takes values from user to fill a two-dimensional array (Square
Matrix) then calculate the transpose of that matrix and display its contents in matrix form.
Task: Write a program use a single-subscripted array to solve the following problem. Read
in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print
it only if it is not a duplicate of a number already read. Provide for the “worst case” in which
all 20 numbers are different. Use the smallest possible array to solve this problem.