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

// 4. To find max number among two numbers

#include<iostream.h>
#include<conio.h>
class MaxTwo
{
    private:
        int x,y;      // data members
    public:
        void getxy()      // member fuctions – Inside definition
        {
            cout<<“\n\n Enter the two numbers: “;
            cin>>x>>y;
        }
        void show_max();
};
void MaxTwo::show_max()      // Member function – outside definition
{
    if(x>y)
    {
        cout<<“\n\n First number is Largest Number.: “<<x;
    }
    else
    if(y>x)
    {
        cout<<“\n\n Second number is Largest Number.: “<<y;
    }
    else
    {
        cout<<“\n\n Both are equals: “<<x;
    }
}
void main()
{
    MaxTwo p;     // Create a object
    clrscr();
    p.getxy();
    p.show_max();
    getch();
}

Output: 
Enter the two numbers: 10

15
Second number is Largest Number.: 15
// 5. To find given number is +ve, -ve or Nutral
#include<iostream.h>
#include<conio.h>
class PosNev
{
    private:
        int num;        // data member
    public:
        void getnum()     // member functions
        {
            cout<<“\n\n Enter the number: “;
            cin>>num;
        }
        void show()
        {
            if(num>0)
            {
                cout<<“\n\n”<<num<<” is +ve Number.”;
            }
            else
            if(num<0)
            {
                cout<<“\n\n”<<num<<” is -ve Number.”;
            }
            else
            {
                cout<<“\n\n”<<num<<” is Nutral”;
            }
        }
}obj;
void main()
{
    clrscr();
    obj.getnum();
    obj.show();
    getch();
}

Output:

Enter the number: 50
50 is +ve Number.

// 6. To find the grade of student

#include<iostream.h>
#include<conio.h>
class StudG
{
    private:
        int c,cpp,java;       // data members
    public:
        void getmarks()      // member functions
        {
            cout<<“\n\n Enter the three subject marks: “;
            cin>>c>>cpp>>java;
        }
        void show();
};
void StudG::show()
{
    int tot;
    float per;
    tot=c+cpp+java;
    cout<<“\n\n Student Subject Wise Marks: “;
    cout<<“\n C Programming : “<<c;
    cout<<“\n C++ : “<<cpp;
    cout<<“\n Java Programming : “<<java;
    tot=c+cpp+java;
    cout<<“\n\n Total Marks: “<<tot;
    per=tot/3;
    cout<<“\n\n Percentage: “<<per;
    if(per>=40 && per<50)
    {
        cout<<“\n\n C Grade.”;
    }
    else
    if(per>=50 && per<60)
    {
        cout<<“\n\n B Grade.”;
    }
    else
    if(per>=60 && per<70)
    {
        cout<<“\n\n A Grade.”;
    }
    else
    if(per>=70 && per<80)
    {
        cout<<“\n\n A++ Grade.”;
    }
    else
    if(per>=80)
    {
        cout<<“\n\n O Grade.”;
    }
    else
    {
        cout<<“\n\n Failed”;
    }
}
void main()
{
    StudG p;
    clrscr();
    p.getmarks();
    p.show();
    getch();
}
Output:

Enter the three subject marks: 70
80
90

Student Subject Wise Marks:
C Programming : 70
C++ : 80
Java Programming : 90
Total Marks: 240
Percentage: 80
Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *