Thursday 27 February 2020

C++ Programs for class 12



C++

Class - 12 
Lab Manual



1.         Write a simple program that prints a string on the screen.

 

Solution

 

#include<iostream.h>
#include<conio.h>
main ()
{
            cout << "Welcome to C++ lab Exercise";
   getch();
}

2.         Write a program to find the average of two numbers.

 

Solution


#include<iostream.h>
#include<conio.h>
main ()
   {
                        float num1, num2, sum, avg;
                        cout  << "Enter two numbers :";
                        cin >> num1;
                        cin >> num2;
                        sum = num1+num2;
                        avg=sum/2;
                        cout << "Sum = " << sum << "\n";
                        cout << "Average = " << avg << "\n";
      getch();
    }

3.         Write a program to get the Name, Age, address, phone number from the user and display them by using the class function.

 

Solution

                            
#include<iostream.h>
#include<conio.h>

class sample
{
char name[30];
int age;
char add[30];
int phone;
public :
            void getdata(void);
            void display(void);
};

void sample :: getdata(void)
{
cout << " Enter your Name : " ;
cin >> name;
cout << " Enter your Age :";
cin >> age;
cout << " Enter your Address :";
cin >> add;
cout << " Enter your Phone Number :";
cin >> phone;
}

void sample :: display(void)
{
            cout << "\n Name : " << name;
            cout << "\n Age : " << age;
            cout << "\n Address : " << add;
            cout << "\n Phone Number : " << phone;
}

void main()
                        {
                                    sample s;
                                    s.getdata();
                                    s.display();
         getch();
                        }

4.         What is the output of the following code?

Solution


#include<iostream.h>
#include<conio.h>
int a=10;
void main()
{
  int a=15;
  cout<<a<<::a<<endl;
  ::a=20;
  cout<<a<<::a;
  getch();
  }

5.         Write a program to get the input from the user and display the out what he\she has entered.

 

Solution


 #include<iostream.h>
#include<conio.h>
void main(void)
{
char a[999];
cout << "Type something :" << endl;
cin >> a;
cout << "You have typed :" << a << endl;
getch();
}

6.         What is the output of the following code?

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
 int a=5,x;
 int b=6;
 x=++a+b++;
 cout<<x<<a<<b;
 getch();
}

7.         Write a program to convert the alphabet from lower case to uppercase.

 

Solution

 

#include<iostream.h>

#include<ctype.h>

#include<conio.h>

void main(void)

{

 


char mydata = 'a';

char s= toupper(mydata);

cout <<" My data in ASCII is : " << s << "." << endl;

getch();

}


8.         What is the output of the following code?

Solution


#include<iostream.h>
#include<conio.h>
class c
{
 volatile x;
 public:
        void show()
        {
         x=9.899;
         cout<<x;
         }
   };

void main()
{
  c c1;
  c1.show();
  getch();
}

9.         Write a program to input a text string and count the length of it using get() and put().

Solution


            #include<iostream.h>
            #include<conio.h>
main()
            {
            int count = 0;
            char c;
            cout<< "Input text\n";
            cin.get(c);
            while(c != '\n')
            {
            cout.put(c);
            count ++;
            cin.get(c);
}
cout <<"\nNumber of characters = "<< count << "\n";
getch( );
}

 

10.      Write a program to multiply numbers by taking assigned default values (First assign default values 3 and 10 and then substitute 3 with 4 and 10 and then substitute 4&10 with 4 and 5).

Solution


#include<iostream.h>
#include<conio.h>
int multiply ( int x=3, int y=10);
void main(void)
{
int iresult;
iresult = multiply();
cout << "\n  When using multiply (      ) : iresult="<< iresult << endl;
iresult = multiply( 4    );
cout << "\n  When using multiply (4     ) : iresult="<< iresult << endl;
iresult = multiply( 4, 5 );
cout << "\n  When using multiply (4  5  ) : iresult="<< iresult << endl;
           getch( );
           }
int multiply ( int x, int y)
{
return x*y;
}

