Find elements

C program:find greatest of three numbers using function

C program: find greatest of three numbers using the function

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

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

C program: find the greatest

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

However, in this program, we embed the logic of the “find the greatest of three number” program in the 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.

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 <stdio.h>
#include <stdlib.h>
biggestNumber(int,int,int);//function prototype
int main()
{

    int a,b,c;
    printf("Enter the three numbers\n");
    scanf("%d%d%d",&a,&b,&c);
//read the numbers from user
    int result=biggestNumber(a,b,c);//function call
    printf("Biggest number is: %d\n",result);
//display the output on the screen
    getch();
    return 0;
}
int biggestNumber(int a,int b,int c){//function definition with parameter
if(a>b)
{
    if(a>c)
    return a;
    else
        return c;
}
else
{
    if(b>c)
        return b;
    else
        return c;
}
}

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

Enter the three numbers
45
89
67
Biggest number is: 89

 

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

Program 2

#include <stdio.h>
#include <stdlib.h>
biggestNumber(int,int,int);//function prototype

int biggestNumber(int a,int b,int c){//function definition with parameter
    int biggest;
if(a>b && a>c)
    biggest=a;
else if(b>a && b>c)
    biggest=b;
else
    biggest=c;
return biggest;
}
int main()
{
    int a,b,c;
    printf("Enter the three numbers\n");
//reads the number for find largest
    scanf("%d%d%d",&a,&b,&c);
    int result=biggestNumber(a,b,c);//call the function
    printf("Biggest number is: %d\n",result);
//display output on the screen
    getch();
    return 0;
}

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

Enter the three numbers
34
56
43
Biggest number is: 56

 

Smiler post

Java code to find the largest of three numbers using method

C++ code to find the largest of three numbers using the function

Python code to find the largest of three numbers using the function

 

Suggested for you

The data type in C language

Variable in C language

Operator in C language

If statements in C language

Function in C language

 

Cpp program:find greatest of three numbers using function
Python 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.

Recent Posts

PHP Star Triangle pattern program

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

2 months 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