Monday, 23 February 2015

U3P10

Create two classes DM and DB which store the value of distances. DM stores distances in meters and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object stores the results may a DM object or DB object, depending on the units in which the results are required. The display should be in the format of feet and inches or meters and centimeters depending on the object on display.
1Feet = 0.3048 Meter, 1Meter = 3.28 Feet,
1Inch = 2.54 Centimeter, 1Centimeter = 0.3937 Inch

 #include <iostream.h>  
 #include <conio.h>  
 class db;  
 class dm  
 {  
      float mt;  
      float cm;  
      public:  
      void getdata()  
      {  
           cout<<"CLASS DM: \n\n";  
           cout<<"Enter Values for metres : ";  
           cin>>mt;  
           cout<<"Enter Values for centimetres: ";  
           cin>>cm;  
      }  
      void display()  
      {  
           cout<<"\nTotal value of distance in metres : "<<mt;  
           cout<<"\nTotal value of distance in Centimetres : "<<cm;  
      }  
      friend dm add(dm,db);  
 };  
 class db  
 {  
      float feet;  
      float inches;  
      public:  
      void getdata()  
      {  
           cout<<"\nCLASS DB: \n\n";  
           cout<<"Enter Values for feet : ";  
           cin>>feet;  
           cout<<"Enter Values for inches : ";  
           cin>>inches;  
      }  
      void display()  
      {  
           cout<<"\nThe value of distance in feet is "<<feet;  
           cout<<"\nThe value of distance in inches is "<<inches;  
      }  
      friend dm add(dm,db);  
 };  
 dm add(dm a,db b)  
 {  
      dm temp;  
      temp.cm = a.cm + (a.mt*100) + (b.feet*30.48) + (b.inches*2.54);  
      temp.mt = a.mt + (a.cm*0.01) + (b.feet*0.3048) + (b.inches*0.0254);  
      return(temp);  
 }  
 void main()  
 {  
      clrscr();  
      dm obj1;  
      obj1.getdata();  
      db obj2;  
      obj2.getdata();  
      cout<<"\nFriend Function Addition : \n";  
      dm extra;  
      extra=add(obj1,obj2);  
      extra.display();  
      getch();  
 }  

OUTPUT : 

U3P9

Write a function power () to raise a number m to a power n. The function takes a double value for m and int value for n, and returns the result correctly. Use a default value of 2 for n to make the function to calculate the square whenthis argument is omitted. Write a main that gets the value of m and n from theuser to test the function.

 #include<iostream.h>  
 #include<conio.h>  
 #include<math.h>  
 double power(double m, int n)  
 {  
      double ans = pow(m,n);  
      return ans;  
 }  
 void main()  
 {  
      clrscr();  
      double a;  
      int b;  
      char ch;  
      cout<<"Enter the double value of base m : ";  
      cin>>a;  
      cout<<"\nDefault value of power is 2. Want to give different value?(Y/N) : ";  
      cin>>ch;  
      if(ch=='y' || ch=='Y')  
      {  
           cout<<"\nEnter integer value for power : ";  
           cin>>b;  
           cout<<"\nValue of Number "<<a<<" to the Power of "<<b<<" is "<<power(a,b);  
      }  
      else  
      {  
           cout<<"\nValue of Number "<<a<<" to the Power of 2 is "<<power(a,2);  
      }  
      getch();  
 }  

OUTPUT :

U3P8

Define a class complex, having data member as X and Y, Define a friend function sum () to add two complex numbers and display all numbers using show () friend function.

 #include<iostream.h>  
 #include<conio.h>  
 class complex  
 {  
      double X,Y;  
      public:  
      void asign()  
      {  
           X =38.3;  
           Y =46.4;  
      }  
      friend void sum(complex);  
      friend void show(complex);  
 };  
 void sum(complex obj)  
 {  
      cout<<"SUM of given numbers : "<<obj.X + obj.Y<<endl;  
 }  
 void show(complex obj)  
 {  
      cout<<"Given integer value for X is : "<<obj.X<<endl;  
      cout<<"Given integer value for Y is : "<<obj.Y<<endl;  
 }  
 void main()  
 {  
      clrscr();  
      cout<<"Program will show use of Friend Function."<<endl;  
      complex c;  
      cout<<"Assigning values...\nX = 38.3\nY = 46.4"<<endl;  
      c.asign();  
      cout<<"\nCalling sum() Friend Function..."<<endl;  
      sum(c);  
      cout<<"\nCalling show() Friend Function..."<<endl;  
      show(c);  
      getch();  
 }  

OUTPUT :

U3P5

Program for finding maximum number using Friend function for two classes.

 #include<iostream.h>  
 #include<conio.h>  
 #include<limits.h>  
 class C2;  
 class C1  
 {  
      int v1,v2,v3;  
      public:  
      void getdata1()  
      {  
           cout<<"Enter 3 values of Class C1 : ";  
           cout<<"\nEnter value 1 : ";  
           cin>>v1;  
           cout<<"Enter value 2 : ";  
           cin>>v2;  
           cout<<"Enter value 3 : ";  
           cin>>v3;  
      }  
      friend void find_max(C1,C2);  
 };  
 class C2  
 {  
      int v4,v5,v6;  
      public:  
      void getdata2()  
      {  
           cout<<"\nEnter 3 values of Class C2 : "<<endl;  
           cout<<"Enter value 4 : ";  
           cin>>v4;  
           cout<<"Enter value 5 : ";  
           cin>>v5;  
           cout<<"Enter value 6 : ";  
           cin>>v6;  
      }  
      friend void find_max(C1,C2);  
 };  
 void find_max(C1 obj1, C2 obj2)  
 {  
      int max1;  
      int max2;  
      if(obj1.v1>obj1.v2 && obj1.v1>obj1.v3)  
      {  
           max1 = obj1.v1;  
      }  
      else if(obj1.v2>obj1.v1 && obj1.v2>obj1.v3)  
      {  
           max1 = obj1.v2;  
      }  
      else  
      {  
           max1 = obj1.v3;  
      }  
      if(obj2.v4>obj2.v5 && obj2.v4>obj2.v6)  
      {  
           max2 = obj2.v4;  
      }  
      else if(obj2.v5>obj2.v4 && obj2.v5>obj2.v6)  
      {  
           max2 = obj2.v5;  
      }  
      else  
      {  
           max2 = obj2.v6;  
      }  
      cout<<endl<<"Maximum number of values given in Class C1 : "<<max1<<endl;  
      cout<<"Maximum number of values given in Class C2 : "<<max2<<endl;  
 }  
 void main()  
 {  
      clrscr();  
      C1 c1_obj;  
      c1_obj.getdata1();  
      C2 c2_obj;  
      c2_obj.getdata2();  
      cout<<"\nFinding Maximum number using Friend Function...";  
      find_max(c1_obj,c2_obj);  
      getch();  
 }  

OUTPUT :

Monday, 16 February 2015

U2P1

