Followers

Sunday, 12 February 2017

CPP Programs

/*__________________________________________________________________
1]Program for swapping
___________________________________________________________________*/
#include<iostream>
using namespace std;

int main()
{
    int a, b, temp;

    cout<<"\nEnter first number: ";
    cin>>a;

    cout<<"\nEnter second number: ";
    cin>>b;

    temp=a;
    a=b;
    b=temp;

    cout<<"\nFirst number on swapping: "<<a;
    cout<<"\nSecond number on swapping: "<<b<<"\n";
}

/******************************OUTPUT****************************
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$ g++ 1.cpp
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$ ./a.out

Enter first number: 45

Enter second number: 98

First number on swapping: 98
Second number on swapping: 45
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$

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

/********************************************************************
2] Program to check if the number is positive, negative or 0

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

#include<iostream>
using namespace std;

int main()
{

    int n;
   
    cout<<"\nEnter a number: ";
    cin>>n;

    if(n>0)
    {
        cout<<"Number is positive\n";
    }

    else if(n<0)
    {
        cout<<"Number is negative\n";

    }

    else
    {
        cout<<"\nNumber is zero\n";
    }
}   


/*****************************OUTPUT***************************************
ubuntu@ubuntu-HP-630-Notebook-PC:~$ cd Desktop/codes/
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$ g++ 2.cpp
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$ ./a.out

Enter a number: -67
Number is negative
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$

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

/******************************************************************
3]String Operations
*******************************************************************/

#include<iostream>
#include<string.h>

using namespace std;

int main()
{

    char s1[]="Pr";
    char s2[]="Jadhav";

    cout<<"\nString 1: "<<s1;
    cout<<"\nString 2: "<<s2;



    if(strcmp(s1,s2)==0)
    {
        cout<<"\nBoth the strings are same\n";
    }
   
    else
    {
        cout<<"\nStrings are different\n";
    }
   

    cout<<"\nString length: "<<strlen(s1);

   
   

    cout<<"\nConactenated string: "<<strcat(s1, s2);



    strcpy(s1,s2);
    cout<<"\nString on copying: "<<s1<<"\n";

   
}

/*************************************OUTPUT*****************************
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$ g++ 3.cpp
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$ ./a.out

String 1: Pr
String 2: Jadhav
Strings are different

String length: 2
Conactenated string: PrJadhav
String on copying: adhav
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$

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

/***************************************************************
4]Simple accept & display
******************************************************************/
#include<iostream>
using namespace std;

class abc
{
   
        public:
       
        int a;
        void get(int x);
        void display();       
       
};

        void abc:: get(int x)
        {
            a=x;
           
        }

        void abc:: display()
        {
            cout<<"a is: "<<a<<"\n";
        }

int main()
{
    abc a;
    a.get(90);
    a.display();
   
    cout<<a.a<<"\n";

    return 0;
}


/*******************************OUTPUT*************************************
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$ g++ 4.cpp
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$ ./a.out
a is: 90
90
ubuntu@ubuntu-HP-630-Notebook-PC:~/Desktop/codes$

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

No comments: