Followers

Monday, March 12, 2012

DIAGONAL ELEMENTS IN A SQUARE MATRIX


#include<iostream.h>
#include<conio.h>
int x[10][10], n, i, j;

void inpt(int a[10][10] , int n)
{
  for(i=0 ; i<n ; i++)
    for(j=0 ; j<n ; j++)
      cin>>a[i][j];
  cout<<"The matrix :\n";
  for(i=0 ; i<n ; i++)
  {
    cout<<"\n";
    for(j=0 ; j<n ; j++)
      cout<<a[i][j]<<"  ";
  }
  cout<<"\n";
}

void otpt(int a[10][10] , int n)
{
  for(i=0 ; i<n/2 ; i++)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      cout<<a[i][j]<<" , ";

    }
  }
  for(i=n-1 ; i>=(n/2) ; i--)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      {
      cout<<a[i][j];
      if(i==n/2)
        cout<<" .";
      else
        cout<<" , ";
      }
    }
  }

}

void main()
{
  clrscr();
  cout<<"Enter the number of rows or columns : ";
  cin>>n;
  cout<<"Enter a matrix : ";
  inpt(x,n);
  cout<<"Diagonal elements : ";
  otpt(x,n);
  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

COMPARING 2 STRINGS FOR EQUALITY

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>


int len1, len2;

void equal(char a[50], char b[50])
{
  if(len1==len2)
  {
    for(int i=0 ; i<len1 ; i++)
    {
      if(a[i]==b[i])
      continue;
      else
      {
      cout<<"NOT EQUAL";
      getch();
      exit(0);
      }
    }
    cout<<"EQUAL";
  }
  else
    cout<<"NOT EQUAL";

}

void main()
{
  char strng1[25], strng2[25];
  cout<<"enter a string : ";
  gets(strng1);
  len1=strlen(strng1);
  cout<<"enter another string : ";

  gets(strng2);
  len2=strlen(strng2);
  equal(strng1, strng2);

  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

REVERSING A STRING-WITHOUT USING ANOTHER STRING

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

void main()
{
  char strng[25];
  int len;
  cout<<"enter a string : ";
  gets(strng);
  len=strlen(strng);
  cout<<"\n\nreverse : \n";
  for(int i=len-1; i>=0 ; i--)
    cout<<strng[i];
  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

SEARCH LETTER IN A PARAGRAPH

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>

void main()
{

  char strng1[100], x;
  int len1, len2;
  cout<<"enter a string : ";
  gets(strng1);
  len1=strlen(strng1);
  cout<<"enter the letter to be searched : ";
  cin>>x;
  {
    textcolor(2);
    textbackground(0);
  }
  for(int i=0 ; i<len1 ; i++)
    {
      if(strng1[i]==x)
      {
      cprintf("(");
      cout<<strng1[i];
      cprintf(")");
      }
      else
      cout<<strng1[i];
    }

  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

Friday, January 6, 2012

REVERSING A STRING-USING ANOTHER STRING


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

void main()
{
  char strng[25], rev[25];
  int len;
  cout<<"enter a string : ";
  gets(strng);
  len=strlen(strng);
  for(int i=0 ; i<len ; i++)
  {
    rev[i]=strng[len-1-i];
  }
  cout<<"\n\n reverse :\n";
  puts(rev);

  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

SUM OF A RANGE

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

void main()
{
 int n, m, a=0, b=0;
 cout<<"Enter the starting number :";
 cin>>n;
 cout<<"Enter the last number :";
 cin>>m;
 a=n;
 int i=0;
 do
 {
  a=n+i;
  b=b+a;
  i++;
 }while(i<=m-n);
 cout<<"Sum of the range ="<<b;
 getch();
}
.................................................................
OUTPUT: (click to enlarge the image)

Thursday, January 5, 2012

CASE CONVERSION IN A STRING - USING LOGIC

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

char strng[50];
int len, n;

void cmpltcnvrsion(char strng[50])
{
  for(int i=0 ; i<len ; i++)
  {
    if((97<=int(strng[i]))&&(int(strng[i])<=122))
      cout<<char(int(strng[i])-32);

    else if((65<=int(strng[i]))&&(int(strng[i])<=90))
      cout<<char(int(strng[i])+32);

    else
      cout<<strng[i];
  }
}

void capital(char strng[50])
{
  for(int i=0 ; i<len ; i++)
  {
    if((97<=int(strng[i]))&&(int(strng[i])<=122))
      cout<<char(int(strng[i])-32);
    else
      cout<<strng[i];
  }
}

void small(char strng[50])
{
  for(int i=0 ; i<len ; i++)
  {
    if((65<=int(strng[i]))&&(int(strng[i])<=90))
      cout<<char(int(strng[i])+32);

    else
      cout<<strng[i];
  }

}

void main()
{
  clrscr();
  cout<<"Enter a string :\n";
  cin.getline(strng,50);
  len=strlen(strng);
  cout<<"\n\nChoose the type of conversion : \n 1>Complete conversion \n 2>ALL CAPITAL \n 3>all small \nEnter your choice : ";
  cin>>n;
  switch(n)
  {
    case 1  : cmpltcnvrsion(strng);
    break;
    case 2  : capital(strng);
    break;
    case 3  : small(strng);
    break;
  }
  getch();
}
.................................................................
OUTPUT: (click to enlarge the image)