Write a program to show use of manipulators.


 #include<iostream.h>  
 #include<conio.h>  
 #include<iomanip.h>  
   
 void main()  
 {  
  clrscr();  
   
  cout<<"--------- USE OF MANIPULATORS : ---------\n\n";  
   
  cout<<"Use of 'setw()' :- \nX = 234, Y = 32, Setting width = 5\n";  
  int x=234,y=32;  
  cout<<"X = "<<setw(5)<<x<<endl<<"Y = "<<setw(5)<<y<<endl;  
   
  cout<<"\nUse of 'setfill()' :- \nX = 234, Y = 32, Setting width = 5, Setfill characters : '*' and '#'\n";  
  cout<<"X = "<<setw(5)<<setfill('*')<<x<<endl<<"Y = "<<setw(5)<<setfill('#')<<y<<endl;  
   
  cout<<"\nUse of 'setprecision()' :-\nZ = 5.0/3.0\n";  
  float z = 5.0/3.0;  
  cout<<"Z [setprecision(2)] :\t"<<setprecision(2)<<z<<endl;  
  cout<<"Z [setprecision(3)] :\t"<<setprecision(3)<<z<<endl;  
  cout<<"Z [setprecision(4)] :\t"<<setprecision(4)<<z<<endl;  
  cout<<"Z [setprecision(5)] :\t"<<setprecision(5)<<z<<endl;  
   
  cout<<"\nUse of 'setbase()' :-\nD = 790\n";  
  int d = 790;  
  cout<<"Octal value of "<<d<<" is [setbase(8)] :\t"<<setbase(8)<<d<<endl;  
  cout<<"Decimal value of "<<d<<" is [setbase(10)] :\t"<<setbase(10)<<d<<endl;  
  cout<<"Hexa Decimal value of "<<d<<" is [setbase(16)] :\t"<<setbase(16)<<d<<endl<<endl;  
   
  cout<<"\nUse of 'dec','hex' and 'oct' :-\nD = 790\n";  
  cout<<"Octal Base of "<<d<<" (oct) is "<<oct<<d<<endl;  
  cout<<"Decimal Base of "<<d<<" (dec) is "<<dec<<d<<endl;  
  cout<<"Hexa Decimal Base of "<<d<<" (hex) is "<<hex<<d<<endl;  
   
  cout<<"\nUse of 'ends' :- \nNumber = 359\nOutput usin 'ends' : \n";  
  int dh = 359;  
  cout<<"Number = "<<dh<<ends<<endl;  
   
  cout<<"\nUse of 'ws' :-\n";  
  char name[50];  
  cout<<"Type the text line : \n";  
  cin>>ws>>name;  
  cout<<"Typed text is : "<<name<<endl;  
  getch();  
 }  


OUTPUT :

U2P5

Program for Analysis of mark sheet using else if ladder.


 #include<iostream.h>  
 #include<conio.h>  
 #include<iomanip.h>  
   
 void main()  
 {  
      clrscr();  
      float marks[3],sum=0;  
      again:  
      cout<<"Program for Analysis of a marksheet using ELSE-IF ladder.";  
      cout<<"\n\nEnter the marks of 3 subjects (Out of 100) :\n";  
   
      for(int i=0;i<3;i++)  
      {  
      cout<<"Subject "<<i+1<<" marks : ";  
      cin>>marks[i];  
      if(marks[i]>100)  
      {  
           cout<<"\n\t---- Enter value between 0 to 100 ----\n";  
           getch();  
           clrscr();  
           goto again;  
      }  
      cout<<"\n";  
      }  
   
      for(int j=0;j<3;j++)  
      {  
           sum = sum + marks[j];  
      }  
   
      float per = sum/3;  
      cout<<"\nPercentage :\t"<<setprecision(2)<<per<<"%";  
      cout<<"\nGrade"<<setw(8)<<": ";  
   
      if(per>=75)  
      {  
           cout<<"\tAA";  
      }  
      else if(per<75 && per>=60)  
      {  
           cout<<"\tBB";  
      }  
      else if(per<60 && per>=35)  
      {  
           cout<<"\tCC";  
      }  
      else if(per<35)  
      {  
           cout<<"\tFF";  
      }  
      else  
      {  
           cout<<"\tError";  
      }  
   
      getch();  
 }  

OUTPUT : 


Saturday, 14 February 2015

U3P4

Example of Inline Function in C++


 #include<iostream.h>  
 #include<conio.h>  
   
 inline void check(int a)  
 {  
      (a%2==0)? cout<<"Even" : cout<<"Odd";  
 }  
   
 void main()  
 {  
      clrscr();  
      cout<<"check(int a) is an inline function to categorize the num as Even or Odd.\n\n";  
      cout<<"check (23)\t: ";  
      check(23);  
      cout<<"\n";  
   
   
      cout<<"check (48)\t: ";  
      check(48);  
      cout<<"\n";  
   
   
      cout<<"check (64)\t: ";  
      check(64);  
      cout<<"\n";  
   
      cout<<"check (83)\t: ";  
      check(83);  
      cout<<"\n";  
   
      getch();  
 }  

