LearnFrenzy
Sign in Take a free test

Functions · Exercise 4 of 4

Functions Function Programs

  • 8 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Write each function yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output. Tick "Mark as solved" to track your progress.

Question 1 Easy

Write gcd() (Euclid's algorithm) and lcm() that cannot overflow for large inputs.

Question page & discussion
Question 2 Easy

Write is_prime() that tests divisors only up to √n, and print all primes up to 50.

Question page & discussion
Question 3 Medium

Find the largest element of an array with a recursive function.

Question page & discussion
Question 4 Easy

Print the first 10 Fibonacci numbers with an iterative function – no exponential recursion.

Question page & discussion
Question 5 Medium

Print the binary form of a number with a recursive function.

Question page & discussion
Question 6 Easy

Write is_palindrome() for numbers by reversing the digits.

Question page & discussion
Question 7 Medium

Print every Armstrong number from 1 to 1000 (a k-digit number equal to the sum of its digits each raised to the power k), using small helper functions.

Question page & discussion
Question 8 Hard

Write a reusable count_if() that counts the elements for which a given test function returns true.

Question page & discussion
All exercises doneBack to FunctionsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Popular