In this tutorial, we will discuss the concept of the C program:find smallest of three numbers using function
In this post, we will learn how to find the smallest number among three numbers using a user-defined function in the C programming language
In my previous post, I have explained the various approaches to find the smallest number of among three numbers in C language
However, in this program, we embed the logic of the “find the smallest of three number” program in the function.
Here, I have explained how to find the smallest number from the given three number and how to embed this logic in the function.
Program 1
This program allows the user to enter three numbers and compare to select the smallest number using nested if statements
#include <stdio.h> #include <stdlib.h> int biggestNum(int, int,int);//function prototype int main() { int num1,num2,num3; //declare the variables printf("Enter the first number: "); scanf("%d",&num1);//get input from user for num1 printf("Enter the second number: "); scanf("%d",&num2);//get input from user for num2 printf("Enter the third number: "); scanf("%d",&num3);//get input from user for num3 //function call biggestNum(num1,num2,num3); getch(); return 0; } int biggestNum(int num1,int num2,int num3){//function definition if(num1<num2){//compare num1 and num2 if(num1<num3){//compare num1 and num3 printf("\nSmallest number is: %d\n",num1); } else{ printf("\nSmallest number is: %d\n",num3); } } else{ if(num2<num3){//compare num2 and num1 printf("\nSmallest number is: %d\n",num2); } else{ printf("\nSmallest number is: %d\n",num3); } } }
When the above code is executed, it produces the following results
Enter the first number: 67 Enter the second number: 98 Enter the third number: 34 Smallest number is: 34
Program 2
This program allows the user to enter three numbers and compare to select the smallest number using if-else-if statements
#include <stdio.h> #include <stdlib.h> int biggestNum(int, int,int);//function prototype int main() { int num1,num2,num3; //declare the variables printf("Enter the first number: "); scanf("%d",&num1);//get input from user for num1 printf("Enter the second number: "); scanf("%d",&num2);//get input from user for num2 printf("Enter the third number: "); scanf("%d",&num3);//get input from user for num3 //function call biggestNum(num1,num2,num3); getch(); return 0; } int biggestNum(int num1,int num2,int num3){//function definition if(num1<num2&&num1<num3){ printf("\nSmallest number is:%d",num1); } else if(num2<num3){//compare num2 and num1 printf("\nSmallest number is:%d",num2); } else{ printf("\nSmallest number is:%d",num3); } }
When the above code is executed, it produces the following results
Enter the first number: 67 Enter the second number:23 Enter the third number: 57 Smallest number is:23
Smiler post
C++ program to find the smallest among three numbers using the function
Python code to find the smallest of three numbers using the function
Java code to find the smallest of three numbers using the method
Suggested for you
Nested if statements in C language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…