Practical / Solution
Functions — Exercise 1
Problem
Write a C program using a function with no arguments and no return value
to display a welcome message.
Program
#include <stdio.h>
void welcome()
{
// Display welcome message
printf("Welcome to C Programming!\n");
}
int main()
{
// Call the function
welcome();
return 0;
}
Explanation
The function welcome() does not accept any arguments and
does not return a value. Its purpose is simply to perform an action.
Expected Output
Welcome to C Programming!
Functions — Exercise 2
Problem
Write a C program using a function with arguments and no return value
to display the square of a number.
Program
#include <stdio.h>
void square(int num)
{
// Calculate and display square
printf("Square = %d\n", num * num);
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Pass number to the function
square(num);
return 0;
}
Explanation
The function receives a value through its parameter but does not
return any value. The result is displayed inside the function.
Expected Output
Enter a number: 8
Square = 64
Functions — Exercise 3
Problem
Write a C program using a function with no arguments but with a return
value to calculate and return the square of a number.
Program
#include <stdio.h>
int square()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Return the calculated square
return num * num;
}
int main()
{
int result;
// Receive the returned value
result = square();
printf("Square = %d\n", result);
return 0;
}
Explanation
The function takes no argument, but it returns an integer value to
the calling function.
Expected Output
Enter a number: 7
Square = 49
Functions — Exercise 4
Problem
Write a C program using a function with arguments and a return value
to calculate the sum of two numbers.
Program
#include <stdio.h>
int add(int a, int b)
{
// Return sum of two numbers
return a + b;
}
int main()
{
int a, b, result;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Call function with arguments
result = add(a, b);
printf("Sum = %d\n", result);
return 0;
}
Explanation
The function receives two arguments and returns their sum to the
main() function.
Expected Output
Enter two numbers: 20 15
Sum = 35
Functions — Exercise 5
Problem
Write a C program using a function to find the larger of two numbers.
Program
#include <stdio.h>
int largest(int a, int b)
{
// Compare the two numbers
if (a > b)
return a;
else
return b;
}
int main()
{
int a, b, result;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Call function to find larger value
result = largest(a, b);
printf("Largest = %d\n", result);
return 0;
}
Explanation
The function receives two numbers and returns the greater value.
This demonstrates how a function can perform decision-making.
Expected Output
Enter two numbers: 35 48
Largest = 48
Functions — Exercise 6
Problem
Write a C program using a function to calculate the factorial of a number.
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;
}
int main()
{
int n;
long long result;
printf("Enter a number: ");
scanf("%d", &n);
// Call factorial function
result = factorial(n);
printf("Factorial = %lld\n", result);
return 0;
}
Explanation
The function performs repeated multiplication and returns the
calculated factorial to main().
Expected Output
Enter a number: 5
Factorial = 120
Functions — Exercise 7
Problem
Write a C program using a function to check whether a number is prime.
Program
#include <stdio.h>
int isPrime(int n)
{
int i;
// Numbers less than 2 are not prime
if (n < 2)
return 0;
// Check possible divisors
for (i = 2; i * i <= n; i++)
{
if (n % i == 0)
return 0;
}
return 1;
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Check result returned by the function
if (isPrime(num))
printf("The number is prime.\n");
else
printf("The number is not prime.\n");
return 0;
}
Explanation
The function checks divisibility and returns 1 for a
prime number and 0 otherwise.
Expected Output
Enter a number: 29
The number is prime.
Functions — Exercise 8
Problem
Write a C program using separate functions to perform addition,
subtraction, multiplication and division of two numbers.
Program
#include <stdio.h>
float add(float a, float b)
{
return a + b;
}
float subtract(float a, float b)
{
return a - b;
}
float multiply(float a, float b)
{
return a * b;
}
float divide(float a, float b)
{
return a / b;
}
int main()
{
float a, b;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
printf("Addition = %.2f\n", add(a, b));
printf("Subtraction = %.2f\n", subtract(a, b));
printf("Multiplication = %.2f\n", multiply(a, b));
// Check division by zero
if (b != 0)
printf("Division = %.2f\n", divide(a, b));
else
printf("Division by zero is not allowed.\n");
return 0;
}
Explanation
Separate functions are created for each arithmetic operation.
This makes the program modular and easier to maintain.
Expected Output
Enter two numbers: 20 5
Addition = 25.00
Subtraction = 15.00
Multiplication = 100.00
Division = 4.00
Functions — Exercise 9
Problem
Write a C program using a function to find the largest among three numbers.
Program
#include <stdio.h>
int largestOfThree(int a, int b, int c)
{
int largest;
// Find larger value among the first two numbers
if (a > b)
largest = a;
else
largest = b;
// Compare with the third number
if (c > largest)
largest = c;
return largest;
}
int main()
{
int a, b, c, result;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
// Call the function
result = largestOfThree(a, b, c);
printf("Largest = %d\n", result);
return 0;
}
Explanation
The comparison logic is separated into a function. The function
receives three arguments and returns the largest value.
Expected Output
Enter three numbers: 25 72 48
Largest = 72
Functions — Exercise 10
Problem
Write a menu-driven C program using separate functions for addition,
subtraction, multiplication and division.
Program
#include <stdio.h>
float add(float a, float b)
{
return a + b;
}
float subtract(float a, float b)
{
return a - b;
}
float multiply(float a, float b)
{
return a * b;
}
float divide(float a, float b)
{
return a / b;
}
int main()
{
int choice;
float a, b;
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter choice: ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
// Select the required function
switch (choice)
{
case 1:
printf("Result = %.2f\n", add(a, b));
break;
case 2:
printf("Result = %.2f\n", subtract(a, b));
break;
case 3:
printf("Result = %.2f\n", multiply(a, b));
break;
case 4:
if (b != 0)
printf("Result = %.2f\n", divide(a, b));
else
printf("Division by zero is not allowed.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Explanation
This program combines functions with switch-case. Each operation
has its own function, while the switch statement selects which
function should be called.
Expected Output
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter choice: 1
Enter two numbers: 25 15
Result = 40.00