Home » C++ programming language

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.


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.





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.