You are on page 1of 4

Exercise 18 (Functions)

1.Write a C program with a user-defined function to find the area of a rectangle. Take
input from the user.
Program:
#include<stdio.h>
int areaRectangle(int,int);
int main()
{
    int l,b,area;
    printf("Enter the length : ");
    scanf("%d", &l);
    printf("Enter the width : ");
    scanf("%d", &b);
    area = areaRectangle(l,b);
    printf("The area of the rectangle : %d",area);
    return 0;
}
int areaRectangle(int length, int breadth)
{
    return length*breadth;
}
Output:
Enter the length : 5
Enter the width : 8
The area of the rectangle : 40
=============================================================
2.Write a program to find the cube of a given number by defining a user-defined
function. Take input from the user.
Program:
#include<stdio.h>
double cube_of_num(double n);
void main()
{
    int n;
    double cube;
    printf("Enter the number: ");
    scanf("%d",&n);
    cube=cube_of_num(n);
    printf("Cube of %d is %.2f",n,cube);
}
double cube_of_num(double n)
{
    return (n*n*n);
}
Output:
Enter the number: 25
Cube of 25 is 15625.00
=============================================================
3.In the C program write a user-defined function to find minimum and maximum in
given three numbers. Take three numbers from the user.
Program:
#include<stdio.h>
float minimum(float a, float b, float c);
float maximum(float a, float b, float c);
int main()
{
  float n1, n2, n3;
  printf("Enter three numbers: \n");
  scanf("%f %f %f", &n1, &n2,  &n3);
  printf("Minimum number = %.2f\n",minimum(n1,n2,n3));
  printf("Maximum number = %.2f\n",maximum(n1,n2,n3));
  return 0;
}
float minimum(float a, float b, float c)
{
  if(a<=b && a<=c) return a;
  else if(b<=a && b<=c) return b;
  else return c;
}
float maximum(float a, float b, float c)
{
  if(a>=b && a>=c) return a;
  else if(b>=a && b>=c) return b;
  else return c;
}
Output:
Enter three numbers:
64
68
69
Minimum number = 64.00
Maximum number = 69.00
=============================================================
4.Write a C program to find the square of a given number using user-defined
functions. Define three functions, take input from the user using one function,
calculate a square in another function, and display the result in another function.
Program:
#include<stdio.h>
int f1();
int f2();
void f3();
int main() {
    int input = f1();
    int calculation = f2(input);
    f3(calculation);
    return 0;
}
int f1(){
    int base;
    printf("Enter the number : ");
    scanf("%d", &base);
    return base;
}
int f2(int power){
    return power*power;
}
void f3(int square){
    printf("square of number is %d ",square);
}
Output:
Enter the number : 5
square of number is 25
=============================================================
5.Write a program that extracts and adds the two least significant digits of any
number. Define three functions addTwoDigits(), lastDigit() and secondLastDigit().
The main function should call addTwoDigits(). The lastDigit() and secondLastDigit()
functions should be called from function addTwoDigits().
Program:
#include<stdio.h>
int add_Two_Digits(int n);
int last_Digit(int n);
int second_Last_Digit(int n);
int main()
{
   int number;
   printf("Enter an integer number: ");
   scanf("%d",&number);
   int sum = add_Two_Digits(number);
   printf("Sum of last two digits is = %d",sum);
   return 0;
}
int add_Two_Digits(int n)
{
   int result = last_Digit(n) + second_Last_Digit(n);
   return result;
}
int last_Digit(int n)
{
   return (n%10);
}
int second_Last_Digit(int n)
{
   return ((n/10)%10);
}
Output:
Enter an integer number: 123456
Sum of last two digits is = 11
======================= END ==============================

You might also like