google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: More C++ programs

Wednesday, December 18, 2013

More C++ programs

//Program to display Floyd triangle:-
#include<iostream.h>
main()
{
    int line,i,num=1,incr;
    cout<<" How many lines = ";
    cin>>line;
    cout<<"\n \n Floyds Triangle \n \n";
    for(i=1;i<=line;i++)
    {
        for(incr=1;incr<=i;incr++)
        {
            cout<<num;
            cout<<"\t";
            num++;
        }
        cout<<"\n";
    }
}

Output:-

How many lines = 7

Floyds triangle


1
2           3
4           5           6
7           8           9           10
11         12         13         1 4          15
16         17         18         19            20          21
22          23         24         25           26          27          28



//Program to check whether number is prime or not:-

#include<iostream.h>
main()
{
    int num,temp=0,i;
    cout<<"\n Enter the number =";
    cin>>num;
    if(num>0)
    {
        for(i=2;i<=num-1;i++)
        {
            if(num%i==0)
            {
                cout<<"\n Number is not a Prime number \n \n";
                temp=1;
                break;
            }
        }
        if(temp==0)
        {
            cout<<"\n Number is Prime \n \n";
        }
    }
    else
        cout<<"Enter only Positive numbers";
}


Output:-

Enter the number = 7
Number is Prime

//Program of destructors in c++:-

#include<iostream.h>
class ABC
{
int *width,*height;
public:
    ABC(int,int);
    ~ABC();
        int area()
    {
            return(*width**height);
    }
};
ABC::ABC(int a,int b)
{
width=new int;
height= new int;
*width=a;
*height=b;
}
ABC::~ABC()
{
    delete width;
    delete height;
   
}
main()
{
    ABC obj1(4,9),obj2(7,5);
    cout<<obj1.area();
    cout<<"\n";
    cout<<obj2.area();
}


Output :-
36
35

//Program of static member variable and member function:
#include<iostream.h>
class ABC
{
private :static int data;
public: static void getdata()
        {
            ++data;
            cout<<"count is =";
            cout<<data<<"\n";
        }
     void putdata()
        {
            ++data;
            cout<<"now count is ="<<data<<"\n";
        }
 void newfunction()
{
++data;
cout<<"now count is ="<<data;
}
};
int ABC::data=5;
void main()
{
    ABC obj;
    ABC::getdata();
    obj.putdata();
    obj.newfunction();
}

Output:-
count is =6
now count is =7
now count is =8


No comments:

Post a Comment

हिम्मत

 अंधेरे में एक करोड का हीरा गिर गया था, उसे ढूंढने के लिए पाँच रूपएं की मोमबत्ती ने सहयोग किया। अभी बताओ वह पाँच रूपएं की एक छोटी सी मोमबत्त...