0% found this document useful (0 votes)
7 views2 pages

Functions and Recursion in Programming

The document outlines a series of programming tasks focused on functions and recursion, including checking for prime numbers, calculating factorials, printing factors, and performing binary search. It also covers recursive functions for various calculations such as GCD, sum of digits, and printing sequences. Additionally, it includes data organization tasks for managing country GDPs and student/customer information.

Uploaded by

vamsidhar9191
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
0% found this document useful (0 votes)
7 views2 pages

Functions and Recursion in Programming

The document outlines a series of programming tasks focused on functions and recursion, including checking for prime numbers, calculating factorials, printing factors, and performing binary search. It also covers recursive functions for various calculations such as GCD, sum of digits, and printing sequences. Additionally, it includes data organization tasks for managing country GDPs and student/customer information.

Uploaded by

vamsidhar9191
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

Functions and Recursion

1. Write a programthat contains a function to check whether the argument is a prime number or
not. Function accepts an integer and returns 0 if the integer is a prime number. Otherwise it
returns 1.
2. Write a programthat contains a function which will print the factors of the given number.
Number to be given as argument/input to the function.
3. Write a programthat contains a function to calculate the factorial of a given number. The
function takes number as argument and returns its factorial.
4. Write a programthat contains a function which will print the number given as argument in
words – 123 to be printed as one two three
5. Write a programwith a function that accepts an array and the number of elements in the array
as inputs and returns the sum of the elements in the array.
6. Write a programthat contains a function that accepts an array and the number of elements in the
array as inputs and returns the difference between largest and smallest elements of the
array.
7. Write a program that contains a recursive function to calculate the sum of first N natural
numbers.
8. Write a program that contains a recursive function to calculate the factorial of an integer.
9. Write a program that contains a recursive function to calculate the sum of elements of
an array.
10. Write a program that contains a recursive function to find the sum of digits of a number.
(Hint: If number is 0, return zero. Otherwise return (number%10 + sum(number/10)))
11. Write a program that contains a recursive function to calculate GCD of two numbers
(Hint: Assume that numbers are A and B. If B=0, return A. Otherwise return GCD(B,
A%B))
12. Write a program that contains a recursive function to M raised to N (MN).
(Hint: If N=0, return 1. Otherwise return M*Pow(M,N-1))
13. Write a recursive function to find the largest (or smallest) number among a collection of
numbers.
14. WRITE A PROGRAM that contains a recursive function to perform Binary Search
15. (Hint: The function will need the sorted array, element to be searched, lower index and upper
index as arguments. Initially lower index will be 0 and upper index will be N-1, where N is the
number of elements in the array. If lower index > upper index, return -1, indicating that element
is not present in the list. Otherwise, do the following:-
16. Mid index is calculated as average of lower index and upper index.
17. If Array’s mid index element equals item to be searched, return mid.
18. If item < Array’s mid index element, call the function with upper limit changed to (mid index-
1) and return the result.
19. If item > Array’s mid index element, call the function with lower limit changed to (mid index+1)
and return the result.
20. Write recursive functions to print the following sequences: -
a. *
* *
***
****
b. 1
12
123
1234
c. 1
22
333
4444
21. Write a recursive function to print the binary equivalent of a given number.
22. Write a recursive function to reverse a number.
23. Store the details of N countries and their GDP for three years using appropriate data
organization. Using functions, do the following:-
a. Country with Maximum GDP in Year1
b. Country with Minimum GDP in Year2
c. Country with best average GDP across three years.
24. Use appropriate data organization to store the roll no., name, age (between 11 to 14) and address
of students (more than 10). Store the information of the students.
a. Write a function to print the names of all the students having age 14.
b. Write another function to print the names of all the students having even roll no.
c. Write another function to display the details of the student whose roll no is given (i.e.
roll no. entered by the user).
25. Use appropriate data organization to store the name, account number and balance of customers
(more than 10) and store their information.
a. Write a function to print the names of all the customers having balance less than $200
b. Write a function to add $100 in the balance of all the customers having more than $1000
in their balance and then print the incremented value of their balance.
26. Write a program to open a file, read its contents and do the following:
a. Total number of Alphabets
b. Number of consonants
c. Number of vowels.
d. Number of words (Assuming that words are separated by a single space or full stop.)
e. Number of sentences (Sentences are separated by full stops)
27. Write a program to copy one text file to the other in such a way that
a. Everything is copied.
b. Only alternate alphabets are written into the copy.
c. Only alternate words are copied
d. Only consonants are copied.

You might also like