OUTPUT : 


U3P2

Program to print Fibonacci Series


 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
      clrscr();  
      int terms;  
      cout<<"Programs will display first n terms of Fibonacci series.\n";  
      cout<<"\nEnter number of terms to be displayed (Max 24) : ";  
      cin>>terms;  
      double a=0,b=1,sum=a+b;  
      cout<<"\n";  
      for(int i=0;i<terms;i++)  
      {  
           if(i==0 || i==1)  
           {  
                cout<<i<<"\t";  
           }  
           else  
           {  
                cout<<sum<<"\t";  
                a = b;  
                b = sum;  
                sum = a+b;  
           }  
      }  
      getch();  
 }  

OUTPUT : 


Wednesday, 11 February 2015

u3p3

Write a program using a function with arguments to swap the values of a pair of integers using call by reference.



 #include<iostream.h>  
 #include<conio.h>  
 void swap_fun(int x, int y)  
 {  
       int temp;  
       temp = x;  
       x = y;  
       y = temp;  
       cout<<"\nValues after the swap : \nX = "<<x<<", Y = "<<y;  
 }  
 void main()  
 {  
       clrscr();  
       int x, y;  
       cout<<"Enter value for swap : \nEnter value of X = ";  
       cin>>x;       
       cout<<"Enter value of Y = ";  
       cin>>y;  
       swap_fun(x,y);  
       getch();  
 }  

OUTPUT : 

u3p1

Program to find factorial of a given number.


 #include<iostream.h>  
 #include<conio.h>  
 void factorial(int x)  
 {  
  int fact=x;  
  for(int i=x;i>1;i--)  
  {  
    fact = fact*(i-1);  
  }  
  cout<<"factorial of "<<x<<" is : "<<fact;  
 }  
 void main()  
 {  
      clrscr();  
      int num;  
      cout<<"Program to find Factorial using USer Defined Function.";  
      cout<<"\n\nEnter a number : ";  
      cin>>num;  
      factorial(num);  
      getch();  
 }  


OUTPUT : 

u2p4

An electricity board charges the following rates to domestic users to discharge large consumption of energy.
For the first 100 units – 60 P per unit
For next 200 units - 80P per unit
Beyond 300 units - 90 P per Unit
All users are charged a minimum of Rs.50.00. If the total amount is more than Rs. 300.00 then an additional surcharge of 15% is added. Write a program to read the names of users and number of units consumed and print out the charges with names.


 #include<iostream.h>  
 #include<conio.h>  
 #include<stdlib.h>  
 void main()  
 {  
  clrscr();  
  int u;  
  cout<<"Enter Number of users : ";  
  cin>>u;  
  if(u==0)  
  {  
      cout<<"\n\n\n\n\t\tBYE THEN";  
      getch();  
      exit(0);  
  }  
  char name['u'][10];  
  float units['u'],min_amt=50,surcharge=15;  
  double total_amt['u'];  
  cout<<"\n----------------------------------\nEnter name of the user and units consumed for particular user.";  
  for(int i=0;i<u;i++)  
  {  
       cout<<"\n\nEnter name of user("<<i+1<<") : ";  
       cin>>name[i];  
       cout<<"\nEnter Units consumed by "<<name[i]<<" : ";  
       cin>>units[i];  
  }  
  for(int j=0;j<u;j++)  
  {  
  total_amt[j]=0;  
  if(units[j]<=100)  
  {  
       total_amt[j] = units[j]*60/100;  
       if (total_amt[j]<=min_amt)  
       {  
           total_amt[j]=50;  
       }    
  }  
  else if(units[j]<=300)  
  {  
       total_amt[j] = total_amt[j] + (100*60/100) + (units[j]-100)*80/100;  
       if(total_amt[j]>300)  
       {  
             total_amt[j] = total_amt[j] + (total_amt[j]*surcharge/100);  
       }  
  }  
  else if(units[j]>300)  
  {  
       total_amt[j] = total_amt[j] + (100*60/100) + (200*80/100) + ((units[j]-300)*90/100);  
       if(total_amt[j]>300)  
       {  
             total_amt[j] = total_amt[j] + (total_amt[j]*surcharge/100);  
       }  
  }  
  }  
  cout<<"\n-----------------\nTotal Chargable amount for each user is as below : ";  
  cout<<"\n-----------------\nName\tAmount\n-----------------";  
  for(int d=0;d<u;d++)  
  {  
  cout<<"\n"<<name[d]<<"\t"<<total_amt[d];  
  }  
  getch();  
 }  
OUTPUT


u2p2

Program for Use of Scope Resolution Operator (::)


 #include<iostream.h>  
 #include<conio.h>  
 class demo  
 {  
      public:  
      void disp_function();  
 };  
 void demo :: disp_function()  
 {  
      cout<<"Function is defined out side the class with the use of Scope Resolution Operator.";  
 }  
 void main()  
 {  
      clrscr();  
      demo obj;  
      obj.disp_function();  
      getch();  
 }  

OUTPUT : 

u2p3

Write a program to evaluate the following investment equation V = P(1+r)n.


 #include<iostream.h>  
 #include<conio.h>  
 #include<math.h>  
 void main()  
 {  
      clrscr();  
      unsigned long int v;  
      int p,a=0;  
      float r,n;  
       cout<<"This program evaluates investment equation V = P(1+r)n";  
       cout<<"\n\nEnter value of P : ";  
       cin>>p;  
       cout<<"\nEnter value of r : ";  
       cin>>r;  
       cout<<"\nEnter value of n(months) : ";  
       cin>>n;  
       a = p*(1+r);  
       v = a*(n/12);  
       cout<<"\n\nValue of V is "<<v;  
       getch();  
 }  

OUTPUT

u1p5

Program that convert the temperature from Fahrenheit to Celsius.(F-32)*5/9.


 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
      clrscr();  
      double temp_c,temp_f;  
      cout<<"This program will convert Temperature from Fahrenheit to Celsius.\n\n";  
      cout<<"Enter Fahrenheit Value : ";  
      cin>>temp_f;  
      temp_c = (temp_f-32)*5/9;  
      cout<<"\n\nCelcius value of "<<temp_f<<" is "<<temp_c;  
      getch();  
 }  

OUTPUT :


u1p4

Program to add the digits of given integer number.


 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
      clrscr();  
      int D,sum=0;  
      cout<<"This program will add the digits of given integer number(MAX 32767).\n";  
      cout<<"Enter integer value : ";  
      cin>>D;  
      while(D>0)  
      {  
            sum = sum + D%10;  
            D = D/10;  
      }  
      cout<<"\n\nSum of the digits of given integer is "<<sum;  
      getch();  
 }  

OUTPUT :


u1p3

Program to find even and odd numbers between 100 and 200.



 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
  clrscr();  
  int d1=100;  
  int d2=200;  
  cout<<"This Program will display Even and Odd numbers between "<<d1<<" and "<<d2<<".\n";  
  cout<<"\nEven Numbers : \n";  
  for(int i=d1;i<=d2;i++)  
  {  
       if(i%2==0)  
       {  
           cout<<"\t"<<i;  
       }  
  }  
  cout<<"\n\nOdd Numbers : \n";  
  for(int j=d1;j<=d2;j++)  
  {  
       if(j%2!=0)  
       {  
           cout<<"\t"<<j;  
       }  
  }  
  getch();  
 }  

OUTPUT :


u1p2

Program which get a integer number and display "WELL DONE" that many times.


 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
  clrscr();  
  int D;  
  cout<<"Enter Number of loops : \n";  
  cin>>D;  
  for(int N=1;N<=D;N++)  
  {  
      cout<<"\n"<<N<<".\tWELL DONE";  
  }  
  getch();  
 }  

OUTPUT :


u1p1

1) Write a program to display “LJIET” using cout statement.


 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
      clrscr();  
      cout<<"LJIET";  
      getch();  
 }  

OUTPUT :