11.     Find the error in the program and explain it.

Solution


#include <iostream.h>
void main()
{
for (int i=1; i<3; i++)
{
int j=3;
cout << i;
}
cout<<i;
cout<<j;
}

When you execute the program you get an error j is not declared.  But you have declared j. The problem is that you have declared the variable j with in the for( ) loop. Thus the scope is only in between the braces({}) of the for () loop. Declare the j variable outside the for() loop as you have declared the i variable.

12.     Write a program to display two variables by declaring them as local and global variables and show the result has follows.

Solution


#include<iostream.h>
#include<conio.h>
int a=200;
void main (void)
{
cout<<"\n Results :";
cout<<"\n ----------";
int a =100;
cout<<"\n Local variable is  :"<<a;

cout<<"\n global variable is  :"<< a<<endl;

}

13.     Find if any error exists and correct the parameter list.

Solution


Int showit ( int s);
Float showit ( int s);

Since there cannot be two identical list of Parameters even if the returned values is different. So we can change the parameter list as follows.

Int showit ( int s, int t);
Int showit ( int s);

14.     Write a program to find the inverse of a number.

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
float a;
char b;
do
{
cout<<"Enter a number:";
cin>>a;
if(a= =0)
break;
cout<<"Inverse of the number is :"<<1/a;
cin>>b;
}while(b!='n');
getch( );
}

15.     Write a program to find the square of the numbers less than 100.

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
int a;
char b='y';
do
{
cout<<"Enter a number :";
cin>>a;
if(a>100)
{
cout<<"The number is greater than 100, enter another number :"<<endl;
continue;
}
cout<<"The square of the numbers is :"<<a*a<<endl;
cout<<"Do you want to enter another (y/n)";
cin>>b;
}while(b!='n');
getch( );
}

16.     Find the output of the following statements.

Solution

                                                   
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
for(i=0;i<10;i++);
cout<<i;
getch();
}

17.     Find the output of the following program

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
            int a=12,b;
b=++a;
cout<<"a ="<<a<<"b="<<b<<endl;
                        getch( );
            }

18.     Write a program to get the input from the user and check if the input value is "y" or "n", if it is other than these values return a message " Invalid option". If it is right choice return a message "Right choice".

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
char a;
cout<<"Enter y or n";
cin>>a;
if(a = = 'y' | | a = = 'n' )
{
cout<<"Right choice";
}
else
{
cout<<"Invalid choice";
}
getch();
}



19.     Write a program to convert temperature from Fahrenheit to Celsius.

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
float a;
cout<<"Enter the temperature in Fahrenheit:";
cin >>a;
float b=(a-32)*5/9;
cout<<"The equivalent temperature in Celsius is:"<<b<<endl;
getch( );
}

20.     Write a program to input two numbers and find the largest of them using nesting member function.

Solution


#include<iostream.h>
#include<conio.h>
class set
{
int m,n;
 public:
void input(void);
void display(void);
int largest(void);
            };
            int set :: largest(void)
{
if(m>=n)
            return (m);
else
            return(n);
}
void set :: input(void)
{
            cout<<"Input values of m and n" << "\n";
            cin >> m >>n;
}
void set :: display(void)
{
            cout<<"Largest value ="<< largest( ) << "\n";
}
main( )
{
            set A;
A.input( );
A.display( );
getch( );
}

21.     Write a program to calculate the square of the first ten natural numbers.

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
int a;
for(a=1;a<=10;a++)
{
cout<<a*a<<" ";
}
getch( );
}

22.     Write a program using function to add two numbers.

Solution


#include<iostream.h>
#include<conio.h>
int add(int,int);
void main()
{
int a,b,c;
cout<<"Enter two numbers:"<<endl;
cin>>b;
cin>>c;
a=add(b,c);
cout<<"The sum of the two numbers is :" <<a<<endl;
getch();
}
int add(int x, int y)
{
return x+y;
}

