4/01/2009

C++ Program To Perform Arithmetic Operation Using Class And Object


/* C++ Program To Perform Arithmetic Operation Using Class And Object */

#include <iostream>

using namespace std;

class arithmetic
{
    float a,b,sum,div,mul,diff;
public:
    void getdata();
    void putdata();
};
void arithmetic::getdata()
{
    cout<<"Enter The Value For Two Numbers:"<<endl;
    cin>>a>>b;
}
void arithmetic::putdata()
{
    sum=a+b;
    cout<<"Sum Of Two Numbers:"<<sum<<endl;
    diff=a-b;
    cout<<"Difference Of Two Numbers:"<<diff<<endl;
    mul=a*b;
    cout<<"Product Of Two Numbers:"<<mul<<endl;
    div=a/b;
    cout<<"Division Result:"<<div<<endl;

}

int main()
{
    arithmetic a;
    a.getdata();
    a.putdata();

    return 0;
 }
Out Put:
Enter The Value For Two Numbers:
21
7
Sum Of Two Numbers:28
Difference Of Two Numbers:14
Product Of Two Numbers:147
Division Result:3

C++ Program To Calculate The Sum Of All Even And Odd Numbers Up to N


/* C++ Program To Calculate The Sum Of All Even And Odd Numbers Up to N */

#include <iostream>

using namespace std;

int main()
{
    int i=1,number,evensum=0,oddsum=0;
      cout<<"Enter The Number:"<<endl;
        cin>>number;
      int temp=number;
        while(i<=number)
        {
            if(i%2==0)
            {
                cout<<i;
                cout<<"\t";
                evensum=evensum+i;
                i++;
            }
            else
            {
                cout<<i;
                cout<<"\t";
                oddsum=oddsum+i;
                i++;
            }
        }

            cout<<"\nSum Of Even Numbers: "<<evensum<<endl;
            cout<<"Sum Of Odd Numbers: "<<oddsum<<endl;


    return 0;
 }
Out Put:
Enter The Number:
6
1       2       3       4       5       6
Sum Of Even Numbers: 12
Sum Of Odd Numbers: 9

C++ Program To Check Whether The Given Number Is Palindrome Or Not


/* C++ Program To Check Whether The Given Number Is Palindrome Or Not */

#include <iostream>

using namespace std;

int main()
{
    int number,rev=0;
      cout<<"Enter The Number:"<<endl;
        cin>>number;
      int temp=number;
        while(number>0)
        {
            int rem=number%10;
            number=number/10;
            rev=(rev*10)+rem;
        }
        if(rev==temp)
            cout<<"The Given Number "<<temp<<" Is A Palindrome Number."<<endl;
        else
            cout<<"The Given Number "<<temp<<" Is Not A Palindrome Number."<<endl;


    return 0;
 }
Out Put:
Enter The Number:
121
The Given Number 121 Is A Palindrome Number.

C++ Program To Find Minimum And Maximum Of Two Numbers Using Function


/* C++ Program To Find Minimum And Maximum Of Two Numbers Using Function */

#include <iostream>

using namespace std;

void min(int,int);
void max(int,int);
void max(int a,int b)
{
    float sum=0;
    if(a>b)
    {
        cout<<"Max of numbers is A:"<<a<<endl;
        cout<<"Min of numbers is B:"<<b<<endl;
    }
    else
        min(a,b);

}
void min(int a,int b)
{
        cout<<"Max of numbers is B:"<<b<<endl;
        cout<<"Min of numbers is A:"<<a<<endl;
}
int main()
{
    int a, b, c;
      cout<<"Enter The Values For A And B:"<<endl;
        cin>>a>>b;
    max(a,b);

    return 0;
 }
Out Put:
Enter The Values For A And B:
34
65
Max of numbers is B:65
Min of numbers is A:34

C++ Program To Find The Biggest Of 3 Given Numbers


/* C++ Program To Find The Biggest Of 3 Given Numbers */

#include <iostream>

using namespace std;

class Number
{
    int a,b,c;
public:
    void getdata();
    void showdata();
};
void Number::getdata()
{
    cout<<"Enter three Numbers:\n";
    cin>>a>>b>>c;
}
void Number::showdata()
{
    if((a>b)&&(a>c))
        cout<<"Biggest Number:"<<a<<endl;
    else if((b>a)&&(b>c))
        cout<<"Biggest Number:"<<b<<endl;
    else
        cout<<"Biggest Number:"<<c<<endl;
}

int main()
{
    Number m1;
    m1.getdata();
    m1.showdata();

    return 0;
}
Out Put:
Enter three Numbers:
54
58
98
Biggest Number:98

C++ Program To Calculate The Average Of 3 Numbers


/* C++ Program To Calculate The Average Of 3 Numbers */

#include <iostream>

using namespace std;

void total(int a,int b, int c)
{
    float sum=0;
    sum=a+b+c;
        cout<<"Sum Of Numbers:"<<sum<<endl;
}
void average(int a,int b, int c)
{
    float avg=(a+b+c)/3.0;
        cout<<"Average Of Numbers:"<<avg<<endl;
}
int main()
{
    float a, b, c;
      cout<<"Enter Three Numbers:"<<endl;
        cin>>a>>b>>c;
    total(a,b,c);
    average(a,b,c);

    return 0;
 }
Out Put:
Enter Three Numbers:
10
20
30
Sum Of Numbers:60
Average Of Numbers:20