Followers

Tuesday, 13 December 2016

CPP CRASH-COURSE PROGRAMS

/*------------------------------------------------------------------------------------------------------------------------------
TITLE :Accept two nos and display
PROGRAM NO:1
--------------------------------------------------------------------------------------------------------------------------------*/
#include<iostream>   
using namespace std;
int main()
{
    int a,b;
    cout<<"Enter a :\n";
    cin>>a;
   
    cout<<"Enter b :\n";
    cin>>b;
   
    cout<<" The nos are :"<<a<<" "<<b<<"\n";
return 0;
}



/*---------------------------------------------OUTPUT--------------------------------------------------------------------

ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ g++ 1.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out
Enter a :
32
Enter b :
23
 The nos are :32 23
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$


****************************************************************************/




 /*----------------------------------------------------------------------------------------------------------------------------
PROGRAM 2
TITLE    : Swap using third variable

-------------------------------------------------------------------------------------------------------------------------------*/

#include<iostream>
using namespace std;
int main()
{
    int a,b,c;
    cout<<"Enter a : ";
    cin>>a;
  
    cout<<"Enter b : ";
    cin>>b;
  
    c=a;
    a=b;
    b=c;
    cout<<" \nThe swap of a : "<<a;
    cout<<" \nThe swap of b : "<<b<<"\n";
return 0;
}


/*----------------------------------OUTPUT-------------------------------------------------------------------------------

ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ g++ 2.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out
Enter a : 67
Enter b : 43

The swap of a : 43
The swap of b : 67
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$

********************************************************************************/




/*------------------------------------------------------------------------------------------------------------------------------
PROGRAM 3
TITLE    : Check whether entered no is positive/ negative /zero...

-------------------------------------------------------------------------------------------------------------------------------*/
#include<iostream>
using namespace std;
int main()
{
    int a;
    cout<<"Enter number to check : ";
    cin>>a;
   
    if(a==0)
    {
        cout<<"\n Number is zero \n";
    }
    else if(a>0)
    {
        cout<<"\n Positive no \n";
    }
   
    else
    cout<<"\n Negative no  \n";
   
return 0;
}

/*----------------------------------OUTPUT-----------------------------------------------------------------------------

ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ g++ 3.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out
Enter number to check : 90
 Positive no

ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out
Enter number to check : 0
 Number is zero

ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out
Enter number to check : -32
 Negative no

ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$

*******************************************************************************/




/*-----------------------------------------------------------------------------------------------------------------------------
PROGRAM 4
TITLE    : Check whether the entered number is Even/Odd
-----------------------------------------------------------------------------------------------------------------------------*/

#include<iostream>
using namespace std;
int main()
{
    int a;
    cout<<"Enter the no to check : ";
    cin>>a;
   
    if(a%2==0)
    {
        cout<<"\n It is Even no\n";
    }
    else
        cout<<"\n It is Odd no\n";
return 0;
}

/*----------------------------------OUTPUT-------------------------------------------------------------------------------

ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ g++ 4.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out
Enter the no to check : 67

 It is Odd no
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$

*******************************************************************************/



/*-------------------------------------------------------------------------------------------------------------------------------
PROGRAM 5
TITLE    :Star Pattern 1
        *
       *  *
      *  *  *   
---------------------------------------------------------------------------------------------------------------------------------*/
#include<iostream>
using namespace std;
int main()
{
    int i,j,k;
   
   
    for(i=0;i<4;i++)
    {
        for(k=4;k>i;k--)
        {
            cout<<" ";
        }
      
        for(j=0;j<i;j++)
        {
            cout<<" * ";
        }
        cout<<"\n";
    }
return 0;
}

/*-------------------------OUTPUT----------------------------------------------------------------------------------------
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ g++ 5.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out
   
    *
   *  *
  *  *  *
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$

*********************************************************************************/





/*----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 6
TITLE    :Menu driven switch case program for
    1.Square
    2.cube
    3.Prime check
    4.Palindrome check
---------------------------------------------------------------------------------------------------------------------------------*/

#include<iostream>
using namespace std;
int main()
{
    int a,i,sq,cnt=0,cube,num,re=0,temp;
    cout<<"\n Enter the no \n";
    cin>>a;
   
    cout<<"1.Square\n";
    cout<<"2.cube\n";
    cout<<"3.Prime check\n";
    cout<<"4.Palindrome check\n";
   
   
    int ch;
    cout<<"\nEnter your choice : \n";
    cin>>ch;
   
    switch(ch)
    {
    case 1:
        sq=a*a;
        cout<<"\nSquare is :" <<sq<<"\n";
      
        break;  
    case 2:
        cube=a*a*a;
        cout<<"\nCube is :"<<cube<<"\n";
      
        break;  
    case 3:
        num=a;
        for(i=1;i<=a;i++)
        {
            if(num%i==0)
            {
                cnt++;
            }
          
        }
        if(cnt==2 && num!=2)
        {
            cout<<"\nIt is prime\n";
        }
        else
            cout<<"\n not  prime\n";
   
        break;
   
    case 4:
        temp=a;
        while(temp!=0)
        {
            re=re*10;
            re=re+(temp%10);
            temp=temp/10;
        }
        if(a==re)
      
        {
            cout<<"\nPalindrome\n";
        }
      
        else
            cout<<"\n not palindrome\n";
        break;
    default:
        cout<<"Exit\n";
        break;  
    }
return 0;  
}

