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

Find Biggest In An Array, Without Sorting It: C

First we have to assume that a[0] is biggest. Now store the value of a[0] inside a variable called big. Now compare value of big with other values in the array. If big is less than any other value, then store the bigger value inside variable big.

Video Tutorial: Biggest In An Array, Without Sorting It: C


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

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



Note that, the previous value inside the variable big is discarded.

Full source code

#include < stdio.h >
#include < conio.h >

void main()
{
int big, a[20], N, pos, i;
clrscr();

printf("Enter the array size\n");
scanf("%d", &N);

printf("Enter %d elements\n", N);
for(i = 0; i < N; i++) 
           scanf("%d", &a[i]);

         big = a[0];
         pos = 0;

         for(i = 1;  i < N; i++)
          if(a[i] > big)
  {
    big = a[i];
    pos = i+1;
    }

 printf("Big is %d and its position is %d", big, pos);

 getch();
}

Output
Enter the array size
5
Enter 5 elements
2
0
100
108
55
Big is 108 and its position is 4

Biggest of 2 Numbers Using Function: C++

Find the biggest of two numbers using function and ternary operator.

Basics of functions:

A function is a sub program to perform a specific task.
OR
a group of instructions to perform specific task.

Along with main() it is possible to define our own functions by following these steps:
1. Function prototype or Function declaration.
2. Function call.
3. Function definition.

Function Prototype:

Giving information about the function such as return type, function name and arguments type to the compiler is known as function prototype; It is written at the declaration section.
Syntax:

< return_type > < function_name >( arguments_type );

Example:
int findsum(int a, int b);
float findsum(int a, int b);
void findsum(int a, int b);
int findsum(int a[], int size);
int findsum(int, int);

Function Definition:
Writing the actual code of the function in a block. It is at this stage the task of the function is defined. It is written after the main function.
Syntax:

< return_type >< function_name >(parameters)
{
 
}

Example:

int findsum(int a, int b)
{
 int sum;
 
 sum = a + b;
 return(sum);
}

Function Call:
It is a technique used to invoke a function.
Syntax:

[variable] < function_name >([arguments]);

Example:
res = findsum(10, 20);
res = findsum(x, y);
res = findsum();

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 Big(int x,int y);  // Prototype
 
  int a, b;
  clrscr();
 
  cout< <"Enter 2 numbers\n";
  cin>>a>>b;
 
  int res = Big(a, b);   // Function call
 
  cout< <"Biggest = "<<res;
  getch();
}
 
int Big(int x, int y)   // Function Definition
{
  return( x>y?x:y );
}

You must also watch these videos, before continuing with this program:
Find Biggest of 2 Numbers: C++
Biggest of Two Numbers Using Ternary Operator: C++

Video Tutorial: Biggest of 2 Numbers Using Function


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

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



Output:
Enter 2 numbers
420
305
Biggest = 420

Biggest of Two Numbers Using Ternary Operator: C++

Video tutorial to find the biggest of the two integer numbers in C++, using Ternary Operator.

To find biggest of Two numbers using if-else control structure: Find Biggest of 2 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
24
25
26
#include < iostream .h >
#include < conio .h >
 
void main()
{
 int a, b;
 clrscr();
 
 cout< <"Enter 2 numbers\n";
 cin>>a>>b;
 
 int big;
 
 big = a > b ? a : b;
 
/*
 if( a > b )
  big = a;
 else
  big = b;
*/ 
 cout< <"\nBig is: "<<big;
 
 getch();
}

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

 big = a > b ? a : b;

if ( a > b ) is true, then value of a will be stored in variable big; else value of b will be stored in big.

Video Tutorial: Biggest of 2 Integer Numbers Using Ternary Operator


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

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




Output
Enter 3 no’s
404
302
Big is: 404