Pages

Subscribe:

Blogger templates

Friday, March 9, 2012

Matrix ( Diagonal Sum )

The coding starts from next line:-
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,sum=0,a[3][3];
cout<<"\nEnter the Matrix of 3-3 size:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<3;i++)
 {
 for(j=0;j<3;j++)
  {
  cout<<" "<<a[i][j];
  }
 cout<<"\n";
 }

for(i=0;i<3;i++)
{
 for(j=0;j<3;j++)
  {
   if(i==j)
   {
   sum=sum+a[i][j];
   }

  }

}
cout<<"\n Sum is:"<<sum;
getch();
}



Fibonacci Series

The coding starts from next line:-
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int i,second=1,first=0,third;
cout<<"\nFIBINACOO SERIES:";
cout<<"\n"<<first;

cout<<"\n"<<second;
for(i=3;i<10;i++)
{
third=first+second;
cout<<third;
first=second;
second=third;
}
getch();
}



Prime or Composite number

The coding starts from next line:-
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void main()
{
clrscr();
int i,num,ans;
cout<<"\nEnter the number you want to check:";
cin>>num;
for(i=2;i<=(num-1);i++)          //(i=2;i<=num/2;i++)
{
if(num%2==0)
{
ans=1;
break;
}
else
{
ans=0;
}
}
if(ans==1)
{
cout<<"\It is a composite number";
}
else {
cout<<"\nits a prime number";
}
getch();
}




Replacing space by a character " * "

The coding starts from next line:-
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char ch,name[20];
int i,l;
cout<<"\nEnter a String:";
gets(name);
l=strlen(name);

for(i=0;i<=l;i++)
{
if(name[i]==' ')
{
name[i]='*';
}
}
cout<<"\n "<<name;
getch();
}





Bubble Sort ( Ascending & Descending )

Code starts from next line :
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void main()
{
    int i, j, size, name[20], temp;
    cout<<"\nHow many numbers:";
    cin>>size;

    cout<<"\nEnter the 1 d arrays:";
    
    for(i = 0; i < size; i++)
    {
        cin>>name[i];
    }
    for(i = 0; i < size; i++)
    {
        for(j = 0; j < size-i-1; j++)
        {
            if(name[j] > name[j+1])   //just replace > by < for descending order. 
            {
                temp = name[j];
                name[j] = name[j+1];
                name[j+1] = temp;
            }
        }
    }
    cout<<"\nAccending order is:";
    for(i = 0; i < size; i++)
    {
        cout<<" "<<name[i];
    }
    getch();
}




Call by Reference


The code starts from next line :
---------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
void swap(int &a,int &b);
void main()
{
clrscr();
int a,b;
cout<<"\nEnter the value of a:";
cin>>a;
cout<<"\nEnter the value of b:";
cin>>b;
swap(a,b);
cout<<"\nReplace value of a:"<<a;
cout<<"\nReplace dvalue of b:"<<b;
getch();
}
void swap(int &a,int &b)                                                                       // Function starts here
{
int c;
c=a;                                                                                                     //Swapping code
a=b;
b=c;

}