Sunday, March 1, 2015

PRACTICAL PROGRAMMES

                                                       
                                                           COMPUTER SCIENCE
                                                   (PRACTICAL PROGRAMMES)

1)      Programme to enter three digit number and display it in words.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum,num,rev=0,rem,i,len;
int count1=0,count2=0;
cout<<" enter a three digit number ";
cin>>num;
for(i=num;i>0;i=i/10)
{
rem=i%10;
rev=rev*10+rem;
count1=count1+1;
}
for(i=rev;i>0;i=i/10)
{
count2=count2+1;
rem=i%10;
switch(rem)
{
case 1:
cout<<"\t one ";
break;
case 2:
cout<<"\t two ";
break;
case 3:
cout<<"\t three ";
break;
case 4:
cout<<"\t four ";
break;
case 5:
cout<<"\t five ";
break;
case 6:
cout<<"\t six ";
break;
case 7:
cout<<"\t seven ";
break;
case 8:
cout<<"\t eight ";
break;
case 9:
cout<<"\t nine ";
break;
}}
len=count1-count2;
for(i=0;i<len;i++)
{
cout<<"\t zero";
}
getch();
}

2)      Programme to find given number is prime or not.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i,flag=0;
cout<<"enter a number ";
cin>>num;
for(i=2;i<=num/2;++i)
{
  if(num%i==0)
  {
  flag=1;
  break;
  }
  }
  if(flag==1||num==0||num==1)
  {
  cout<<"the number is not prime" ;
  }
  else
  {
  cout<<"the number is prime";
  }

getch();
}
3)      Programme to check whether the string is palindrome or not.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int big,end,i,length,flag=0;
char a[100];
cout<<"enter a string";
cin>>a;
length=strlen(a);
end=length-1;
for(big=0;big<=end;big++)
{
  if(a[big]!=a[end])
   {
   flag=1;
   break;
   }
   else
   {
   end=end-1;
   }
}
if(flag==1)
{
cout<<"string is not paliandrome";
}
else
{
cout<<"string is paliandrome";
}
getch();
}
4)      Programme to find smallest and largest number using array.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,j,limit,temp;
cout<<"enter a limit";
cin>>limit;
cout<<"enter "<<limit<<"numbers";
for(i=0;i<limit;i++)
{
cin>>a[i];
}
for(i=0;i<limit-1;i++)
{
 for(j=j+1;j<limit;j++)
 {
   if(a[i]>a[j])
   {
   temp=a[i];
   a[i]=a[j];
   a[j]=temp;
   }
 }
}
cout<<"largest number in arrey"  ;
cout<<"\t"<<a[limit-1];
cout<<"smallest number in arrey";
cout<<"\t"<<a[0];
getch();
}
5)      Programme to sort the given number in ascenting order.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,j,limit,temp;
cout<<"enter a limit";
cin>>limit;
cout<<"enter "<<limit<<"numbers";
for(i=0;i<limit;i++)
{
cin>>a[i];
}
      for(i=0;i<limit-1;i++)
       {
            for(j=i+1;j<limit;j++)
              {
                      if(a[i]>a[j])
                        {
                      temp=a[i];
                      a[i]=a[j];
                      a[j]=temp;
                         }
                 }
          }
cout<<"arrey after sorting";
for(i=0;i<limit;i++)
{
cout<<"\t"<<a[i];
}
getch();
}
6)      Decimal to binary

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int binnum[100],decimal,i,rem,num;
cout<<"enter a decimal number";
cin>>num;
i=0;
for(decimal=num;decimal>0;decimal=decimal/2)
{
rem=decimal%2;
binnum[i]=rem;
i=i+1;
}
cout<<"binary equivalent is";
if(num==0)
{
cout<<"0";
}
else
{
  for(int j=i-1;j>=0;j--)
  {
  cout<<binnum[j];
  }
}
getch();
}
7)      Amstrong or not.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int num,sum=0,rev,val,rem;
cout<<"enter the number ";
cin>>num;
val=num;
while(num!=0)
{
rem=num%10;
sum=sum+pow(rem,3);
num=num/10;
}
if(val==sum)
{
cout<<"the given number "<<"\t"<<val<<"amstrong";
}
else
{
 cout<<"the given number "<<"\t"<<val<<"not amstrong";
 }
 getch();
 }
8)      Sum of elements in matrix.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100][100],i,j,row,col,sum=0;
cout<<"enter the row and col size";
cin>>row>>col;
cout<<"enter the elements of metrix";
for(i=0;i<row;i++)
{
  for(j=0;j<col;j++)
  {
  cin>>a[i][j];
  }
}
for(i=0;i<row;i++)
{
  for(j=0;j<col;j++)
  {
  sum=sum+a[i][j];
  }
}
cout<<"sum of elemebts"<<sum;
getch();
}
9)      Factorial of a number using recursive function.

#include<iostream.h>
#include<conio.h>
long factorial(long);
void main()
{
clrscr();
long int n,result;
cout<<"enter a number " ;
cin>>n;
result=factorial(n);
cout<<"factorial is "<<result;
getch();
}
long factorial(long a)
{
long int fact;
    if(a<=1)
     {
     return 1;
     }
     else
     {
     fact=a*factorial(a-1);
     return(fact);
     }
}
10)    Sum of diagonal elements.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,sum=0,a[10][10],j,row,col;
cout<<"\n enter the number of row and col";
cin>>row>>col;
cout<<"enter the elements for"<<row<<"x"<<col<<"matrix";
for(i=0;i<row;i++)
{
  for(j=0;j<col;j++)
  {
  cin>>a[i][j];
  }
}
for(i=0;i<row;i++)
{
  for(j=0;j<col;j++)
  {
  if(i==j)
  {
  sum=sum+a[i][j];
  }

  }
}
cout<<"sum of diagonal elemebts "<<sum;
getch();
}
11)   Swapping using pointer.

#include<iostream.h>
#include<conio.h>
void swap(int*,int*);
void main()
{
clrscr();
int a,b;
cout<<"enter two numbers ";
cin>>a>>b;
cout<<"before swapping"<<"a="<<a;
cout<<"b="<<b;
swap(&a,&b);
getch();
}
void swap(int *p1,int *p2)
{
int *t;
t=p1;
p1=p2;
p2=t;
cout<<"after swapping"<<"a="<<*p1<<"b="<<*p2;
}
12)   Add two matrix.

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

class matrix
{
public:
int i,j,row,col,a[10][10],b[10][10],c[10][10];
void getdata();
void sumdata();
void putdata();
};
void matrix :: getdata()
{
cout<<"enter the size of row and col";
cin>>row>>col;
cout<<"enter the values for first "<<col<<"x"<<row<<"matrix" ;
for(i=0;i<row;i++)
{
   for(j=0;j<col;j++)
     {
     cin>>a[i][j];
     }
}
cout<<"enter the values for second "<<col<<"x"<<row<<"matrix" ;
for(i=0;i<row;i++)
{
   for(j=0;j<col;j++)
     {
     cin>>b[i][j];
     }
}
}

void matrix :: sumdata()
{
cout<<"sum of metrix is ";

for(i=0;i<row;i++)
{
   for(j=0;j<col;j++)
     {
     c[i][j]=a[i][j]+b[i][j];

     }
}
}
void matrix ::putdata()
{
for(i=0;i<row;i++)
{
   cout<<"\n";

   for(j=0;j<col;j++)
     {
     cout<<c[i][j]<<"\t";
     }
}

}
void main()
{
clrscr();
matrix obj;
obj.getdata();
obj.sumdata();
obj.putdata();
getch();
}
13)   Subtract two matrix.

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

class matrix
{
public:
int i,j,row,col,a[10][10],b[10][10],c[10][10];
void getdata();
void subdata();
void putdata();
};
void matrix :: getdata()
{
cout<<"enter the size of row and col";
cin>>row>>col;
cout<<"enter the values for first "<<col<<"x"<<row<<"matrix" ;
for(i=0;i<row;i++)
{
   for(j=0;j<col;j++)
     {
     cin>>a[i][j];
     }
}
cout<<"enter the values for second "<<col<<"x"<<row<<"matrix" ;
for(i=0;i<row;i++)
{
   for(j=0;j<col;j++)
     {
     cin>>b[i][j];
     }
}
}
void matrix :: subdata()
{
cout<<"sum of metrix is ";

for(i=0;i<row;i++)
{
   for(j=0;j<col;j++)
     {
     c[i][j]=a[i][j]-b[i][j];

     }
}
}
void matrix ::putdata()
{
for(i=0;i<row;i++)
{
   cout<<"\n";

   for(j=0;j<col;j++)
     {
     cout<<c[i][j]<<"\t";
     }
}

}
void main()
{
clrscr();
matrix obj;
obj.getdata();
obj.subdata();
obj.putdata();
getch();
}
14)   Product of two matrix.

#include<iostream.h>
#include<conio.h>
class matrix
{
public:
int i,j,k,row1,row2,col1,col2,a[10][10],b[10][10],c[10][10];
void input();
void process();
void output();
};
void matrix :: input()
{
cout<<"enter the size of row and col";
cin>>row1>>col1;
cout<<"enter the values for first "<<col1<<"x"<<row1<<"matrix" ;
for(i=0;i<row1;i++)
{
   for(j=0;j<col1;j++)
     {
     cin>>a[i][j];
     }
}
cout<<"enter the size of row and col";
cin>>row2>>col2;
if(col1==row2)
{

cout<<"enter the values for second "<<col2<<"x"<<row2<<"matrix" ;

for(i=0;i<row2;i++)
{
   for(j=0;j<col2;j++)
     {
     cin>>b[i][j];
     }
}
}

else
{
cout<<"multiplication not possible";
}

}
void matrix :: process()
{
cout<<"sum of metrix is ";

for(i=0;i<row1;i++)
{
   for(j=0;j<col2;j++)
     {
       c[i][j]=0;
       for(k=0;k<row2;k++)
       {
     c[i][j]=c[i][j]+a[i][k]*b[k][j];

     }
}
}
 }
void matrix :: output()
{


for(i=0;i<row1;i++)
{
   cout<<"\n";

   for(j=0;j<col1;j++)
     {
     cout<<c[i][j]<<"\t";
     }
}

}



void main()
{
clrscr();
matrix obj;
obj.input();
obj.process();
obj.output();
getch();
}
15)   Particular character is present in a string.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class character
{
public:
char a,str[50];
int i,flag;
void readdata();
void result();
};
void character::readdata()
{
cout<<"enter a string ";
gets(str);
cout<<"\n enter a character";
cin>>a;
flag=0;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==a)
{
flag=1;
}
}
}
void character::result()
{
if(flag==1)
{
cout<<"character is present";
}
else
{
cout<<"character not present";
}
}
void main()
{
clrscr();
character obj;
obj.readdata();
obj.result();
getch();
}

17)   Program to perform arithmetic operation.

#include<iostream.h>
#include<conio.h>
#include<math.h>
class operations
{
int a;
float b;
float result;
public:
void input();
void sum();
void sub();
void mul();
void div();
};
void operations::input()
{
cout<<"\n enter the numbers";
cin>>a>>b;
}
void operations::sum()
{
result=a+b;
cout<<"sum is"<<result;
}
void operations::sub()
{
result=a-b;
cout<<"difference="<<result;
}
void operations::mul()
{
result=a*b;
cout<<"product="<<result;
}

void operations::div()
{
result=a/b;
cout<<"quotient="<<result;
}
 void main()
 {
 clrscr();
 operations obj ;
 obj.input();
 obj.sum();
 obj.sub();
 obj.mul();
 obj.div();
 getch();
 }

19)   PROGRAMME TO CONVERT TEMPERATURE

#include<iostream.h>
#include<conio.h>
class temperature
{
int temp;
float result;
public:
void input();
void ctof();
void ftoc();
void display();
};
void temperature ::input()
{
cin>>temp;
}
void temperature ::display()
{
cout<<result;
}
void temperature ::ctof()
{
result=(1.8*temp)+32;
}
void temperature ::ftoc()
{
result=(temp-32)/1.8;
}
void main()
{
clrscr();
int choice;
char ch;
temperature obj;
do
{
    cout<<"\n 1: farenheit to celcius";
    cout<<"\n 2:  celcius to farenhiet";
    cout<<"\n choose your choice 1 or 2";
    cin>>choice;
      if(choice==1)
      {
      cout<<"enter temperature in f";
      obj.input();
      obj.ftoc();
      cout<<"temperature in c is :";
      obj.display();
      }
      else
      {
     cout<<"enter temperature in c";
      obj.input();
      obj.ctof();
      cout<<"temperature in f is :";
      obj.display();
      }
   cout<<"\n do you want to continue ,   Y or N";
   cin>>ch;
   }
   while(ch=='y'||ch=='Y');
   getch();
   }

No comments:

Post a Comment