printf("Biggest of %d and %d is %d\n", a, b, biggest(a, b));
return 0;
}
//function definition
int biggest(int x, int y)
{
return( x>y?x:y );
}
#include<stdio.h>
int biggest(int, int); // function prototype
int main()
{
int a, b;
printf("Enter 2 integer numbers\n");
scanf("%d%d", &a, &b);
// function call biggest(a, b)
printf("Biggest of %d and %d is %d\n", a, b, biggest(a, b));
return 0;
}
//function definition
int biggest(int x, int y)
{
return( x>y?x:y );
}
Output 1 Enter 2 integer numbers 50 25 Biggest of 50 and 25 is 50
Output 2 Enter 2 integer numbers -5 -10 Biggest of -5 and -10 is -5
Logic To Find Biggest of 2 Numbers using Function
We ask the user to enter 2 integer numbers. We pass those 2 integer numbers to user defined function biggest. Inside function biggest we use ternary operator to determine the biggest number. Function biggest returns the biggest of the 2 numbers.
x>y?x:y
Here if x is greater than y, x will be returned else y will be returned.
Note: Function biggest returns integer type data. And it takes 2 arguments of type integer. We’re calling function biggest from inside printf() function.
Write a C program to find first and second biggest numbers in a list of N numbers entered by the user, without using arrays and without sorting the list and by using for loop.
Output 1: Enter the limit 5 Enter 5 positive numbers 1 9 5 3 8 First big = 9 Second Big = 8
Output 2: Enter the limit 8 Enter 8 positive numbers 1 5 9 3 7 8 6 10 First big = 10 Second Big = 9
Logic To Find First and Second Biggest Number in N Numbers, without using Arrays
First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the for loop until loop counter variable count is less than or equal to limit. For each iteration of for loop we ask the user to enter a positive integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig.
Next we check if the user entered number is greater than sbig and less than fbig, if true, we assign the value of num to sbig.
Once control exits for loop, inside fbig we will have biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers entered by the user.
Logic To Find Biggest Number of N numbers, without using Arrays
We ask the user to enter the limit. i.e., the length of the list of numbers. For Example, if user enters limit value as 5, then we accept 5 numbers from the user and then find the biggest and output that number on to the console window.
First we ask the user to enter the limit. If user enters limit value as 5, we iterate the for loop 5 times i.e., until value of count is less than or equal to limit.
We check if its the first iteration of for loop. If true, we assign the first user entered number to variable big.
Inside the for loop, for each iteration we ask the user to enter a number. Next we check if that user entered number is greater than the value present in variable big. If the new number entered by the user is greater than value present in variable big, then we assign the new number entered by the user to the variable big. We keep doing this for each number entered by the user.
Source Code: C Program To Find Biggest of N Numbers, without using Arrays, using For Loop
Write a C program to find the range of a set of numbers entered through the keyboard. Range is the difference between the smallest and biggest number in the list.
Example: If biggest number in the list is 5 and smallest number in the list is 1. The difference between them is the range. i.e., 5 – 1 = 4. So range = 4.
User Input: Enter the limit 5 Enter 5 numbers 1 2 3 4 5
Output: Small Number = 1 Big Number = 5 Range is 4
Logic To Find Range of Set of Numbers
First we ask the user to enter the length or the size of the list, and store it inside the variable limit. If the user enters the list size as 5, then we ask the user to enter 5 numbers.
Next we ask the user to enter the first number. We assign the first number entered by the user to variables small and big. Since user already entered 1 number into the list, we decrement the value of variable limit by 1.
Next we take remaining inputs inside the while loop. For each iteration of the while loop we decrement the value of variable limit by 1, until the value of limit is 0. Once value of limit is zero, control exits while loop.
For each input(inside the while loop), we check if the value entered is bigger than the value present in variable big. If its true, we assign the bigger number to variable big. We also check if the value entered is smaller than the value present in variable small. If its true, we assign the smaller number to variable small.
Once the control exits the while loop, variable big will have the biggest number in the list and variable small will have the smallest number in the list.
Finally, we use below formula to calculate range of the list: range = big – small;
Video Tutorial: C Program To Find Range of Set of Numbers