In this article if else program in CPP we give the if else statement programs such as to find small number among three different number, to find given number is odd or even etc.
if else program in CPP:
// 1. To find odd or even number using class
#include<iostream.h>
#include<conio.h>
class OddEven
{
private:
int n; // data member
public:
void getn() // member functions
{
cout<<“\n\n Enter the Number: “;
cin>>n;
}
void show();
};
void OddEven::show() // Outside defination
{
if(n%2==0)
{
cout<<“\n\n”<<n<<” is Even number.”;
}
else
{
cout<<“\n\n”<<n<<” is Odd number.”;
}
}
void main()
{
OddEven obj; // Create a object
clrscr();
obj.getn(); // Through Object call to Member function
obj.show();
getch();
}
Output:
Enter the Number: 10
10 is Even number.
// 2. To find given year is Leap or Not.
#include<iostream.h>
#include<conio.h>
class LeapYear
{
private:
int year; // data member
public:
void getyear() // member functions
{
cout<<“\n\n Enter the year: “;
cin>>year;
}
void show();
}obj; // Object created
void LeapYear::show() // Outside definition
{
if(year%4==0 || year%400==0)
{
cout<<“\n\n”<<year<<” is leap year.”;
}
else
if(year%100==0 || year%4!=0)
{
cout<<“\n\n”<<year<<” is not leap year”;
}
}
void main()
{
clrscr();
obj.getyear();
obj.show();
getch();
}
Output:
Enter the year: 2025
2025 is not leap year.
// 3. To find small number among three different numbers
#include<iostream.h>
#include<conio.h>
class SmallNo
{
private:
int a,b,c; // data member
public:
void getnum() // member functions – inside definition
{
cout<<“\n\n Enter the Three Different numbers: “;
cin>>a>>b>>c;
}
void show() // inside definition
{
if(a<b && a<c)
{
cout<<“\n\n First number is small : “<<a;
}
else
if(b<c)
{
cout<<“\n\n Second number is small : “<<b;
}
else
{
cout<<“\n\n Thried number is small : “<<c;
}
}
};
void main()
{
SmallNo p; // create a object
clrscr();
p.getnum();
p.show();
getch();
}
Output:
Enter the Three Different numbers: 10
20
30
First number is small : 10
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining