Finding largest number from user input in c programming

Find the largest number: 

Finding largest number

The logic of finding a large number is so easy.
We have to declare some variables and just compare them with each other. For example, we have 3 numbers to compare and we have to find the biggest number from them. The logic of comparing 3 numbers and find the biggest one is pretty simple.

Think, we have numbers 5, 15, and 10. So we can first compare the first two numbers with a condition like, is (5 > 15)? If this condition is true, then we will take 5 as the bigger number. But here the condition is not true.
5 is not bigger than 15. So, there will be other condition like:



if (5>15){
//pick 5 as a big number
}
else{
//15 is a big number
}

We have found a big number from the first two numbers. Now time to compare the third number with our founded number.

Now again the condition is

if(15>10){
 //15 is the big number
}
else{
//10 is the big number
}

Here we have compared the first two numbers and then compare the third number with the biggest number between the first two numbers.
If we put our values into the integer type variable like:


int a = 5;
int b = 15;
int c = 10;

Then the full code looks like:



I hope the basic logic is now clear to find the largest number among three integers.

Now if we want to check different 3 numbers from user input, we just need to take input from the user. For this, we have to use scanf function.

If we want to input the value of int a from user, we have to use scanf ("%d",&a);
We also can ask user to give the input by following:


int a, b, c;
    printf ("input a : ");
    scanf ("%d",&a);
    printf ("input b : ");
    scanf ("%d",&b);
    printf ("input c : ");
    scanf ("%d",&c);

Now, the complete code will look like:



If you have any more questions about Finding the largest number from user input in c programming, please comment below.

Happy Coding

You may also like to see:

How to print Stars pattern in C                                           How to print Stars pattern in java

 

0/Post a Comment/Comments