23.     Write a program to accept two numbers and find the greatest number among them.

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
cout<<"Input the first number:";
cin>>a;
cout<<"Input the second number:";
cin >>b;
if(a>b)
{
cout<<"a is greater than b" <<endl;
}
else
{
cout<<"b is greater than a" <<endl;
}
getch();
}

24.     Write a program to find whether the input number is an even or odd number.

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
int a;
cout<<"Enter a number:";
cin>>a;
if((a!=0)&&((a%2) = = 0))
{
cout<<"Even number";
}
else
{
cout<<"Odd number or the number is Zero";
}
getch();
}

25.     Write a program to accept strings into a two-dimensional array and display them
      (User can enter five strings).

Solution


#include<iostream.h>
#include<conio.h>
void main()
{
char name[5][21];
int i;
for(i=0; i<5; i++)
{
cout<<"Enter Name "<<(i+1)<<" : ";
cin>>name[i];
}
for (i=0; i<5; i++)
{
cout<<"Name"<<(i+1)<<" is : "<<name[i]<<endl;
}
getch();
}

26.     Write a program that calculates the sum of two or three numbers.
(The first two numbers are 25 35 and the second set of numbers 12 13 45)

Solution


#include<iostream.h>
#include<conio.h>
int add(int, int);
int add(int, int, int);
void main()
{
cout<<"Sum of two Numbers is   : " << add(25,35)<<endl;
cout<<"Sum of three Numbers is : " << add(12,13,45)<<endl;
getch();
}
int add(int a, int b)
{
return a+b;
}
int add( int a, int b, int c)
{
return a+b+c;
}

27.     Write a program to print hi followed by the name of the user entered at the command line.

Solution


#include<iostream.h>
#include<conio.h>
void main ( int argc, char *argv[])
{
if (argc!=2)
{
cout<<" You have not typed your name"<<endl;
}
else
{
cout<<" Hi "<<argv[1]<<endl;
}
getch();
}

28.     Write a program to input three students Name and Age and display them.

Solution


#include<iostream.h>
#include<conio.h>
class student
{
char name[30];
            float age;
public:
            void getdata(void);
            void putdata(void);
};
void student :: getdata(void)
{
            cout << "Enter Name :";
            cin >> name;
            cout << "Enter Age:";
            cin >> age;
}
void student :: putdata(void)
{
            cout << "Name :" << name << "\n";
            cout << "Age :" << age << "\n";
}
const int size = 3;
main()
{
 student info[3];
for(int i=0; i<size; i++)
{
            cout<<"\nDetails of Students" << (i+1) << "\n";
            info[i].getdata();
}
cout << "\n";
for (int i=0; i< size; i++)
{
            cout <<"\nStudent"<< (i+1) << "\n";
            info[i].putdata();
}
getch();
}

29.     Write a program to find the mean of two numbers (25,40) using the friend function.

Solution


#include<iostream.h>
#include<conio.h>
class sample
{
int a;
int b;
public:
void setvalue ( ) { a = 25; b =40; }
friend float mean(sample s);
};

float mean(sample s)
{
            return float(s.a + s.b)/2.0;
}

main()
{
sample x;
x.setvalue();
cout << "Mean value = " << mean(x) << "\n";
getch();
}

30.     Write a program to display Roll number and marks of Tamil and English subjects and the total marks scored, using multilevel inheritance.

Solution


#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student :: get_number(int a)
{
roll_number =a;
}
void student :: put_number()
{
cout << "Roll Number :" << roll_number << "\n";
}
class test : public student
{
protected:
float tamil;
float english;
public:
void get_marks(float, float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
tamil = x; english=y;
}
void test :: put_marks()
{
cout<<"Marks scored in Tamil = " <<tamil << "\n";
cout<<"Marks scored in English = " <<english << "\n";
}
class result : public test
{
float total;
public:
void display(void);
};
void result :: display(void)
{
total = tamil+english;
put_number();
put_marks();
cout<<"Total = " << total << "\n";
}
main()
{
result student1;
student1.get_number(222);
student1.get_marks(90.0, 90.0);
student1.display();
getch();
            }

No comments:

Post a Comment