/*--------------------------OUTPUT------------------------------------------------------------------------------------
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ g++ 6.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out

 Enter the no
15
1.Square
2.cube
3.Prime check
4.Palindrome check

Enter your choice :
1

Square is :225
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out

 Enter the no
3
1.Square
2.cube
3.Prime check
4.Palindrome check

Enter your choice :
2

Cube is :27
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out

 Enter the no
3
1.Square
2.cube
3.Prime check
4.Palindrome check

Enter your choice :
3    

It is prime
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$ ./a.out

 Enter the no
123321
1.Square
2.cube
3.Prime check
4.Palindrome check

Enter your choice :
4

Palindrome
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/10-12-16$
********************************************************************************/

/*--------------------------------------------------------------------------------------------------
7] To study single level inheritance
----------------------------------------------------------------------------------------------------*/
#include<iostream>
using namespace std;

class stud
{

    public:
        int rno,m1,m2,m3;
    char nm[20];
        void getStud()
        {
            cout<<"\n\t Enter roll number : ";
            cin>>rno;
           
            cout<<"\n\t Enter name : ";
            cin>>nm;
           
            cout<<"\n\t Enter maths 1 marks : ";
            cin>>m1;
           
            cout<<"\n\t Enter maths 2 marks : ";
            cin>>m2;
           
            cout<<"\n\t Enter maths 3 marks : ";
            cin>>m3;
        }
       
       
        void dispStud()
        {
            cout<<"\n\t roll number : "<<rno;
           
            cout<<"\n\t name : "<<nm;
           
            cout<<"\n\t maths 1 marks : "<<m1;
           
            cout<<"\n\t maths 2 marks : "<<m2;
           
            cout<<"\n\t maths 3 marks : "<<m3;
        }
};




class result : stud                    //single level inheritence
{
    int tot;
    float per;
    public:
        void getResult()
        {
            getStud();
            tot = m1+m2+m3;
            per=tot/3;
        }
       
        void dispResult()
        {
            dispStud();
            cout<<"\n\t Total is : "<<tot;
            cout<<"\n\t Percentage : "<<per;
        }
};

int main()
{
    result r;
    r.getResult();
    r.dispResult();
}

/*------------------------------------------------------OUTPUT---------------------------------------------------------
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$ g++ 9.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$ ./a.out

     Enter roll number : 1

     Enter name : SonaliDhurjad

     Enter maths 1 marks : 23

     Enter maths 2 marks : 12

     Enter maths 3 marks : 32

     roll number : 1
     name : SonaliDhurjad
     maths 1 marks : 23
     maths 2 marks : 12
     maths 3 marks : 32
     Total is : 67
     Percentage : 22ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$

-----------------------------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------
18] Inheritance example
--------------------------------------------------------------------------------------------------*/
#include<iostream>
using namespace std;

class second; //forward declaration
class first
{
    private:
        int no;
    public:
        void get();
        friend void check(first,second);
};
void first::get()  
{
    cout<<"\n\t Enter no. : ";
    cin>>no;
}


class second
{
    private:
        int no;
    public:
        void get();
        friend void check(first,second);
};
void second::get()
{
    cout<<"\n\t Enter no. : ";
    cin>>no;
}


void check (first f, second s)
{
    if(f.no>s.no)
    {
        cout<<"First number is greater : "<<f.no<<"\n";
    }
    else
    {
        cout<<"Second numbner is greater : "<<s.no<<"\n";
    }
}
int main()
{
    first f;
    second s;
    f.get();
    s.get();
    check(f,s);
}

/*-----------------------------------------------------------OUTPUT-------------------------------------------------
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$ g++ 8.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$ ./a.out

     Enter no. : 12

     Enter no. : 4
First number is greater : 12
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$

---------------------------------------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------------------
19]  Conceptof function using function within a class
-------------------------------------------------------------------------------------------------*/
#include<iostream>//function using function within a class
using namespace std;

class abc
{
   
    public:
    int a;
   
    void get(int x)
    {
        a=x;
        disp();
    }
   
    void disp()
    {
        cout<<" a is "<<a<<"\n";
    }
};



int main()
{
   
    abc a;
    a.get(90);
   
    cout<<a.a<<"\n";
return 0;   
}
/*-------------------------------------------------OUTPUT-------------------------------------------------
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$ g++ 7.cpp
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$ ./a.out
 a is 90
90
ubantu@ubantu-HP-630-Notebook-PC:~/Desktop/crashcourse/13-10-16$

--------------------------------------------------------------------------------------------------------*/

No comments: