Friday, April 26, 2013

ALL C++ PROGRAMS FOR OOP-PART2


 
               

// 1. Write a program using function to print line of 50 asterisk.

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

using namespace std;

void display(char ch);

int main()
{
display('*');
getch();
return(0);
}

void display(char ch)
{
for(int i=1;i<=50;i++)
{
cout<<ch;
}
}



// 2. Write a program to print line of n character of user given character.

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

using namespace std;

void display(char ch,int n);

int main()
{
int n;
char ch;
cout<<"Enter any character : ";cin>>ch;
cout<<"Enter the number of character '"<<ch<<"' to display : ";cin>>n;
display(ch,n);
getch();
return(0);
}

void display(char ch,int n)
{
for(int i=1;i<=n;i++)
{
cout<<ch;
}
}

// 3. WAP to calculate simple interest using inline function.

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

using namespace std;

inline float interest(float p,float t,float r)
{
return((p*t*r)/100);
}

int main()
{
float p,t,r;
cout<<"Principle = ";cin>>p;
cout<<"Time = ";cin>>t;
cout<<"Rate = ";cin>>r;
cout<<"Simple Interest = "<<interest(p,t,r);
getch();
return(0);

}



// 4. WAP to find surface area of box using inline function.
#include<iostream>
#include<conio.h>

using namespace std;

inline float surface_area(float l,float b,float h)
{
return(2*(l*b+b*h+l*h));
}

int main()
{
float l,b,h;
cout<<"Length = ";cin>>l;
cout<<"Breadth = ";cin>>b;
cout<<"Height = ";cin>>h;
cout<<"Surface Area = "<<surface_area(l,b,h);
getch();
return(0);
}



// 5. WAP to swap to character data using function.

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

using namespace std;

void swap(char &a,char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}

int main()
{
char ch1,ch2;
cout<<"Enter first character = ";cin>>ch1;
cout<<"Enter second character = ";cin>>ch2;
swap(ch1,ch2);
cout<<"After swapping character : ";
cout<<"\nNow first character = "<<ch1;
cout<<"\nNow second character = "<<ch2;
getch();
return(0);
}



// 6. WAP to calculate simple interest using function with default argument for rate = 15%, time = 2 years and principle = 100000.
#include<iostream>
#include<conio.h>

using namespace std;

float interest(float p=100000.0,float t=2.0,float r=15);

int main()
{
float p,t,r;
cout<<"Interst without passing any argument: "<<interest()<<endl;
cout<<"Principle = ";cin>>p;
cout<<"Interest passing only principal: "<<interest(p)<<endl;
cout<<"Principle = ";cin>>p;
cout<<"Time = ";cin>>t;
cout<<"Interest with passing principal and time: "<<interest(p,t)<<endl;
cout<<"Principle = ";cin>>p;
cout<<"Time = ";cin>>t;
cout<<"Rate = ";cin>>r;
cout<<"Simple Interest with p, t and r = "<<interest(p,t,r)<<endl;
getch();
return(0);
}

float interest(float p,float t,float r)
{
return((p*t*r)/100);
}




/* 7. WAP to print line of character using function and default argument
default character is '*' and default number of items is    50. */

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

using namespace std;

void display(char ch='*',int n=50);

int main()
{
char ch;
int n;
display();
cout<<"\nEnter the character to print : ";cin>>ch;
cout<<"Enter the number of character in a line : ";cin>>n;
display(ch,n);
getch();
return(0);
}

void display(char ch,int n)
{
for(int i=1;i<=n;i++)
{
cout<<ch;
}
}



/* 8. WAP that uses function prime() for testing prime.
Function should take integer argument and return Boolean value. */

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

using namespace std;

bool prime(int n);

int main()
{
int n;
cout<<"Enter any number : ";cin>>n;
if(prime(n)==1)
   cout<<n<<" is a prime number.";
else
   cout<<n<<" is not a prime number.";
getch();
return(0);
}

bool prime(int n)
{
if(n==0||n==1||n==2)
   return(0);
else
   {
for(int i=2;i<=(n/2);i++)
{
if(n%i==0)
   return(0);
}
return(1);
}
}




// 9. WAP to find cube of an integer using inline function.

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

using namespace std;

inline int cube(int n)
{
return(n*n*n);
}

int main()
{
int n;
cout<<"Enter any number : ";cin>>n;
cout<<"Cube of "<<n<<" is "<<cube(n);
getch();
return(0);
}


//10. WAP to swap to numeric data using function.(Reference variable).
#include<iostream>
#include<conio.h>

using namespace std;

void swap(int &,int &);

int main()
{
int a,b;
cout<<"Enter first number : ";cin>>a;
cout<<"Enter second number : ";cin>>b;
swap(a,b);
cout<<"After Swap !!!";
cout<<"\nFirst number is "<<a;
cout<<"\nSecond number is "<<b;
getch();
return(0);
}

void swap(int &x,int &y)
{
int z=x;
x=y;
y=z;
}



1.    [11]//   WAP to find maximum of pair of data of each type using overloaded function.

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float n;
cout<<"n=";
cin>>n;
int r;
r=floor(n+0.5);
cout<<"r="<<r;
return 0;
}



1.       [12]WAP to round a value to nearest integer using floor function.

#include<iostream>
#include<cmath>
using namespace std;
float power1(float x,int n);
int main()
{
float x;
int n;
cout<<"enter base and power";
cin>>x>>n;
cout<<"the power of"<<x<<"power"<<n<<"is"<<power1(x,n);
return 0;
}
float power1(float x,int n)
{
/* if(n<0)
return 1/(pow(x,n));*/
else if(n==0)
return  (1.0);
else 
return (x*power1(x,n-1));
}


[13]WAP to calculate power of base using recursion

#include<iostream>
using namespace std;
int rev(int);
int main()
{
int a;
cout<<"enter the number:";
cin>>a;
cout<<"the reversed number is "<<rev(a);
return 0;
}
int rev(int a)
{
int r=0;
    do
{
d=a%10;
r=r*10+a;
a=a/10;
} while(a!='\0');
return r;
}


[14]      WAP to reverse   a number using function.
#include<iostream>
using namespace std;
int main()
{
int a[10],n;
cout<<"enter n";
cin>>n;
int i; 
cout<<"enter "<<n<<"number"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
int max=a[0];
int min=a[0];
for(i=0;i<n;i++)
{
if (max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
cout<<"maximum number is= "<<max<<endl;
cout<<"minimum number is = "<<min<<endl;
return 0;
}


[15] WAP to find GCD of two number using function.
#include<iostream>
using namespace std;
/*#define swap(int a,int b){
int t;
t= a;
a= b;
b=t;*/

int main()
{
int a[6]={2,9,5,4,8,19};
int i,j;
cout<<"before sorting\n";
for(i=0;i<6;i++)
cout<<a[i]<<"\t";

for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
if(a[j]>a[j+1])
{
int t;
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
cout<<"\nafter sorting\n";
for(i=0;i<6;i++)
cout<<a[i]<<"\t";
return 0;
}



[16]     WAP to find the largest and smallest among n numbers.
#include<iostream>
using namespace std;

int main()
{
int a[6]={2,9,5,4,8,10};
int i,j,large,index;
cout<<"before sorting\n";
for(i=0;i<6;i++)
cout<<a[i]<<"\t";

for(i=5;i>0;i--)
{
large=a[0];
index=0;
for(j=1;j<=i;j++)
   {
if(a[j]>large)
{
large=a[j];
index=j;
   }
   a[index]=a[i];
   a[i]=large;
         }
    }
cout<<"\nafter sorting\n";
for(i=7;i>0;i--)
cout<<a[i]<<"\t";
return 0;

}



[17]    WAP to sort n numbers in ascending order using bubble sort.
#include <cstdlib>
#include <iostream>

using namespace std;
void selectsort(int A[],int n){
  int i,j,min;
  for(i=0;i<n;i++){
     min=i; 
     for(j=i+1;j<n;j++)
        if(A[j]<A[min]) min=j; //find min value
    //swapping
    int temp=A[i];
    A[i]=A[min];
    A[min]=temp; 

   } 

}

int main(int argc, char *argv[])
{
   int A[]={23,4,45,23,12,2};
   selectsort(A,6);
   for(int i=0;i<6;i++)
   cout<<A[i]<<endl;

   system("PAUSE");
   //return EXIT_SUCCESS;
}




[18]WAP to sort n numbers in descending order using selection sort
#include<iostream>
using namespace std;
void sort(int *,int );
int main()
{
int n,a[20],i;
cout<<"enter n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter n element";
cin>>a[i];
}
cout<<"numbers before sorting\n";
for(i=0;i<n;i++)
cout<<*(a+i)<<"\t";
sort(a,n);
cout<<"\nafter sorting\n";
for(i=0;i<n;i++)
cout<<*(a+i)<<"\t";
return 0;
}
void sort(int *a,int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n;j++)
if(*(a+i)>*(a+j))
{
int *temp;
*temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=*temp;
}
}
}

[19] WAP using function for 16,17,and 18.
#include<iostream>
using namespace std;
int main()
{
int *a;
int n,sum=0,i;
cout<<"n=";
cin>>n;
a=new int[n];
cout<<"enter"<<n<<"number"<<endl;
for(i=0;i<n;i++)
{
cin>>*(a+i);
sum+=*(a+i);
}
float avg=sum/n;
cout<<"sum="<<sum<<endl;
cout<<"avg="<<avg<<endl;
delete [] a;
return 0;
}

[20]WAP using function and pointer for 16, 17, and 18.
#include<iostream>
using namespace std;
int main()
{
int mul(int a[3][3],int b[3][3] ,int c[3][3]);
int i,j;
int a[3][3],b[3][3],c[3][3];
cout<<"enter elements for a matrix";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\nenter elements for b matrix";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
c=mul(a,b,c);
cout<<"the multiplied matrix is:";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<a[i][j];
return 0;
}
int mul(int a[3][3],int b[3][3],int c[3][3])
{
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
int c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}


[21] WAP to read n numbers in an array by using new and delete operator. And find their sum and average.

#include<iostream>
using namespace std;
int main()
{
int *a;
int n,sum=0,i;
cout<<"n=";
cin>>n;
a=new int[n];
cout<<"enter"<<n<<"number"<<endl;
for(i=0;i<n;i++)
{
cin>>*(a+i);
sum+=*(a+i);
}
float avg=sum/n;
cout<<"sum="<<sum<<endl;
cout<<"avg="<<avg<<endl;
delete [] a;
return 0;


1.      [22] WAP to find sum of square of diagonal elements of a square matrix.

#include<iostream>
using namespace std;
const int col =5;
const int row =5;
int main()
{
int a[row][col],i,j,n;
cout<<"enter the order of square matrix:";
cin>>n;
cout<<"enter elements of"<<n<<"*"<<n<<"matrix"<<endl;
for(i=0;i<n;i++)
  for(j=0;j<n;j++)
  cin>>a[i][j];
  int d1=0;
  for(i=0;i<n;i++)
  for(j=0;j<n;j++)
  if(i==j)
  d1=d1+a[i][j];
  int d2=0;
  i=0;
  j=n-1;
  while(i<=n-1&&j>0)
  {
  d2=d2+a[i][j];
  i++;
  j--;
  }
  cout<<"the sum of its first diagonal is "<<d1<<endl;
  cout<<"the sum of its second diagonal is "<<d2<<endl;
  return 0;
}



[23] Write a program to multiply two matrices using function.
#include<iostream>
using namespace std;
int main()
{
int mul(int a[3][3],int b[3][3] ,int c[3][3]);
int i,j;
int a[3][3],b[3][3],c[3][3];
cout<<"enter elements for a matrix";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\nenter elements for b matrix";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
c=mul(a,b,c);
cout<<"the multiplied matrix is:";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<a[i][j];
return 0;
}
int mul(int a[3][3],int b[3][3],int c[3][3])
{
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
int c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}


[23a] Write a program to multiply two matrices using function.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
//clrscr();
int a[10][10],b[10][10],c[10][10],m,n,o,p,i,j;
cout<<"Enter number of rows of A: ";
cin>>m;
cout<<"Enter number of coloumns of A: ";
cin>>n;
cout<<endl<<"Enter elements of matrix A: "<<endl;
//Coding by: Snehil Khanor
//http://WapCPP.blogspot.com
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"Enter element a"<<i+1<<j+1<<": ";
cin>>a[i][j];
}
}
cout<<endl<<"Enter number of rows of B: ";
cin>>o;
cout<<"Enter number of coloumns of B: ";
cin>>p;
cout<<endl<<"Enter elements of matrix B: "<<endl;
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
{
cout<<"Enter element b"<<i+1<<j+1<<": ";
cin>>b[i][j];
}
}
/*cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl<<endl;
}
cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl<<endl;
}*/
if(n==o)
{
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
c[i][j]=0;
for(int k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
cout<<endl<<"Matrix A * Matrix B = Matrix C: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl<<endl;
}
}
else
cout<<"Multiplication not possible :(";
getch();
}


[24] Write a program to reverse a string without using function.
#include<iostream>
#include<string.h>
int main()
{
 char str[100],temp;
 int i,j=0;

 printf("nEnter the string :");
 gets(str);

 i=0;
 j=strlen(str)-1;

 while(i<j)
     {
     temp=str[i];
     str[i]=str[j];
     str[j]=temp;
     i++;
     j--;
     }

 printf("nReverse string is :%s",str);
 return(0);
}


[25]Write a program to check whether a word is palindrome or not without using built in function.

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

using namespace std;
int main()
{
char str1[20];
int len=0,j;
cout<<"enter string ";
cin>>str1;
while(str1[len]!='\0')
{len++;
}
bool flag=1;
for(j=0;j<len/2;j++)
{
if(str1[j]!=str1[len-j-1])
{
flag=0;
break;
}
}
if(flag)
cout<<str1<<" is palindrome";
else
cout<<str1<<" is not palindrome";
cout<<endl;
turn 0;
}

[26] Write a program to copy string using pointer and function.

#include<iostream>
using namespace std;
int main()
{
void copy(char *,const char *);
char str1[10]="anita";
char str2[10];
    copy(str2,str1);
cout<<"the string copied is:"<<str2;
return 0;
}
void copy(char *ds,const char *ss)
{
while(*ss)
*ds++=*ss++;
*ds='\0';
}


[27]Write a program to add two time using structure and function

#include<iostream>
#include<conio.h>
using namespace std;
struct Time
{
int hour, minute;
};
void AddTime(Time *, Time * );
int main()
{
Time var1, var2;
for(int i=0;i<1;i++)
{
cout<<"for structure variable #"<<i+1<<" Enter the hours:";
cin>>var1.hour;
cout<<"for structure variable #"<<i+1<<" Enter the minutes:";
cin>>var1.minute;
//cout<<"for structure variable #"<<i+1<<" Enter the seconds:";
//cin>>var1.second;
//cout<<endl<<endl;
cout<<"for structure variable #"<<i+2<<" Enter the hours:";
cin>>var2.hour;
cout<<"for structure variable #"<<i+2<<" Enter the minutes:";
cin>>var2.minute;

}
AddTime(&var1, &var2);
cout<<"\nNew hours = "<<var2.hour;
cout<<"\nNew minutes = "<<var2.minute;
//cout<<"\nNew seconds = "<<var2.second;
cout<<endl;
}
void AddTime(Time *time1, Time *time2)
{
time2->hour += time1->hour;
time2->minute += time1->minute;
//time2->second += time1->second;

/*if(time2->second > 60)
{
time2->minute += 1;
time2->second -= 60;
}*/
if(time2->minute > 60)
{
time2->hour += 1;
time2->minute -= 60;
}
if(time2->hour > 24)
{
time2->hour -= 24;
}
}

[27b] Write a program to add two time using structure and function. (structure time should have member hours and minutes)



#include<iostream>
using namespace std;
struct time{
int hrs;
int mins;
}t1,t2,t3;
int add(int,int);
int main()
{

cout<<"enter hrs of t1:";
cin>>t1.hrs;
cout<<"enter mins of t1:";
cin>>t1.mins;

cout<<"enter hrs of t2:";
cin>>t2.hrs;
cout<<"enter mins of t2:";
cin>>t2.mins;
t3=add(t1,t2);
cout<<"time t3="<<t3.hrs<<":"<<t3.mins;
return 0;

}
int add(int t1,int t2)
{
int t;
t.mins=t1.mins+t2.mins;
t.hrs=0;
while(t.mins>=60)
{
t.mins-=60;
t.hrs++;
}
t.hrs+=t1.hrs+t2.hrs;
return(t);
}















No comments:

Post a Comment