Program to find greatest between three numbers using function

1 year ago Admin 0

Find greatest between three numbers using function

This article illustrates the concept how to find greatest between three numbers using function in c/c++.

Greatest between three numbers

Here is the source code and output of both the programs.

C Programming language

Program


#include<stdio.h>
#include<conio.h>
int larger(int,int,int);
void main()
{
 int first,second,third,larg;
 clrscr();
 printf("Enter first number:- ");
 scanf("%d",&first);
 printf("Enter second number:- ");
 scanf("%d",&second);
 printf("Enter third number:- ");
 scanf("%d",&third);
 larg=larger(first,second,third);
 printf("Largest of three numbers is:- %d",larg);
 getch();
}
int larger(int x,int y,int z)
{
 if(x>y)
  {
   if(x>z)
    {
     return x;
    }else{
     return z;
    }
  }else{
   if(y>z)
    {
     return y;
    }else{
     return z;
    }
  }
}

After successfully compiling and executing the above C program,output is:-

find greatest between three numbers using function


C++ Programming language

Program


#include<iostream.h>
#include<conio.h>
int larger(int,int,int);
void main()
{
 clrscr();
 int first,second,third,larg;
 cout<<"Enter first number:- ";
 cin>>first;
 cout<<"Enter second number:- ";
 cin>>second;
 cout<<"Enter third number:- ";
 cin>>third;
 larg=larger(first,second,third);
 cout<<"Largest of three numbers is:- "<<larg;
 getch();
}
int larger(int x,int y,int z)
{
 if(x>y)
  {
   if(x>z)
    {
     return x;
    }else{
     return z;
    }
  }else{
   if(y>z)
    {
     return y;
    }else{
     return z;
    }
  }
}

After successfully compiling and executing the above C++ program,output is:-

find greatest between three numbers using function


Comment below if you are facing any problem or you find any error in the above program and also subscribe to our newsletter for more updates.


Read more c/c++ Programs published by snapcout:-

Program to add two numbers using function
Program to swap two numbers using function
Program to find largest element in array using function
Program to search an element in array using function
Program to insert an element in array using function


About article

Written By:- Admin

Published on:- 03/25/2018

Published By:- Snapcout


Don't forget to share the article if you like it:-