Professional Documents
Culture Documents
Computer Science Practical
Computer Science Practical
Computer Science Practical
PRACTICAL
1.PROGRM USING FUNCTION TO MAKE A CALCULATOR
#include<iostream.h>
#include<conio.h>
void showchoice();
float add(float,float);
float sub(float,float);
float mul(float,float);
float div(float,float);
void main()
{
clrscr();
float x,y;
int ch;
showchoice();
cin>>ch;
cout<<"\nenter two numbers ";
cin>>x>>y;
cout<<endl;
switch(ch)
{
case 1:
cout<<"sum "<<add(x,y);
break;
case 2:
cout<<"difference "<<sub(x,y);
break;
case 3:
cout<<"product "<<mul(x,y);
break;
case 4:
cout<<"division "<<div(x,y);
break;
case 5:
break;
default:
cout<<"invalid choice";
}
getch();
}
void showchoice()
{
cout<<"\n\nmenu";
cout<<"\n1: addition";
cout<<"\n2: subtraction";
cout<<"\n3: multiplication";
cout<<"\n4: division";
cout<<"\n5: exit";
cout<<"\nenter your choice ";
}
float add(float a, float b)
{
return (a+b);
}
float sub(float a, float b)
{
return a-b;
}
float mul(float a, float b)
{
return a*b;
}
float div(float a,float b)
{
return a/b;
}
getch();
}
getch();
}
5.SMALLEST ELEMENT OF A 2-D MATRIX
CH12@Q13
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a[3][3],i,j,min;
for(i=0;i<3;i++)
{
cout<<"\nenter element at row "<<i+1<<": ";
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"\n\nelements of the matrix are ";
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<a[i][j]<<'\t';
}
min=a[0][0];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(min>a[i][j])
min=a[i][j];
cout<<"\nthe smallest element of the matrix is "<<min;
getch();
}
6.SUM OF DIAGONALS OF A 2-D MATRIX
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],i,j,right,left;
left=right=0;
for(i=0;i<3;i++)
{
cout<<"\nenter element at row "<<i+1<<": ";
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"\n\nelements of the matrix are ";
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<a[i][j]<<'\t';
}
for(i=0,j=0;i<3,j<3;i++,j++)
left+=a[i][j];
for(i=0,j=2;i<3,j>=0;i++,j--)
right+=a[i][j];
cout<<"\nsum of the left diagonal of the matrix "<<left;
cout<<"\nsum of the right diagonal of the matrix "<<right;
getch();
}
7.SUM OF ROWS AND COLUMNS OF A 2-D MATRIX
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],i,j,rsum,csum;
for(i=0;i<3;i++)
{
cout<<"\nenter element at row "<<i+1<<": ";
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"\n\nelements of the matrix are ";
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<a[i][j]<<'\t';
}
for(i=0;i<3;i++)
{
rsum=0;
for(j=0;j<3;j++)
rsum+=a[i][j];
cout<<"\nsum of row "<<i+1<<" is "<<rsum;
}
for(i=0;i<3;i++)
{
csum=0;
for(j=0;j<3;j++)
csum+=a[j][i];
cout<<"\nsum of column "<<i+1<<" is "<<csum;
}
getch();
}
8.PROGRAM TO COUNT VOWELS, CONSONANTS, DIGITS, SPACES IN A STRING
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
char string[150];
int i,v,c,d,s,sp;
v=c=d=s=sp=0;
cout<<"enter a line of strings;"<<endl;
cin.getline(string,150);
for(i=0;string[i]!='\0';++i)
{
if(string[i]=='a'
||string[i]=='e'
||string[i]=='i'
||string[i]=='o'
||string[i]=='u'
||string[i]=='A'
||string[i]=='E'
||string[i]=='I'
||string[i]=='O'
||string[i]=='U')
++v;
else if((string[i]>='a'&&string[i]<='z')||(string[i]>='A'&&string[i]<='Z'))
++c;
else if(string[i]>='0'&&c<='9')
++d;
else if(string[i]==' ')
++s;
else
++sp;
}
cout<<"Vowels:"<<v<<endl;
cout<<"Consonants:"<<c<<endl;
cout<<"Digits:"<<s<<endl;
cout<<"White Spaces:"<<s<<endl;
cout<<"Special Characters"<<sp;
getch();
return 0;
}
getch();
getch();
}
getch();
}
getch();
}
getch();
}
getch();
}
void fibonacci()
{
int a,b,s,n;
a=0,b=1,s;
cout<<endl<<"enter total number up to which the series to be generated";
cin>>n;
cout <<"Fibonacci series:"<<a<<","<<b<<",";
for(int i=1;i<=(n-2);i++)
{
s=a+b;
cout<<s<<"," ;
a=b;
b=s;
}
}
getch();
}
getch();
}
getch();
}
getch();
}
getch();
}