Sunday 2 November 2014

non diagonal

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int r,c,i=0,j=0;
  int*ptr;
  cout<<"Enter row and column of a matrix";
  cin>>r>>c;
  ptr=new int[r*c];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  {
  cin>>ptr[i*c+j];
  }
  }
  cout<<endl<<"Matrix form";
  cout<<endl;
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  cout<<ptr[i*c+j]<<'\t';
  cout<<endl;
  }
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  {
     if(i!=j)
     ptr[i*c+j]=0;
  }
  }
  cout<<endl<<"The resulting matrix is:";
  cout<<endl;
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  cout<<ptr[i*c+j]<<'\t';
  cout<<endl;
  }
  getch();
}

diagonal

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int r,c,i=0,j=0,sum=0;
  int*ptr;
  cout<<"Enter row and column of a matrix";
  cin>>r>>c;
  ptr=new int[r*c];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  {
  cin>>ptr[i*c+j];
  }
  }
  cout<<endl<<"Matrix form";
  cout<<endl;
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  cout<<ptr[i*c+j]<<'\t';
  cout<<endl;
  }
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  {
     if(i==j && i+j==r-1)
     sum=sum+ptr[i*c+j];
  }
  }
  sum-ptr[r/2*c/2+c/2];
  cout<<endl<<"The Sum of diagonal elements of a matrix:"<<sum;
  getch();
}














series sum

#include<iostream.h>
#include<conio.h>
#include<math.h>
long facto(int);
void main()
{
  clrscr();
  int num,n;
  int i=0;
  long j=0;
  int sum=0;
  cout<<"The series is:";
  cout<<"x/1!+x^2/2!+x^3/3!+............................";
  cout<<endl<<"Enter the number upto which you want the sum:";
  cin>>n;
  cout<<endl<<"The value of x:";
  cin>>num;
  for(i=1;i<=n;i++)
  {
   j=facto(i);
   sum=sum+pow(num,i)/j;
  }
  cout<<endl<<"The sum of the series is:"<<sum;
  getch();
}
long facto(int g)
{
  int k=1;
  while(g>0)
  {
   k=k*g;
   g--;
  }
}



























#include<iostream.h>
#include<conio.h>
#include<math.h>
long facto(int);
void main()
{
  clrscr();
  int num,n;
  int i=0;
  long j=0;
  int sum=0;
  cout<<"The series is:";
  cout<<"x/1!-x^2/2!+x^3/3!-............................";
  cout<<endl<<"Enter the number upto which you want the sum:";
  cin>>n;
  cout<<endl<<"The value of x:";
  cin>>num;
  for(i=1;i<=n;i++)
  {
   j=facto(i);
   if(i%2==0)
   sum=sum-pow(num,i)/j;
   else
   sum=sum+pow(num,i)/j;
  }
  cout<<endl<<"The sum of the series is:"<<sum;
  getch();
}
long facto(int g)
{
  int k=1;
  while(g>0)
  {
   k=k*g;
   g--;
  }
}

copy of a string

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char *ptr;
int i=0,j=0,len=0;
char str1[20],str2[20];
cout<<"Enter a string:";
gets(str1);
cout<<endl<<"Original string is:";
puts(str1);
ptr=str1;
for(i=0;*(ptr+i)!='\0';i++)
{
      len++;
}
for(i=0,j=0;*(ptr+i)!='\0';i++,j++)
{
      str2[j]=*(ptr+i);
}
cout<<endl<<"Coppied string is:";
puts(str2);
getch();
}

palindrome

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char *ptr;
int i=0,j=0,k=0,len=0;
char str1[20];
cout<<"Enter a string:";
gets(str1);
cout<<endl<<"Original string is:";
puts(str1);
ptr=str1;
for(i=0;*(ptr+i)!='\0';i++)
{
      len++;
}
for(i=0,j=len-1;i<=len/2;i++,j--)
{
     if( *(ptr+i)==*(ptr+j) )
     k=1;
}
if(k==1)
cout<<endl<<"Palindrone";
else
cout<<endl<<"Not a Palindrone";
getch();
}

