Skip to content

← Back to Overview

⚙️ Functions & Methods

Functions (or methods) are the building blocks of reusable code. These problems focus on defining logic once and calling it multiple times with different parameters.


1. Simple Greeting Function

Rating: Beginner | Difficulty: Easy Write a function that takes a name as a string and prints "Hello, [Name]!".

💡 Hint Define a function with one parameter and no return value (void in C/Java).


2. Max of Two (Function)

Rating: Beginner | Difficulty: Easy Create a function findMax(a, b) that returns the larger of two numbers.

💡 Hint The function should have an integer return type.


3. Factorial Function

Rating: Beginner | Difficulty: Easy Write a function factorial(n) that returns the factorial of a given number.

💡 Hint Reuse the loop logic from the Loops section, but wrap it in a function.


4. Prime Checker Function

Rating: Intermediate | Difficulty: Medium Create a boolean function isPrime(n) that returns true if a number is prime and false otherwise.

💡 Hint Useful for many advanced problems! Return 1/0 in C or true/false in Java/C++.


5. Circle Properties

Rating: Beginner | Difficulty: Easy Write functions to calculate the area and circumference of a circle given its radius.

💡 Hint

Area = 3.14 * r * r, Circumference = 2 * 3.14 * r.


6. Power Function

Rating: Intermediate | Difficulty: Medium Write a function power(base, exponent) that calculates the power of a number without using math libraries.

💡 Hint Use a loop to multiply the base exponent times.


7. Array Sum Function

Rating: Intermediate | Difficulty: Medium Write a function that takes an array and its size as parameters and returns the sum of all elements.

💡 Hint In C, you must pass the size separately. In Java, you can use `arr.length`.

8. Even/Odd Logic

Rating: Beginner | Difficulty: Easy Write a function isEven(n) that returns true if n is even. Use this function in your main program to print all even numbers between 1 and 50.

💡 Hint Call the function inside a loop in main.


9. Fibonacci Term Finder

Rating: Intermediate | Difficulty: Medium Write a function getFibonacci(n) that returns the Nth term of the Fibonacci sequence.

💡 Hint

If n=5, return 3 (Sequence: 0, 1, 1, 2, 3).


10. Swap by Reference

Rating: Advanced | Difficulty: Medium Write a function to swap two numbers. Challenge: Ensure the values stay swapped in the calling function (main).

💡 Hint Use pointers in C or pass-by-reference in C++. In Java, you'll need to use an array or an object since primitives are pass-by-value.


11. String Length Function

Rating: Intermediate | Difficulty: Medium Re-implement the strlen or .length() logic inside a custom function.

💡 Hint Pass the string to the function and loop until the null terminator.


12. GCD and LCM

Rating: Advanced | Difficulty: Medium Write two functions: findGCD(a, b) and findLCM(a, b). Use the formula LCM = (a*b)/GCD.

💡 Hint The Euclidean algorithm is the most efficient way to find GCD.

13. Unit Converter Utility

Rating: Intermediate | Difficulty: Medium Create a menu-driven program using functions to convert: 1. Km to Miles 2. Inches to Feet 3. Celsius to Fahrenheit

💡 Hint Use a `switch` case in `main` to call different functions.

14. Array Average and Grade

Rating: Intermediate | Difficulty: Medium Write one function to find the average of an array of marks, and another to return the grade based on that average.

💡 Hint Chain the functions: getGrade(getAverage(marks)).


15. Perfect Number Finder

Rating: Pro | Difficulty: Hard Write a function isPerfect(n). Use this function to find and print all perfect numbers between 1 and 10,000.

💡 Hint Perfect numbers are rare (6, 28, 496...). Your loop in main will call the function 10,000 times.


← Previous: Strings | Next Topic: Recursion →