Biggest of 3 Numbers: C

Lets write C program to find biggest of 3 numbers, using nested if else statement.

Related Read:
Nested if else Statement In C
Relational Operators In C

Biggest of 3 Numbers Using Ternary Operator: C

C Program To Find Biggest of 3 numbers

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter 3 numbers\n");
    scanf("%d %d %d", &a, &b, &c);

    if( a > b && a > c)
    {
        printf("%d is biggest\n", a);
    }
    else
    {
        if( b > c )
        {
            printf("%d is biggest\n", b);
        }
        else
        {
            printf("%d is biggest\n", c);
        }
    }

    return 0;
}

Output 1:
Enter 3 numbers
1
2
3
3 is biggest

Output 2:
Enter 3 numbers
10
18
15
18 is biggest

Here first we check if a is greater than b. If yes, then we display a as big. Else b or c is biggest. So now we check if b is greater than c, if yes, we display b as biggest else c is biggest. Nested if else condition works great for programs like this.

Biggest of 3 numbers: C Program


[youtube https://www.youtube.com/watch?v=iy–Q0cLnfc]

YouTube Link: https://www.youtube.com/watch?v=iy–Q0cLnfc [Watch the Video In Full Screen.]


C Program To Find Biggest of 3 numbers

 
#include < stdio.h >

int main()
{
    int a, b, c, big;

    printf("Enter 3 numbers\n");
    scanf("%d %d %d", &a, &b, &c);

    if( a > b && a > c)
    {
        big = a;
    }
    else
    {
        if( b > c )
        {
            big = b;
        }
        else
        {
            big = c;
        }
    }

    printf("Biggest of 3 number is %d\n", big);

    return 0;
}

Output:
Enter 3 numbers
500
550
555
Biggest of 3 number is 555

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Biggest of Two Numbers: C

In this video tutorial we ask the user to enter 2 integer numbers and using if else and relational operator we check and display the biggest of 2 numbers on the console.

Related Read:
if else statement in C
Relational Operators In C

C Program To Find Biggest of 2 numbers

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    if(a > b)
    {
        printf("Biggest of %d and %d is %d\n", a, b, a);
    }
    else
    {
        printf("Biggest of %d and %d is %d\n", a, b, b);
    }

    return 0;
}

Output 1:
Enter 2 numbers
20
30
Biggest of 20 and 30 is 30

Output 2:
Enter 2 numbers
200
300
Biggest of 200 and 300 is 300

Biggest of 2 numbers: C Program


[youtube https://www.youtube.com/watch?v=Bx_aowugT3E]

YouTube Link: https://www.youtube.com/watch?v=Bx_aowugT3E [Watch the Video In Full Screen.]


C Program To Find Biggest of 2 numbers

 
#include < stdio.h >

int main()
{
    int a, b, big;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    if(a > b)
    {
        big = a;
    }
    else
    {
       big = b;
    }

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output:
Enter 2 numbers
75
100
Biggest of 75 and 100 is 100

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

if else statement in C

In our previous video tutorial we saw how to use the decision control instruction if. In this tutorial we shall see how we can use if else statement in division of 2 numbers example.

We illustrate the use of if else statement in this video tutorial with the help of a division of 2 numbers example program.

Division of 2 Numbers: if else construct in C

Single statement in both if and else block

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter value of a and b for division (a/b)\n");
    scanf("%d%d", &a, &b);

    if( b != 0)
        printf("Division of %d and %d is %d\n", a, b, a/b);
    else
        printf("You can't divide a number by 0\n");

    return 0;
}

Output 1:
Enter value of a and b for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value of a and b for division (a/b)
50
0
You can’t divide a number by 0

Note: When condition for if is true, if block gets executed. When condition in if is false, else block code gets executed.

Multiple statements in both if and else block

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter value of a and b for division (a/b)\n");
    scanf("%d%d", &a, &b);

    if( b != 0)
    {
        c = a / b;
        printf("Division of %d and %d is %d\n", a, b, c);        
    }
    else
    {
       printf("You can't divide a number by 0\n");
       printf("Please run the program once again and give proper value\n");
    }


    return 0;
}

Output 1:
Enter value of a and b for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value of a and b for division (a/b)
50
0
You can’t divide a number by 0
Please run the program once again and give proper value

In above example we are enclosing if and else block code in curly braces as both if and else has multiple lines of code or block of code to be executed.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

if else Construct in C: Division of 2 numbers logic


[youtube https://www.youtube.com/watch?v=WLLJn3hcFa0]

YouTube Link: https://www.youtube.com/watch?v=WLLJn3hcFa0 [Watch the Video In Full Screen.]


This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Biggest of 3 Numbers Using Ternary Operator: C++

Video tutorial to find the biggest of the three integer numbers in C++, using if-else control statements and Ternary Operator.

Before watching this video, please make sure to watch and understand this short video: Find Biggest of 3 Numbers: C++

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream .h>
#include<conio .h>
 
void main()
{
int a, b, c;
clrscr();
 
cout< <"Enter 3 no's\n";
cin>>a>>b>>c;
 
int big =  ( a>b && a>c )?a:(b>c?b:c);
/*
if( a > b && a > c )
  big = a;
else if( b > c )
  big = b;
else
  big = c;
*/cout< <"\nAmong "<<a<<" , "<<b<<" , "<<c<<" Biggest is "<<big;
getch();
}

In this program we take 3 integer values from the user and using ternary operator decide the biggest of 3 numbers.

int big =  ( a>b && a>c )?a:(b>c?b:c);

if ( a > b && a > c ) is true, then value of a will be stored in variable big; else ( b > c ? b : c ) will be evaluated. Here, if value of b is greater than c, value of b will be stored in variable big else value of c will be stored in the variable big.

Video Tutorial: Biggest of 3 Integer Numbers


[youtube https://www.youtube.com/watch?v=gfmqT4PwfY0]

YouTube Link: https://www.youtube.com/watch?v=gfmqT4PwfY0 [Watch the Video In Full Screen.]



Output
Enter 3 no’s
10
20
30
Among 10, 20, 30 Biggest is 30

Find Biggest of 3 Numbers: C++

Video tutorial to find the biggest of the three integer numbers in C++, using if-else control statements.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream .h>
#include<conio .h>
 
void main()
{
int a, b, c;
clrscr();
 
cout< <"Enter 3 no\n";
cin>>a>>b>>c;
 
int big;
 
if( a > b )
 big = a;
else
{
 if( b > c )
  big = b;
 else
  big = c;
}
cout< <"\nBiggest is: "<<big;
getch();
}

In this program we get three integer variables from the user.
If value of variable a is bigger, then we store it inside the variable big; else we compare b with c. Now will store the biggest number inside the variable big.
Video Tutorial: Biggest of 3 Integer Numbers


[youtube https://www.youtube.com/watch?v=YRkCpS_GMFw]

YouTube Link: https://www.youtube.com/watch?v=YRkCpS_GMFw [Watch the Video In Full Screen.]



Output
Enter 3 no’s
404
501
301
Biggest is: 501