addition of two matrix

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int r1,c1,r2,c2,i=0,j=0;
  int*ptr1;
  int*ptr2;
  int*ptr3;
  cout<<"Enter row and column of a matrix for matrix1";
  cin>>r1>>c1;
  ptr1=new int[r1*c1];
  ptr2=new int[r2*c2];
  ptr3=new int[r1*c1];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r1;i++)
  {
  for(j=0;j<c1;j++)
  {
  cin>>ptr1[i*c1+j];
  }
  }
  cout<<endl<<"Matrix form-1";
  cout<<endl;
  for(i=0;i<r1;i++)
  {
  for(j=0;j<c1;j++)
  cout<<ptr1[i*c1+j]<<'\t';
  cout<<endl;

  }
  cout<<"Enter row and column of a matrix for matrix2";
  cin>>r2>>c2;
  ptr2=new int[r2*c2];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r2;i++)
  {
  for(j=0;j<c2;j++)
  {
  cin>>ptr2[i*c2+j];
  }
  }
  cout<<endl<<"Matrix form-2";
  cout<<endl;
  for(i=0;i<r2;i++)
  {
  for(j=0;j<c2;j++)
  cout<<ptr2[i*c2+j]<<'\t';
  cout<<endl;

  }
  for(i=0;i<r1;i++)
  {
  for(j=0;j<c2;j++)
  ptr3[i*c2+j]=ptr1[i*c2+j]+ptr2[i*c2+j];

  }
  cout<<endl<<"The resulting matrix after adding two matrices";
  for(i=0;i<r1;i++)
  {
  for(j=0;j<c1;j++)
  cout<<ptr3[i*c1+j]<<'\t';
  cout<<endl;

  }
  getch();
}

transpose of a matrix

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int r,c,i=0,j=0,sum=0;
  int*ptr;
  cout<<"Enter row and column of a matrix";
  cin>>r>>c;
  ptr=new int[r*c];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  {
  cin>>ptr[i*c+j];
  }
  }
  cout<<endl<<"Matrix form";
  cout<<endl;
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  cout<<ptr[i*c+j]<<'\t';
  cout<<endl;

  }
  cout<<endl<<"The Transpose of a matrix is:";
  cout<<endl;
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  cout<<ptr[j*r+i]<<'\t';
  cout<<endl;

  }
  getch();
}

reverse

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char *ptr;
int i=0,j=0,len=0;
char str1[20],str2[20];
cout<<"Enter a string:";
gets(str1);
cout<<endl<<"Original string is:";
puts(str1);
ptr=str1;
for(i=0;*(ptr+i)!='\0';i++)
{
      len++;
}
for(i=0,j=len-1;*(ptr+i)!='\0';i++,j--)
{
      str2[j]=*(ptr+i);
}
cout<<endl<<"Reversed string is:";
puts(str2);
getch();
}

trace of a matrix

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int r,c,i=0,j=0,sum=0;
  int*ptr;
  cout<<"Enter row and column of a matrix";
  cin>>r>>c;
  ptr=new int[r*c];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  {
  cin>>ptr[i*c+j];
  }
  }
  cout<<endl<<"Matrix form";
  cout<<endl;
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  cout<<ptr[i*c+j]<<'\t';
  cout<<endl;
  }
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  {
     if(i==j)
     sum=sum+ptr[i*c+j];
  }
  }
  cout<<endl<<"The Trace of a matrix:"<<sum;
  getch();
}

comparing two string

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char *ptr;
char *ptr1;
int i=0,j=0,k=1;
char str1[20],str2[20];
cout<<"Enter a string:";
gets(str1);
cout<<"Enter another string:";
gets(str2);
ptr1=str2;
cout<<endl<<"Original string is:";
puts(str1);
ptr=str1;
for(i=0,j=0;*(ptr+i)!='\0';i++,j++)
{
      if( *(ptr1+j)==*(ptr+i) )
      k=1;
}
if(k==1)
cout<<endl<<"Similar string";
else
cout<<endl<<"Different string";
getch();
}

concatenation of strimg

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int r,c,i=0,j=0,sum=0;
  int*ptr;
  cout<<"Enter row and column of a matrix";
  cin>>r>>c;
  ptr=new int[r*c];
  cout<<endl<<"Enter elements of the matrix";
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  {
  cin>>ptr[i*c+j];
  }
  }
  cout<<endl<<"Matrix form";
  cout<<endl;
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  cout<<ptr[i*c+j]<<'\t';
  cout<<endl;

  }
  cout<<endl<<"The Transpose of a matrix is:";
  cout<<endl;
  for(i=0;i<r;i++)
  {
  for(j=0;j<c;j++)
  cout<<ptr[j*r+i]<<'\t';
  cout<<endl;

  }
  getch();
}

to count no. of blank spaces

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char *ptr;
int i=0,k=0;
char str1[20];
cout<<"Enter a string:";
gets(str1);
cout<<endl<<"Original string is:";
puts(str1);
ptr=str1;
for(i=0;*(ptr+i)!='\0';i++)
{
      if( *(ptr+i)==' ' )
      k++;
      else
      cout<<*(ptr+i);
}
cout<<endl<<"Number of blank spaces is:";
cout<<endl<<k;
getch();
}