Find elements

Cpp program:find greatest of three numbers using function

Cpp program: find greatest of three numbers using the function

In this tutorial, we will discuss the concept of Cpp program: find the greatest of three numbers using the function

In this post, we will learn how to find the greatest number among three numbers using a user-defined function in C++ programming language

Cpp program: find the greatest

In my previous post, I have explained the various approaches to find the largest number  among  three numbers in C++ language

However, in this program, we embed the logic of the program in the user defined-function.

Here, I have explained how to find the greatest number from the given three numbers and how to embed this logic in the function in C++.

Program 1

find the greatest of three numbers using nested if statements

This program allows the user to enter three numbers and compare to select the largest number using nested if statements

#include <iostream>
#include <conio.h>

using namespace std;
int findBiggest(int,int,int);//function prototype
int main()
{
    double num1, num2,num3; //declare the variables
    cout<<"Enter the first number to compare: ";
    cin>>num1;//get input from user for num1
    cout<<"Enter the second number to compare: ";
    cin>>num2;//get input from user for num2
    cout<<"Enter the third number to compare: ";
    cin>>num3;//get input from user for num3
    int result=findBiggest(num1,num2,num3);//function call
    cout<<"Biggest number is: "<<result;//display the output
   getch();
    return 0;
}
int findBiggest(int num1, int num2, int num3){//function definition
    int biggest;
    if(num1>=num2){
            if(num1>=num3){
                return num1;
            }
            else{
                return num3;
            }
    }
    else{
        if(num2>num3){
            return num2;
        }
            else{
                return num3;
            }
        }
}

When the above code is executed, it produces the following results

Enter the first number to compare: 87
Enter the second number to compare: 23
Enter the third number to compare: 67
Biggest number is: 87

 

 

 

Program 2

find the greatest of three numbers using if-else-if statements

This program allows the user to enter three numbers and compare to select the largest number using if-else-if statements

#include <iostream>
#include <conio.h>

using namespace std;
int findBiggest(int,int,int);
int main()
{
    double num1, num2,num3; //declare the variables
    cout<<"Enter the first number to compare: ";
    cin>>num1;//get input from user for num1
    cout<<"Enter the second number to compare: ";
    cin>>num2;//get input from user for num2
    cout<<"Enter the third number to compare: ";
    cin>>num3;//get input from user for num3
    int result=findBiggest(num1,num2,num3);//function call
    cout<<"Biggest number is: "<<result;//Display the output
   getch();
    return 0;
}
int findBiggest(int num1, int num2, int num3){//function definition
    int biggest;
    if(num1>=num2 && num1>=num3){
         biggest=num1;
    }//num1 compare num 2 and num 3
    else if(num2>=num1 && num2>=num3){
        biggest=num2;
    }//num2 compare num1 and num3
    else{biggest=num3;
    }
    return biggest;

    }

When the above code is executed, it produces the following results

Enter the first number to compare: 35
Enter the second number to compare: 89
Enter the third number to compare: 54
Biggest number is: 89

 

Similar post

Java  program: find the greatest number of three numbers using method

C  program: find the greatest number of three numbers using function

Python  program: find the greatest number of three numbers using function

 

Suggested for you

Function in C language

Operator in C language

Data type in C language

Variable in C language

If statements in C language

 

Java program:find greatest of three numbers using method
C program:find greatest of three numbers using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • Clear and simple:

    cin >> nNumberOne;
    cin >> nNumberTwo;
    cin >> nNumberThree;

    nLargestNumber = nNumberOne;

    if (nLargestNumber < nNumberTwo)
    {
    nLargestNumber = nNumberTwo;
    }

    if (nLargestNumber < nNumberThree)
    {
    nLargestNumber = nNumberThree;
    }

    cout << nLargestNumber << endl;

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago