Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

Department of Computer Science

INDEX
S. No. Activity Name Page No. Remark

1. WAP in C++ to find the given number is even or odd. 1

2. WAP in C++ to find the greatest no. between three number. 2-3

3. WAP in C++ to find the area and parameter of a rectangle. 4

4. WAP in C++ to swap the value using only two Variable. 5

5. WAP in C++ to find the given year is leap or not using logical operator. 6-7

6. WAP in C++ to find the given number is prime or not. 8-9

7. WAP in C++ to find the given number Armstrong or not. 10-11

8. WAP in C++ to find the given number id palindrome or not. 12-13

9. WAP in C++ to display Fibonacci series minimum 10 digit. 14-15

10. WAP in C++ to enter and display 10 Element in one dimensional 16-17
array.
11. WAP in C++ to find the sum of 2 Matrix. 18-19

12. WAP in C++ to find the multiplication of 2 Matrix. 20-22

13. WAP in C++ to perform all arithmetic operation using switch 23-25
statement.
14. WAP in C++ to enter and display a student record using structure 26-27
required field are: student id student name, student class and
percentage.
15. WAP in C++ to insert and display a book record using union required 28-29
field are: book id, book name, author name and prize.
16. WAP in C++ to find factorial of any given number using recursion. 30

17. WAP in C++ to display function overloading. 31

18. WAP in C++ using inheritance. 32

19. WAP in C++ using multiple Constructor. 33-34

20. WAP in C++ using friend function. 35-36


1. WAP in C++ to find the given number is even or odd.

#include<iostream>

using namespace std;

int main()

int num;

cout<<"Enter a Number: ";

cin>>num;

if(num%2==0)

cout<<"\nIt is an Even Number.";

else

cout<<"\nIt is an Odd Number.";

cout<<endl;

cout<<"\n This output is produced by “Aniket Pawar;

return 0;

Output:--
PS C :\ Users\HP\Desktop\c++> g++ .\even_odd.cpp

PS C:\Users\HP\Desktop\c++ -\a.exe

Enter a Number : 5

It is an Odd Number .

This output is produced by Aniket Pawar

PS C : \Users\HP\Desktop\c++
2. WAP in C++ to find the greatest no. between three number.

#include<iostream>

using namespace std;

int main()

int numOne, numTwo, numThree, larg;

cout<<"Enter the Three Numbers: ";

cin>>numOne>>numTwo>>numThree;

if(numOne>numTwo)

if(numTwo>numThree)

larg = numOne;

else

if(numThree>numOne)

larg = numThree;

else

larg = numOne;

else

if(numTwo>numThree)

larg = numTwo;

else

larg = numThree;

} Pg:2
cout<<"\nLargest Number = "<<larg;

cout<<endl;

cout<<" Output is produced by Aniket Pawar ";

return 0;

Output:--
PS C : \Users\HP\Desktop\c++> g++ .\larg_three_no.cpp

PS C : \User\HP\Desktop\c++> .\a.exe

Enter the Three Numbers: 9 8 5

Largest Number = 9

This output is produced by Aniket Pawar

PS C:\Users\HP\Desktop\c++>

3. WAP in C++ to find the area and parameter of a rectangle


#include<iostream>

using namespace std;

int main()

float len, bre, area;

cout<<"Enter Length of Rectangle: ";

cin>>len;

cout<<"Enter Breadth of Rectangle: ";

cin>>bre;

area = len*bre;

cout<<"\nArea = "<<area;

cout<<endl;

cout<<" Output is produced by Aniket Pawar";

return 0;

Output:--

PS C:\User\HP\Desktop\c++> g++ .\rec_peri.cpp


PS C:\HP\Desktop\c++> .\a.exe

Enter Length of Rectangle: 6

Enter Breadth of Rectangle: 4

Area = 24

This output is produced by Aniket Pawar

PC C: \Users\HP\Desktop\c++>

4. WAP in C++ to swap the value using only two Variable


#include <iostream>
using namespace std;

int main()

cout << "\n\n Swap two numbers without using third variable:\n";

cout << "---------------------------------------------------\n";

int num1, num2, temp;

cout << " Input 1st number : ";

cin >> num1 ;

cout << " Input 2nd number : ";

cin >> num2;

num2=num2+num1;

num1=num2-num1;

num2=num2-num1;

cout << " After swapping the 1st number is : "<< num1 <<"\n" ;

cout << " After swapping the 2nd number is : "<< num2 <<"\n\n" ;

cout<<"\ Output is produced by Aniket Pawar ";

Output:--

PS C: \Users\HP\Desktop\c++ g++ .\two_variable.cpp


PS C: \Users\HP\Desktop\c++> .\a.exe

Swap two numbers without using third variable:

---------------------------------------------------------------------------

Inout 1st number : 41

Input 2nd number : 20

After swapping the 1st number is : 20

After swapping the 2nd number is : 41

This output is produced by Aniket Pawar

PS C: \Users\HP\Desktop\c++>

5. WAP in C++ to find the given year is leap or not using logical operator.
#include<iostream>

using namespace std;

int main()

    int y;

    cout<<"Enter year   :   ";

    cin>>y;

    if(y%4==0)

    {

        if(y%100==0)

        {

            if(y%400==0)

                cout<<"leap year";

            else

                cout<<"not leap year";

        }

        else

            cout<<"leap year";

    }   

    else

        cout<<"not leap year";

        cout<<"\ This output is produced by Aniket Pawar ";

    return 0;

Output:--
PS C: \Users\HP\Desktop\c++> g++ .\leap-year .cpp

PS C: \Users\HP\Desktop\c++ . \a.exe

Enter year : 2011

Not leap year

This output is produced by Aniket Pawar

PS C: \Users\HP\Desktop\c++>

6. WAP in C++ to find the given number is prime or not.


#include <iostream>  

using namespace std;  
int main()  

{  

  int n, i, m=0, flag=0;  

  cout << "Enter the Number to check Prime: ";  

  cin >> n;  

  m=n/2;  

  for(i = 2; i <= m; i++)  

  {  

      if(n % i == 0)  

      {  

          cout<<"Number is not Prime."<<endl;  

          flag=1;  

          break;  

      }  

  }  

  if (flag==0)  

      cout << "Number is Prime."<<endl;

      cout<<"\n This output is produced by Aniket Pawar ";  

  return 0;  

}  

Pg:8

Output:--

PS C;\Users\HP\Desktop\c++> g++ .\prime.cpp


PS C: \Users\HP\Desktop\c++> .\a.exe

Enter the number to check Prime: 62

Number is not Prime.

This output is produced by Aniket Pawar

PS c: \User\HP\Desktop\c++>

7. WAP in C++ to find the given number Armstrong or not.


#include <iostream>  

using namespace std;  

int main()  

{  

int n,r,sum=0,temp;    

cout<<"Enter the Number=  ";    

cin>>n;    

temp=n;    

while(n>0)    

{    

r=n%10;    

sum=sum+(r*r*r);    

n=n/10;    

}    

if(temp==sum)    

cout<<"Armstrong Number."<<endl;    

else    

cout<<"Not Armstrong Number."<<endl; 

cout<<"\ This output is produced by Aniket Pawar ";  

return 0;  

}  

Pg:10

Output:--

PS C:\Users\HP\Desktop\c++> G++ . \armstromg.cpp


PS C:\Users\HP\Desktop\c++ . \a.exe

Enter the Number= 11

Not Armstrong Number.

This output is produced by Aniket Pawar

PS C:\User\HP\Desktop\c++>

8. WAP in C++ to find the given number id palindrome or not.


#include <iostream>

using namespace std;
int main()

     int n, num, digit, rev = 0;

     cout << "Enter a positive number: ";

     cin >> num;

     n = num;

     do

     {

         digit = num % 10;

         rev = (rev * 10) + digit;

         num = num / 10;

     } while (num != 0);

     cout << " The reverse of the number is: " << rev << endl;

     if (n == rev)

         cout << " The number is a palindrome.";

     else

         cout << " The number is not a palindrome.";

         cout<<"\n This output is produced by Aniket Pawar";

Pg:12

    return 0;

}
Output:--
PS C : \Users\HP\Desktop\c++> g++ .\ palindrome.cpp

PS C :\Users\HP\desktop\c++> .\a.exe

Enter a positive number : 6

The reverse of the number is : 6

The number is a palindrome.

This output is produced by Aniket Pawar

PS C:\Users\HP\Dsktop\c++

9. WAP in C++ to display Fibonacci series minimum 10 digit.


#include <iostream>
using namespace std;

int main() {

    int n, t1 = 0, t2 = 1, nextTerm = 0;

    cout << "Enter the number of terms: ";

    cin >> n;

    cout << "Fibonacci Series: ";

    for (int i = 1; i <= n; ++i) {

        // Prints the first two terms.

        if(i == 1) {

            cout << t1 << ", ";

            continue;

        }

        if(i == 2) {

            cout << t2 << ", ";

            continue;

        }

        nextTerm = t1 + t2;

        t1 = t2;

        t2 = nextTerm;

         Pg:14

        cout << nextTerm << ", ";

      
    }

    cout<<"\n This output is produced by Aniket Pawer ";

    return 0;

Output:--

PS C:\Users \HP\Desktop\c++> g++ .\fabino.cpp

PS C:\Users\HP\Desktop\c++> . \a.exe

Enter the number of term : 6

Fibonacci Series: 0, 1, 1, 2, 3, 5,

This output is produced by Aniket Pawer

PS C:\ Users\HP\Desktop\c
Pg:15

10. WAP in C++ to enter and display 10 Element in one dimensional array.
#include<iostream>

using namespace std;

int main()

    int arr[50], tot, i;

    cout<<"Enter the Size: ";

    cin>>tot;

    cout<<"Enter "<<tot<<" Numbers: ";

    for(i=0; i<tot; i++)

        cin>>arr[i];

    cout<<"\nArray with Index\tIts Value\n";

    for(i=0; i<tot; i++)

        cout<<"arr["<<i<<"]"<<"\t\t\t"<<arr[i]<<endl;

    cout<<endl;

    cout<<"\n This output is produced by Aniket Pawar ";

    return 0;

Output:--

PS C: \Users\HP\Desktop\c++> g++ . \one.dimen.cpp

PS C: \ Users\HP\Desktop\c++> . \a.exe

Enter the Size : 10

Enter 10 Numbers: 1 2 3 4 5 6 7 8 9
Array with Index Its Value

arr[0] 1

arr[1] 2

arr[2] 5

arr[3] 4

arr[4] 5

arr[5] 4

arr[6] 6

arr[7] 4

arr[8] 6

arr[9] 9

This output is produced by Aniket Pawar

PS C:\User\HP\Desktop\c++>

11. WAP in C++ to find the sum of 2 Matrix.


#include<iostream>

using namespace std;

int main()

        int m1[3][3], m2[3][3], i, j, m3[3][3];


        cout<<"\n Enter First Matrix Elements : \n";

        for(i=0; i<3; i++)

    {

                for(j=0; j<3; j++)

        {

                        cout<<" ";

                        cin>>m1[i][j];

        }

    }

        cout<<"\n Enter Second Matrix Elements : \n";

        for(i=0; i<3; i++)

    {

                for(j=0; j<3; j++)

        {

                        cout<<" ";

                        cin>>m2[i][j];

        }

    }

        cout<<"\n Sum of Two Matrices : \n\n";

        for(i=0; i<3; i++)

    {

                for(j=0; j<3; j++) Pg:18

        {

                        m3[i][j]=m1[i][j]+m2[i][j];

        }

    }

        for(i=0; i<3; i++)

    {

                cout<<" ";
                for(j=0; j<3; j++)

        {

                        cout<<m3[i][j]<<" ";

        }

                cout<<"\n";

    }

        cout<<"\n This output is produced by Aniket Pawar";

        return 0;

Output:--
PS C: \Users\HP\Desktop\c++ .\a.exe

Sum of Two Matrices : PS C: \Users\HP\Desktop\c++> g++ .\


sum_matrix.cpp

Enter First Matrix Elements :

26 5
3 2 1

2 5 8

Enter Second Matrix Elements :

1 5 8
2 6 9

3 10 12

5 7 9

4 11 17

This output is produced by Aniket Pawar


PS C: \Users\HP\Desktop\c++>

12. WAP in C++ to find the multiplication of 2 Matrix.


#include <iostream>  

using namespace std;  

int main()  

int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;    

cout<<"enter the number of row=";    


cin>>r;    

cout<<"enter the number of column=";    

cin>>c;    

cout<<"enter the first matrix element=\n";    

for(i=0;i<r;i++)    

{  

for(j=0;j<c;j++)    

{  

cin>>a[i][j];  

}  

}  

cout<<"enter the second matrix element=\n";    

for(i=0;i<r;i++)    

{  

for(j=0;j<c;j++)    

{  

cin>>b[i][j];    

}  

}  

cout<<"multiply of the matrix=\n";    

for(i=0;i<r;i++)    

{   Pg:20

for(j=0;j<c;j++)    

{  

mul[i][j]=0;    

for(k=0;k<c;k++)    

{  

mul[i][j]+=a[i][k]*b[k][j];    

}  
}  

}  

//for printing result    

for(i=0;i<r;i++)    

{  

for(j=0;j<c;j++)    

{  

cout<<mul[i][j]<<" ";    

}  

cout<<"\n";    

 cout<<"\n This output is produced by Abhishek kumar";

return 0;  

Output:--
PS C: \Users\HP\Desktop\c++> g++ .|multi.cpp

PS C: \Users\HP\Desktop\c++> .\a.exe

enter the number of row=5

enter the number of column=3

enter the first matrix element=

s
enter the second matrix element=

multiply of the matrix=

-1260678616 6000690048 -413887872


-1179547762 668449293 -1347743636
-238572746 -520665520 1968232428
-169983528 -2049699520 -1156285784
-1185019516 1512109632 930914640

This output is produced by Aniket Pawar


PS c: \Users\HP\Desktop\c++

13. WAP in C++ to perform all arithmetic operation using switch statement.

#include<iostream>

using namespace std;

int main() {

    int x, y, res;

    int ch;
    cout << "Enter 1 For Addition :";

    cout << "\nEnter 2 For Subtraction :";

    cout << "\nEnter 3 For Multiplication :";

    cout << "\nEnter 4 For Division :";

    cout << "\nEnter 5 For Modulus :"<<endl;

    cin >> ch;

    switch (ch) {

        case 1:

    {

            cout << "Enter Two Numbers :";

            cin >> x >> y;

            res = x + y;

            cout << "\nResult is :" << res;

            break;

    }

        case 2: Pg:23

    {

            cout << "Enter Two Numbers :";

            cin >> x >> y;

            res = x - y;

            cout << "Result is :" << res;

            break;

    }

        case 3:
    {

            cout << "Enter Two Numbers :";

            cin >> x >> y;

            res = x * y;

            cout << "Result is :" << res;

            break;

    }

        case 4:

    {

            cout << "Enter Two Numbers :";

            cin >> x >> y;

            res = x / y;

            cout << "Result is :" << res;

            break;

    } Pg:24

        case 5:

    {

            cout << "Enter Two Numbers :";

            cin >> x >> y;

            res = x % y;

            cout << "Result is :" << res;

            break;

    }
  }

cout<<"\n This output is produced by Aniket Pawar";

    return 0;

Output:--
PS C: \Users\HP\Desktop\c++> g++ .\all.arith.cpp

PS C: \Users\HP\Desktop\c++> .\a.exe

Enter 1 For Addition :

Enter 2 For Subtraction :

Enter 3 for Multiplication :

Enter 4 For Division :

Enter 5 For Modulus :

Enter Two Numbers : 55 25

Result is : 30

This output is produced by Aniket Pawar

PS C: \Users\HP\desktop\c++>

14. WAP in C++ to enter and display a student record using structure required field are:
student id student name, student class and percentage.

#include <iostream>

using namespace std;

struct student

    char name[50];

    int roll;
    float marks;

} s[10];

int main()

    cout << "Enter information of students: " << endl;

    // storing information

    for(int i = 0; i < 10; ++i)

  {

        s[i].roll = i+1;

        cout << "For roll number" << s[i].roll << "," << endl;

        cout << "Enter name: ";

        cin >> s[i].name;

        cout << "Enter marks: ";

        cin >> s[i].marks;

        cout << endl; Pg:26

  }

    cout << "Displaying Information: " << endl;

    // Displaying information

    for(int i = 0; i < 10; ++i)

  {

        cout << "\nRoll number: " << i+1 << endl;

        cout << "Name: " << s[i].name << endl;

        cout << "Marks: " << s[i].marks << endl;


  }

        cout<<"\n This output is produced by Aniket Pawar ";  

    return 0;

Output:--
Enter name: Enter marks:

Displaying Information:

Roll number: 1

Name: Aniket Pawar

Marks: 0

Roll number : 2

Name:
Marks: 0

Roll number: 3

Name:

Marks: 0

Roll number: 4

Name:

Marks: 0

Roll number: 5

Name:

Marks: 0

This output is produced by Aniket Pawar

PC c:\Users\HP\Desktop\c++>

Pg:27

15. WAP in C++ to insert and display a book record using union required field are: book id,
book name, author name and prize.

#include <iostream>

using namespace std;

struct Book {

    char name[100];

    int price;

    int ISBN;

}; 

int main() {
    Book b;

    cout << "Enter name of book\n";

    cin.getline(b.name, 100);

    cout << "Enter price of employee\n";

    cin >> b.price;

    cout << "Enter ISBN code\n";

    cin >> b.ISBN;

    cout<<"\nAuthor Name - Peter";

   

    // Printing Book details

    cout << "\n*** Book Details ***" << endl;

    cout << "Name : " << b.name << endl;

 cout << "Price : " << b.price << endl;

    cout << "ISBN Code : " << b.ISBN;

     cout<<"\n This output is produced by Aniket Pawar";

   

    return 0;

Pg:28

Output:--
PS C: \Users\HP\Desktop\c++> g++ .\book.cpp

PS C: \ Users\HP\Desktop\c++> .\a.exe

Biology

Enter price of employee

500

Enter ISBN code

2586

Author Name - Berlin


***Book Details ***

Name : biology

Price : 500

ISBN code : 2586

This output is produced by Aniket Pawar

PS C:\Users\HP\Desktop\c++

16. WAP in C++ to find factorial of any given number using recursion.

#include<iostream>

using namespace std;

int factorial(int n);

int main() {

  int n;

  cout << "Enter a positive integer: ";

  cin >> n;

  cout << "Factorial of " << n << " = " << factorial(n);
  return 0;

int factorial(int n) {

  if(n > 1)

    return n * factorial(n - 1);

  else

    return 1;

    cout<<"\n This output is produced by Aniket Pawar ";

Output:--
PS C: \Users\HP\Desktop\c++> g++ .\recusion.cpp

PS C: \ Users\HP\Desktop\c++> .\a.exe

Enter a positive integer: 8

Factorial of 8 = 40320

This output is produced by Aniket Pawar

PS C: \Users\HP\Desktop\c++>

Pg:30

17. WAP in C++ to display function overloading.


#include <iostream>    

using namespace std;    

class Cal {    

    public:    

static int add(int a,int b){      

        return a + b;      

  }   

static int add(int a, int b, int c)      


  {   

        return a + b + c;      

  }   

};    

int main(void) {    

    Cal C;                                                    .  

    cout<<C.add(10, 20)<<endl;      

    cout<<C.add(12, 20, 23);

    cout<<"\n This output is produced by Aniket Pawar";    

   return 0;    

}  

Output:--
PS C: \Users\HP\Desktop\c++> g++ .\function_over.cpp

PS C: \ Users\HP\Desktop\c++> .\a.exe

30

60

This output is produced by Aniket Pawar

PS c: \Users\HP\Desktop\c++

18. WAP in C++ using inheritance.


#include <iostream>  
using namespace std;  
 class Account {  
   public:  
   float salary = 60000;  
 };  
   class Programmer: public Account {  
   public:  
   float bonus = 5000;    
   };      
int main(void) {  
     Programmer p1;  
     cout<<"Salary: "<<p1.salary<<endl;    
     cout<<"Bonus: "<<p1.bonus<<endl;
     cout<<"\n This output is produced by Abhishek kumar ";    
    return 0;  

Output:-
PS C: \Users\HP\Desktop\c++> g++ .\inheritance.cpp

PS C: \ Users\HP\Desktop\c++> .\a.exe

Salary: 75000

Bonus: 7500

This output is produced by Aniket Pawar

PS c: \Users\HP\Desktop\c++>

19. WAP in C++ using multiple Constructor.


#include <iostream>

using namespace std;

class Room {

   private:

    double length;

    double breadth;

   public:
    // 1. Constructor with no arguments

    Room() {

        length = 6.9;

        breadth = 4.2;

  }

    // 2. Constructor with two arguments

    Room(double l, double b) {

        length = l;

        breadth = b;

  }

    // 3. Constructor with one argument

    Room(double len) {

        length = len;

        breadth = 7.2;

  }

    double calculateArea() {

        return length * breadth;

  }

}; Pg:33

int main() {

    Room room1, room2(8.2, 6.6), room3(8.2);

    cout << "When no argument is passed: " << endl;

    cout << "Area of room = " << room1.calculateArea() << endl;

    cout << "\nWhen (8.2, 6.6) is passed." << endl;

    cout << "Area of room = " << room2.calculateArea() << endl;

    cout << "\nWhen breadth is fixed to 7.2 and (8.2) is passed:" << endl;

    cout << "Area of room = " << room3.calculateArea() << endl;

    cout<<"\n This output is produced by Aniket Pawar ";

    return 0;
}

Output:--
PS C: \Users\HP\Desktop\c++> g++ .\construc.cpp

PS C: \ Users\HP\Desktop\c++> .\a.exe

When no argument is passed:

Area of room = 28.98

When (8.2, 6.6) is passed.

Area of room = 50.6

When breadth is fixed to 7.2 and (8.2) is passed :

Area of room = 66.24

This output is produced by Aniket Pawar

PS c: \Users\HP\Desktop\c++>

20. WAP in C++ using friend function.

#include <iostream>

using namespace std;

class Distance {

    private:

        int meter;

        // friend function

        friend int addFive(Distance);


    public:

        Distance() : meter(0) {}    

};

// friend function definition

int addFive(Distance d) {

    //accessing private members from the friend function

    d.meter += 5;

    return d.meter;

int main() {

    Distance D;

    cout << "Distance: " << addFive(D);

    cout<<"\n This output is produced by Aniket pawar";

    return 0;

Pg:35

Output:--
PS C: \Users\HP\Desktop\c++> g++ .\friend_function.cpp

PS C: \ Users\HP\Desktop\c++> .\a.exe

Distance: 8

This output is produced by Aniket Pawar

PS c: \Users\HP\Desktop\c++>
Pg:36

You might also like