51 : C Program to Print Greatest of two number using ternary operator

Check out the complete list of c-programs : C Program List    

We have already see how to check greatest among two numbers using if-else condition. This can also be done using ternary/conditional operator.

Logic :

(number1 > number2) ? printf("%d is greater then %d",number1,number2) : printf("%d is greater then %d",number2,number1); }

Variables :

number1 : Datatype int : Holds value of number 1 inputted by the user. %d is the Format specifier for int

number2 : Datatype int : Holds value of number 2 inputted by the user.

* 1000+ C programs + tutorials
* 51_c_program_to_find_greatest_among_2_numbers_using_ternary_operator
* Created on: Oct 20, 2014
* Author: Code2care.org
//#include <conio.h>

void main() {

  int number1,number2;

//    clrscr();

	printf("C Program to find Greatest of Two numbers using Ternary Operator :  ");

	printf("\n\n Enter number 1  : ");
	scanf("%d", &number1);
    printf("\n\n Enter number 2  : ");
	scanf("%d", &number2);
   (number1 > number2) ? printf("\n\n  %d is greater then %d",number1,number2) : printf("\n\n %d is greater then %d",number2,number1);

//	getch();


The best way to learn C programming is to practice more and more of programs . Code2care C Programming tutorials provide 1000+ programs in C that you can study and become an expert in the language. Programs are divided into categories depending upon type and complexity.

BSc. IT, BSc. Computer Science, BSc. IT, MSc. IT, MSc. Computer Science, MCA, BTech IT & BTech Engineers may find these programs very useful.