A simple example of Ternary Operator in C++ (check whether a year if Leap or not)

Example of Ternary/Conditional Operator in C++: using Example to check Leap year, we will learn what Ternary operator is, how it is works in C/C++?

Ternary Operator

It is called Ternary Operator because it has 3 operands to operate a statement. It is also known as Conditional Operator or ? : (Questions Mark and Colon) Operator.

The three operands are:

  1. Operand1: Condition_part (Here, we write a conditional statement to validate).
  2. Operand2: True_part (Here, we write statement to execute if conditional part is true).
  3. Operand3: False_part (Here, we write statement to execute if conditional part is false).

Syntax:

Operand1? Operand2: Operand3;

In this program, we are using Ternary (Conditional) operator to check whether a Year is Leap Year or not.

Example/program: Given a year and we have to check whether it is Leap year or not using Ternary Operator/Conditional Operator.

Consider the program:

/*
C++ program to check whether entered year is Leap year or not
*/

#include <iostream>
using namespace std;

int main()
{
	int year;

	//input a year 
	cout<<"\nEnter year: ";
	cin>>year;
	
	/*
	if( (year%100!=0 && year%4==0) || (year%400==0) )
		cout<<year<<" is a Leap year."<<endl;
	else
		cout<<year<<" is not a Leap year."<<endl;

	*/
	
	//using TERNARY OPERATOR/CONDITIONAL OPR/?: OPR
	( (year%100!=0 && year%4==0) || (year%400==0) ) ?
		cout<<year<<" is Leap year."<<endl :
		cout<<year<<" is not a Leap year."<<endl;

	return 0;
}

Output

First run:
Enter year: 2000
2000 is Leap year.

Second run:
Enter year: 2017
2017 is not a Leap year.

Third run:
Enter year: 1900
1900 is not a Leap year.

Fourth run:
Enter year: 2016
2016 is Leap year.
Advertisement

program using goto to run program until input year is not 0

/*
C++ program to check whether entered year is Leap year or not
*/

#include <iostream>
using namespace std;

int main()
{
	int year;

	//Label to run again program from here
	INIT:
	//input a year 
	cout<<"\nEnter year (ENTER 0 for exit...): ";
	cin>>year;

	/*
	if( (year%100!=0 && year%4==0) || (year%400==0) )
		cout<<year<<" is a Leap year."<<endl;
	else
		cout<<year<<" is not a Leap year."<<endl;

	*/
	
	//using TERNARY OPERATOR/CONDITIONAL OPR/?: OPR
	( (year%100!=0 && year%4==0) || (year%400==0) ) ?
		cout<<year<<" is Leap year."<<endl :
		cout<<year<<" is not a Leap year."<<endl;




	//////////////
	//condition to go to label INIT or end of the program
	if(year!=0)
		goto INIT;
	//////////////
	return 0;
}

Output

Enter year (ENTER 0 for exit...): 2000
2000 is Leap year.

Enter year (ENTER 0 for exit...): 2017
2017 is not a Leap year.

Enter year (ENTER 0 for exit...): 1900
1900 is not a Leap year.

Enter year (ENTER 0 for exit...): 2016
2016 is Leap year.

Enter year (ENTER 0 for exit...): 0 
0 is Leap year.

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.