C Programming • Arrays & Functions
C Programming / Nested Functions — Exercises

Nested Functions — Exercises

Practical 3 Arrays & Functions

Write a C program in which one function calls another function to calculate the square of a number.

Practical / Solution

Nested Functions — Exercise 1

Problem

Write a C program in which one function calls another function to calculate the square of a number.

Program

#include <stdio.h> int square(int n) { // Return square of the number return n * n; } void displaySquare(int n) { // Call square() from another function int result = square(n); printf("Square = %d\n", result); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); // Call the function that internally calls square() displaySquare(num); return 0; }

Explanation

The displaySquare() function calls square(). This demonstrates function-to-function calling in C.

Expected Output

Enter a number: 8

Square = 64

Nested Functions — Exercise 2

Problem

Write a C program where one function calls another function to calculate the cube of a number.

Program

#include <stdio.h> int cube(int n) { // Calculate cube return n * n * n; } void displayCube(int n) { // Call cube() from this function printf("Cube = %d\n", cube(n)); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); displayCube(num); return 0; }

Explanation

The displayCube() function does not calculate the cube itself. Instead, it calls the cube() function and uses its returned value.

Expected Output

Enter a number: 4

Cube = 64

Nested Functions — Exercise 3

Problem

Write a C program where a function calls another function to determine whether a number is even or odd.

Program

#include <stdio.h> int isEven(int n) { // Return 1 when the number is even return n % 2 == 0; } void checkNumber(int n) { // Call isEven() from another function if (isEven(n)) printf("The number is even.\n"); else printf("The number is odd.\n"); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); checkNumber(num); return 0; }

Explanation

checkNumber() calls isEven() to obtain the result and then displays the appropriate message.

Expected Output

Enter a number: 15

The number is odd.

Nested Functions — Exercise 4

Problem

Write a C program where one function calls two other functions to calculate the square and cube of a number.

Program

#include <stdio.h> int square(int n) { return n * n; } int cube(int n) { return n * n * n; } void displayResults(int n) { // Call both helper functions printf("Square = %d\n", square(n)); printf("Cube = %d\n", cube(n)); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); displayResults(num); return 0; }

Explanation

A single function can call multiple other functions. Here, displayResults() calls both square() and cube().

Expected Output

Enter a number: 5

Square = 25

Cube = 125

Nested Functions — Exercise 5

Problem

Write a C program where one function calculates the largest of two numbers by calling another comparison function.

Program

#include <stdio.h> int larger(int a, int b) { // Return the larger number if (a > b) return a; else return b; } void displayLargest(int a, int b) { // Call larger() from this function int result = larger(a, b); printf("Largest = %d\n", result); } int main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); displayLargest(x, y); return 0; }

Explanation

displayLargest() delegates the comparison to larger() and displays the returned value.

Expected Output

Enter two numbers: 35 72

Largest = 72

Nested Functions — Exercise 6

Problem

Write a C program where one function calculates the area of a rectangle and another function calls it to display the result.

Program

#include <stdio.h> float rectangleArea(float length, float width) { // Calculate rectangle area return length * width; } void displayArea(float length, float width) { // Call rectangleArea() from this function printf("Area = %.2f\n", rectangleArea(length, width)); } int main() { float l, w; printf("Enter length and width: "); scanf("%f %f", &l, &w); displayArea(l, w); return 0; }

Explanation

The function displayArea() calls rectangleArea() and displays the returned area.

Expected Output

Enter length and width: 10 5

Area = 50.00

Nested Functions — Exercise 7

Problem

Write a C program where one function calls another function to calculate the factorial of a number and then determines whether the factorial is even or odd.

Program

#include <stdio.h> long long factorial(int n) { long long fact = 1; int i; // Calculate factorial for (i = 1; i <= n; i++) { fact = fact * i; } return fact; } void checkFactorial(int n) { long long result; // Call factorial() from this function result = factorial(n); printf("Factorial = %lld\n", result); // Check the returned result if (result % 2 == 0) printf("Factorial is even.\n"); else printf("Factorial is odd.\n"); } int main() { int n; printf("Enter a number: "); scanf("%d", &n); checkFactorial(n); return 0; }

Explanation

checkFactorial() first calls factorial() and then performs another operation on the returned value.

Expected Output

Enter a number: 5

Factorial = 120

Factorial is even.

Nested Functions — Exercise 8

Problem

Write a C program where a function calls separate functions to calculate total and average marks of three subjects.

Program

#include <stdio.h> int totalMarks(int a, int b, int c) { // Return total marks return a + b + c; } float averageMarks(int a, int b, int c) { // Calculate average return totalMarks(a, b, c) / 3.0; } void displayResult(int a, int b, int c) { // Call both functions printf("Total = %d\n", totalMarks(a, b, c)); printf("Average = %.2f\n", averageMarks(a, b, c)); } int main() { int m1, m2, m3; printf("Enter marks of three subjects: "); scanf("%d %d %d", &m1, &m2, &m3); displayResult(m1, m2, m3); return 0; }

Explanation

This example demonstrates a chain of function calls: main() calls displayResult(), which calls other functions. averageMarks() also calls totalMarks().

Expected Output

Enter marks of three subjects: 70 80 90

Total = 240

Average = 80.00

Nested Functions — Exercise 9

Problem

Write a C program where one function calls separate functions to calculate the area and perimeter of a circle.

Program

#include <stdio.h> float circleArea(float radius) { return 3.14159 * radius * radius; } float circlePerimeter(float radius) { return 2 * 3.14159 * radius; } void displayCircleResult(float radius) { // Call both calculation functions printf("Area = %.2f\n", circleArea(radius)); printf("Circumference = %.2f\n", circlePerimeter(radius)); } int main() { float radius; printf("Enter radius: "); scanf("%f", &radius); displayCircleResult(radius); return 0; }

Explanation

The display function calls two separate calculation functions. This makes each function responsible for one specific task.

Expected Output

Enter radius: 5

Area = 78.54

Circumference = 31.42

Nested Functions — Exercise 10

Problem

Write a C program using a chain of functions to find the largest of three numbers. One function should compare two numbers, while another function should use it to determine the largest value.

Program

#include <stdio.h> int larger(int a, int b) { // Return the larger of two values if (a > b) return a; else return b; } int largestOfThree(int a, int b, int c) { int firstLargest; // Call larger() to compare first two values firstLargest = larger(a, b); // Call larger() again with the third value return larger(firstLargest, c); } void displayLargest(int a, int b, int c) { // Call largestOfThree() int result = largestOfThree(a, b, c); printf("Largest = %d\n", result); } int main() { int x, y, z; printf("Enter three numbers: "); scanf("%d %d %d", &x, &y, &z); displayLargest(x, y, z); return 0; }

Explanation

This example demonstrates a chain of function calls: main() calls displayLargest(), displayLargest() calls largestOfThree(), and largestOfThree() calls larger(). This is a useful way to demonstrate nested function calls in standard C.

Expected Output

Enter three numbers: 25 68 42

Largest = 68