Google Add

Search

Program to Find Largest of Three Numbers - C, C++ Code


  • Write a program to find largest of three numbers.
  • C program to find largest of three numbers.
  • C++ program to find largest of three numbers.

In this program, We'll take three input numbers and print largest among three numbers. To solve this problem, first let's write an algorithm to find largest number among three numbers.

Program to Find Largest of Three Numbers


Algorithm to Find Largest of Three Numbers



 1. Declare three variable a ,b, c.

 2. Compare a with b and c. If a is greater than b and c than a is greatest among three numbers.

 3. Compare b with a and c. if b is greater than a and c than b is greatest among three numbers.

 4. Compare c with a and b. If c is greater than a and b than c is greatest among three numbers.

Program to find second largest element in an array

Find largest element in an array


C Program to Find Largest of  Three Numbers


We have discussed the algorithm to find largest among three numbers. Let's write a c code to find largest among three input numbers.

#include <stdio.h>
 
int main() {
 
      int a, b, c;
 
      printf("Enter three numbers: ");
      scanf("%d %d %d", &a, &b, &c);
 
      //If a is greater than b and c

      if(a >= b && a >= c){
         printf("Largest number is %d", a);
 
      //if b is greater than a and c

      }else if(b >= a && b >= c){
         printf("Largest number is %d", b);
 
      //If c is greater than a and b

      }else if(c >= a && c >= b){
         printf("Largest number is %d", c);
 
      } 
      return 0;
}

Output -

Enter three numbers :

14
34
23

Largest number is : 34


C Program Find Largest of three Numbers using Ternary Operator


In this programming example, We are going to use conditional operator to print largest of three numbers.
#include <stdio.h>

int main(void) {
 
    int a, b, c, larg;
 
    printf("Enter three numbers \n");
    scanf("%d %d %d", &a , &b , &c);
 
 
    larg =  (a > b && a > c) ? a : b > c ? b : c ;
 
    printf("Largest number is %d ", larg);

 
    return 0;
}


C++ Program Find Largest of three Numbers


We have written a code, Let's write a C++ code to print larges among three numbers.
#include <iostream>
using namespace std;

int main() {
 
    int a, b, c;
 
    cout << "Enter three numbers \n";

    /* Taking input */
    cin >> a >> b >> c;
 
    /* If a is greater than b and c. */

    if (a > b && a > c) {
       cout << "Largest number is " << a;

    /* If b is greater than a and c */
    } else if (b > a && b > c) {
       cout << "Largest number is " << b;

    } else {
       cout << "Largest number is "<< c;

    }
 
     return 0;
}




Programming question on Arrays

Programming question on Linked List

Sorting algorithm and their time complexity

Stack implementation

Programming questions using Recursion

C, C++ interview questions

Programming Books

C Program to Print Even Numbers between 1 to 100 using For and While Loop


  • Write a C program to print even numbers between 1 to 100 using for and while loop.  
  • Write a C program to print even numbers between 1 to N. 


In this tutorial, we are going to write a c program which prints even numbers between 1 to 100. In another example, we write a c code which prints even numbers between 1 to N (N is an input number by a user).

C Program to Print Even Numbers between 1 to 100 using For and While Loop


What is Even Numbers?

An even numbers are those numbers which is exactly divided by 2.

For example - 2, 4 , 6 etc. are even numbers.

C++ program to print even numbers between 1 to 100

Program to print odd and even numbers of an array

Program to check whether number is even or odd

C interview questions with answers

C Program to Print Even Numbers from 1 to N


#include <stdio.h>

int main(void) {
 
   int n;
 
   printf ("Enter a number \n");
   scanf ("%d", &n);
 
   printf ("Even numbers from 1 to %d is : ",n);
 
   for (int i = 1; i <= n; i++) {
  
      if ( i % 2 == 0) {
   
          printf (" %d ", i);
      }
  
  }
 
 return 0;
}


C Program to Print Even Numbers between 1 to 100 using While Loop


#include <stdio.h>

int main() {
 
     int i=1;

     /* Run a loop from 1 to 100 */
     while( i <= 100) {
  
      /* If number is divisible by 2
         then it's an even number
      */
  
      if(i % 2 == 0){
   
        printf(" %d ", i);
      }
  
        /* Increment i. */
  
        i++;
     }
    
    return 0;
}

Output -

2  4  6  8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  
42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  
80  82  84  86  88  90  92  94  96  98  100 

C Program to Print Even Numbers between 1 to 100 using For Loop


#include <stdio.h>

int main() {
 
     /* Run a loop from 1 to 100 */
    for(int i = 1; i <= 100; i++) {
  
      /* If number is divisible by 2
         then it's an even number
      */
        if(i % 2 == 0){
   
           printf(" %d ", i);
        }
    }
    
    return 0;
}

C++ Program to Print Even Numbers between 1 to 100 using For & While Loop


  • Write a C++ program to print even numbers between 1 to 100. 
  • Write a C++ program to print even numbers between 1 to N.


In this tutorial, we are going to write a c++ code which print even numbers between 1 to 100 using for and while loop.

C program to print even numbers between 1 to 100

C++ Program to Print Even Numbers between 1 to 100

C++ Program to Print Even Numbers between 1 to 100 using For Loop


#include <iostream>
using namespace std;

int main() {
 
    int i;
 
    /* Run a loop from 1 to 100. */
 
    for(i = 1; i < = 100; i++){
  
       /* If number is divisible by 2. */
  
      if(i % 2 == 0) {

         cout << i <<" "; 
      }
   }
    return 0;
}


Output : 


2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 
42 44 46 48 50 52 54 56 58 60 62 64 66 68 70
72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

C++ Program to Print Even numbers between 1 to 100 using While Loop


#include <iostream>
using namespace std;

int main() {
 
    /* Initialize i with 1. */
 
    int i=1;
 
   /* If i is less than or equal to 100. */
 
   while( i <= 100){
  
      /* If number is divisible by 2, then print.*/
  
      if(i % 2 == 0){
   
         cout <<i<< " ";
      }
  
     /* Increment i. */
  
     i++;
 }
 
 return 0;
}


C++ Program to Print Even numbers between 1 to N


In above examples, we have written a code to print even numbers between 1 to 100 using for and while loop. In this example, instead of 100 we take a input value from user and print even numbers between 1 to n (where n is input by user).

#include <iostream>
using namespace std;

int main() {
 
    /* Initialize i with 1. */
 
    int i=1, n;

    cout << "Enter a number \n";
    cin >> n;
 
   /* If i is less than or equal to n. */
 
   while( i <= n){
  
      /* If number is divisible by 2
         then it's an even number
      */
  
      if(i % 2 == 0){
   
         cout <<i<< " ";
      }
  
     /* Increment i. */
  
     i++;
 }
 
 return 0;
}


C interview questions with answers

Programming question on strings

Sorting algorithm