Followers

Thursday, March 15, 2012

HCF


#include<iostream.h>
#include<conio.h>
int n1, n2, a, hcf;
void main()
{
  cout<<"Enter two numbers : ";
  cin>>n1>>n2;
  if(n1<n2)
    a=n1;
  else
    a=n2;
  for(int i=a; i>=1 ; i--)
  {
    if(n1%i==0 && n2%i==0)
    {
      hcf=i;
      break;
    }
  }
  cout<<"\nHCF = "<<hcf;
  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

LCM

#include<iostream.h>
#include<conio.h>
int n1, n2, lcm,a;
void main()
{
  cout<<"Enter two integers : ";
  cin>>n1>>n2;
  cout<<"\nLCM = ";
  if(n1>n2)
    a=n2;
  else
    a=n1;
  for(int i=a; i<=((n1)*(n2)); i++)
  {
    if(i%n1==0 && i%n2==0)
    {
      lcm=i;
      break;
    }
  }
  cout<<"\n"<<lcm;
  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

Wednesday, March 14, 2012

INVERTED TRIANGLE OF (*)

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

void main()
{
 clrscr();
 int n;
 cout<<"enter the number of lines :";
 cin>>n;
 for(int i=n; i>=1 ; i--)
 {
   for(int j=i ; j>=1 ; j--)
     cout<<"*";
   cout<<"\n";
 }
 getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

LATERALLY INVERTED TRIANGLE OF ALPHABETS

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

void main()
{
 int n;
 cout<<"enter the number of lines :";
 cin>>n;
 for(int i=1 ; i<=n ; i++)
 {
   for(int j=1 ; j<=n-i ; j++)
     cout<<" ";
   for(int k=1 ; k<=i ; k++)
     cout<<char(64+k);
   cout<<"\n";
 }
 getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

LATERALLY INVERTED TRIANGLE OF ALPHABETS

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

void main()
{
 int n;
 cout<<"enter the number of lines :";
 cin>>n;
 for(int i=1 ; i<=n ; i++)
 {
   for(int j=1 ; j<=n-i ; j++)
     cout<<" ";
   for(int k=1 ; k<=i ; k++)
     cout<<char(64+k);
   cout<<"\n";
 }
 getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

TRIANGLE OF NUMBERS

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

void main()
{
 clrscr();
 int n;
 cout<<"enter the number of lines :";
 cin>>n;
 for(int i=1 ; i<=n ; i++)
 {
   for(int j=1; j<=i ; j++)
     cout<<(0+j);
   cout<<"\n";
 }
 getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

DIAMOND OF (*)

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

void main()
{
 clrscr();
 int n, a, b;
 cout<<"enter the number of lines : ";
 cin>>n;
 a=((n/2)+1);
 b=n-a;
 for(int i=1 ; i<=a ; i++)
 {
   for(int j=1 ; j<=a-i ; j++)
     cout<<" ";

   for(int k=1 ; k<=i ; k++)
     cout<<"*";

   for(int l=2 ; l<=i ; l++)
     cout<<"*";
   cout<<"\n";
 }

 int c=b+1;
// int d=b-1;

 for(int p=1; p<=b ; p++)
 {
  for(int q=1 ; q<=p ; q++)
    cout<<" ";
  for(int s=1 ; s<=c-p ; s++)
    cout<<"*";
  for(int r=1 ; r<=b-p ; r++)
    cout<<"*";
  cout<<"\n";
 }
 getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

TRIANGLE OF ALPHABETS

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

void main()
{
 clrscr();
 int n;
 cout<<"enter the number of lines :";
 cin>>n;
 for(int i=1 ; i<=n ; i++)
 {
   for(int j=1; j<=i ; j++)
     cout<<char(64+j);
   cout<<"\n";
 }
 getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

CHECKING A LIST OF INTEGERS FOR PALINDROME

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

long int rev=0, a[10];
int nmbr;

void revrse(int n)
{

  do
  {
    int temp=n%10;
    rev=(rev*10)+temp;
    n=n/10;
  }while(n!=0);
}

void cmpare(int n)
{
  cout<<"\n\t"<<rev;
  if(n==rev)
    cout<<"PALINDROME";
  else
    cout<<"NON-PALINDROME";
  cout<<"\n";
}

void main()
{
  clrscr();
  cout<<"enter the number of integers : ";
  cin>>nmbr;
  cout<<"Enter the integers : ";
  for(int i=0 ; i<nmbr ; i++)
    cin>>a[i];
  for( i=0 ; i<nmbr ; i++)
  {
    revrse(a[i]);
    cmpare(a[i]);
    rev=0;
  }

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

Monday, March 12, 2012

SUM OF DIAGONAL ELEMENTS OF A SQUARE MATRIX

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

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)
      b=b+a[i][j];
      else
      continue;
    }
  }
  for(i=n-1 ; i>=(n/2) ; i--)
  {
    for(j=0 ; j<n ; j++)
    {
      if(j==i || j==n-1-i)
      b=b+a[i][j];
      else
      continue;
    }
  }
  cout<<"\nSum of diagonal elements = "<<b;
}

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


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)