Computer Program File class 12

Preview:

DESCRIPTION

20 computer programs

Citation preview

Manav Singh

XII B

2015-2016

Program File

INDEXS No. Program1 To find minimum and maximum

number in the array2 To reverse a string

3 To count the number of words

4 To count the number of vowels

5 To search an element in an array6 To compare two strings

7 To do the catenation of two strings8 To transpose a matrix9 To find the right diagonal sum of a

2-D matrix10 To find the left diagonal sum of a

2-D matrix

11 To count the number of 2-digit entries in a 2-D array

12 To find the sum of rows in a 2-D array

13 To find the sum of columns in a

2-D array14 To write contents in a text file

15 To read from text file and display its contents

16 To copy the contents in another file

17 Static Stack

18 Dynamic Stack19 Static Queue

20 Dynamic Queue

21 Armstrong Number

1. To find minimum and maximum number in the array

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int a[20];

int i,n,min,max;

cout<<"Enter The Number Of Elements:->";

cin>>n;

for(i=0;i<n;i++)

{

cin>>a[i];

}

cout<<"Your Entered Array Is:->";

for(i=0;i<n;i++)

{

cout<<a[i];

}

for(i=0;i<n;i++)

{

max=a[0];

if(a[i]>max)

max=a[i];

}

cout<<"\nMaximum Number Entered Is:->"<<max;

for(i=0;i<n;i++)

{

min=a[0];

if(a[i]<min)

min=a[i];

}

cout<<"\nMinimum Number Entered Is:->"<<min;

getch();

return 0;

}

2. To reverse a string

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

int main()

{

clrscr();

char srcstr[30],desstr[30];

int i,len;

cout<<"Enter The String:->";

gets(srcstr);

len=strlen(srcstr);

for(i=0;srcstr[i]!='\0';i++)

{

desstr[--len]=srcstr[i];

}

desstr[i]='\0';

cout<<"Reversed String Is:->";

puts(desstr);

getch();

return 0;

}

3. To count the number of words

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int s=0,j;

char a[75];

cout<<"Enter A String:->";

gets(a);

int l=strlen(a);

for(j=0;j<l;j++)

{

if(a[j]==' ')

s=s+1;

}

cout<<"Number Of Words In The String Are:->"<<s+1;

getch();

return0;

}

4. To count the number of vowels

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

int main()

{

clrscr();

char a[25];

int f=0,i;

cout<<"Enter A String:->";

gets(a);

for(i=0;a[i]!='\0';i++)

{

if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')

f++;

}

{

if(a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')

f++;

}

cout<<"Total Number Of Vowels Are:->"<<f;

getch();

return 0;

}

5. To search an element in an array#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int a[100],n,found=0,i,find;

cout<<"Enter The Number Of Elements:->";

cin>>n;

cout<<"Enter Your Array:->";

for(i=0;i<n;++i)

{

cin>>a[i];

}

cout<<"Enter A Number To Find:->";

cin>>find;

for(i=0;i<n;++i)

{

if(a[i]==find)

{

found=1;

cout<<find<<" Found At Position "<<i+1;

}

}

if(found==0)

cout<<"Sorry!"<<find<<" Not Found";

getch();

return 0;

}

6. To compare two strings#include<iostream.h>

#include<conio.h>

#include<stdio.h>

void main()

{

clrscr();

char str1[30],str2[30];

int l1=0,l2=0,i=0,flag=0;

cout<<"Enter 1st String :->";

gets(str1);

while(str1[l1]!='\0')

{

l1++;

}

cout<<"\nEnter 2nd String:->";

gets(str2);

while(str2[l2]!='\0')

{

l2++;

}

if(l2!=l1)

{

cout<<"Strings Are Not Equal";

}

else

{

for(i=0;i<l1;i++)

{

if(str1[i]!=str2[i])

{

flag=1;

break;

}

}

if(flag==1)

{

cout<<"Strings Are Not Equal";

}

else

{

cout<<"Strings Are Equal";

}

}

getch();

return0;

}

7. To do the catenation of two strings#include<iostream.h>

#include<conio.h>

#include<stdio.h>

int main()

{

clrscr();

char str1[30],str2[30];

int l1=0,l2=0,i=0,flag=0;

cout<<"Enter 1st String:->";

gets(str1);

while(str1[l1]!='\0')

{

l1++;

}

cout<<"Enter 2nd String:->";

gets(str2);

while(str2[l2]!='\0')

{

l2++;

}

for(i=0;i<l2;i++)

{

str1[l1+i]=str2[i];

}

str1[l1+l2]='\0';

cout<<"The Concatenated String Is:->";

puts(str1);

getch();

return 0;

}

8. To transpose a matrix#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int a[10][10],m,n,i,j;

cout<<"Enter Number Of Rows:->";

cin>>m;

cout<<"Enter Number Of Columns:->";

cin>>n;

if(m!=n)

{

cout<<"Matrix Not Square So Transpose Not Possible:->";

}

else

{

cout<<"Enter Elements Of Matrix:->";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

cin>>a[i][j];

}

}

}

cout<<"Entered Elements:->"<<"\n";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

cout<<a[i][j]<<" ";

}

cout<<"\n";

}

cout<<"Transposed Matrix:->"<<"\n";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

cout<<a[j][i]<<" ";

}

cout<<"\n";

}

getch();

return 0;

}

9. To find the Right diagonal sum of a 2-D matrix#include<iostream.h>

#include<conio.h>

void Diagonal(int a[][20],int n)

{

int l,Rdiag;

for(l=0,Rdiag=0;l<n;l++)

{

Rdiag+=a[l][l];

}

cout<<"\nRight Diagonal Sum:->"<<Rdiag;

}

void main()

{

clrscr();

int x[20][20],i,j,m,n;

cout<<"\nEnter The Number Of Rows And Columns:->";

cin>>m;

cout<<"\nEnter The Elements Of The Array:->";

for(i=0;i<m;i++)

{

for(j=0;j<m;j++)

{

cin>>x[i][j];

}

}

cout<<"Entered Elements:->"<<"\n";

for(i=0;i<m;i++)

{

for(j=0;j<m;j++)

{

cout<<x[i][j]<<" ";

}

cout<<"\n";

}

Diagonal(x,m);

getch();

return0;

}

10. To find the Left diagonal sum of a 2-D matrix#include<iostream.h>

#include<conio.h>

void Diagonal(int a[][20],int n)

{

int l,Ldiag;

for(l=0,Ldiag=0;l<n;l++)

{

Ldiag+=a[n-l-1][l];

}

cout<<"\nLeft Diagonal Sum:->"<<Ldiag;

}

void main()

{

clrscr();

int x[20][20],i,j,m,n;

cout<<"\nEnter The Number Of Rows And Columns:->";

cin>>m;

cout<<"\nEnter The Elements Of The Array:->";

for(i=0;i<m;i++)

{

for(j=0;j<m;j++)

{

cin>>x[i][j];

}

}

cout<<"Entered Elements:->"<<"\n";

for(i=0;i<m;i++)

{

for(j=0;j<m;j++)

{

cout<<x[i][j]<<" ";

}

cout<<"\n";

}

Diagonal(x,m);

getch();

return0;

}

11. To count the number of 2-digit entries in a 2-D array#include<iostream.h>

#include<conio.h>

void twodigit(int a[10][10],int m,int n)

{

clrscr();

int i,j,flag=0;

cout<<"\nThe Two Digit Numbers Are:->";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

if(a[i][j]>=10&&a[i][j]<=99)

{

cout<<a[i][j]<<"\n";

flag=1;

}

}

}

if(flag==0)

{

cout<<"None";

}

}

void main()

{

clrscr();

int a[10][10],i,j,m,n;

cout<<"\nEnter Number Of Rows:->";

cin>>m;

cout<<"\nEnter Number Of Columns:->";

cin>>n;

cout<<"\nEnter The Elements Of The Array:->";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

cin>>a[i][j];

}

}

twodigit(a,m,n);

getch();

return0;

}

12. To find the sum of rows in a 2-D array#include<iostream.h>

#include<conio.h>

void sumrow(int a[][20],int m,int n)

{

for(int r=0;r<m;r++)

{

int sumr=0;

for(int c=0;c<n;c++)

{

sumr+=a[r][c];

}

cout<<"\nRow ("<<r+1<<"):->"<<sumr;

}

}

void main()

{

clrscr();

int x[20][20],i,j,m,n;

cout<<"\nEnter Number Of Rows:->";

cin>>m;

cout<<"\nEnter Number Of Columns:->";

cin>>n;

cout<<"\nEnter The Elements Of The Array:->";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

cin>>x[i][j];

}

cout<<"Entered Elements:->\n";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

cout<<x[i][j]<<" ";

}

cout<<"\n";

}

sumrow(x,m,n);

getch();

return0;

}

13. To find the sum of columns in a 2-D array

#include<iostream.h>

#include<conio.h>

void sumcol(int a[][20],int m,int n)

{

for(int c=0;c<n;c++)

{

int sumc=0;

for(int r=0;r<m;r++)

{

sumc+=a[r][c];

}

cout<<"\nColumn ("<<c+1<<"):->"<<sumc;

}

}

void main()

{

clrscr();

int x[20][20],i,j,m,n;

cout<<"\nEnter Number Of Rows:->";

cin>>m;

cout<<"\nEnter Number Of Columns:->";

cin>>n;

cout<<"\nEnter The Elements Of The Array:->";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

cin>>x[i][j];

}

cout<<"Entered Elements:->\n";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

cout<<x[i][j]<<" ";

}

cout<<"\n";

}

sumcol(x,m,n);

getch();

return0;

}

14. To write contents in a text file#include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

ofstream fout;

fout.open("out.txt");

char str[300]="Time is a great teacher";

fout<<str;

fout.close();

getch();

return0;

}

15. To read from text file and display its contents#include<fstream.h>

#include<conio.h>

void main()

{

ifstream fin;

fin.open("out.txt);

char ch;

while(!fin.eof())

{

fin.get(ch);

cout<<ch;

}

fin.close();

getch(); return0;}

16. To copy the contents in another file#include<fstream.h>

#include<conio.h>

void main()

{

ifstream fin;

fin.open("out.txt");

ofstream fout;

fout.open("sample.txt");

char ch;

while(!fin.eof())

{

fin.get(ch);

fout<<ch;

}

fin.close();

getch();

return 0;

}

17. Static Stack#include<iostream.h>

#include<conio.h>

#define size 4

class stack

{

private:

int data[size];

int top;

public:

stack()

{

top=-1;

}

void push();

void pop();

void display();

};

void stack::push()

{

if(top==size-1)

{

cout<<"\nStack Is Full";

return;

}

else

{

top++;

cout<<"Enter Data:->";

cin>>data[top];

}

}

void stack::pop()

{

if(top==-1)

{

cout<<"\nStack Is Empty";

}

else

{

cout<<data[top]<<"deleted"<<endl;

top--;

}

}

void stack::display()

{

int t=top;

while(t>=0)

{

cout<<data[t]<<endl;

t--;

}

}

void main()

{

stack st;

int ch;

do

{

cout<<"\n1.Push";

cout<<"\n2.Pop";

cout<<"\n3.Display";

cout<<"\n4.Quit";

cout<<"\nEnter Choice(1-4)";;

cin>>ch;

switch(ch)

{

case 1:

st.push();

break;

case 2:

st.pop();

break;

case 3:

st.display();

}

}

while(ch!=4);

getch();

return0;

}

18. Dynamic Stack#include<iostream.h>

#include<conio.h>

struct node

{

int data;

node *next;

};

class stack

{

node *top;

public:

stack()

{

top=NULL;

}

void push();

void pop();

void display();

~stack();

};

void stack::pop()

{

if(top!=NULL)

{

node *temp=top;

top=top->next;

cout<<temp->data<<"deleted";

delete temp;

}

else

{

cout<<"Stack Empty";

getch();

}

}

void stack::display()

{

node *temp=top;

while(temp!=NULL)

{

cout<<temp->data<<" ";

temp=temp->next;

}

getch();

}

stack::~stack()

{

while(top!=NULL)

{

node *temp=top;

top=top->next;

delete temp;

}

}

void main()

{

stack st;

int ch;

do

{

clrscr();

cout<<"\n1.Push";

cout<<"\n2.Pop";

cout<<"\n3.Display";

cout<<"\n4.Quit";

cout<<"\nEnter Your Choice:->";

cin>>ch;

switch(ch)

{

case 1:

st.push();

break;

case 2:

st.pop();

break;

case 3:

st.display();

break;

}

}

while(ch!=4);

getch();

return0;

}

19. Static Queue#include<iostream.h>

#include<conio.h>

class queue

{

private:

int queue1[5];

int rear,front;

public:

queue()

{

rear=-1;

front=-1;

}

void insert(int x)

{

if(rear>100)

{

cout<<"Queue Over Flow";

front=rear=-1;

return;

}

queue1[++rear]=x;

cout<<"Inserted"<<x;

getch();

}

void delet()

{

if(front==rear)

{

cout<<"Queue Under Flow";

return;

}

cout<<"Deleted"<<queue1[++front];

getch();

}

void display()

{

if(rear==front)

{

cout<<"Queue Empty";

return;

}

for(int i=front+1;i<=rear;i++)

cout<<queue1[i]<<" ";

getch();

}

};

void main()

{

clrscr();

int ch;

queue qu;

while(1)

{

clrscr();

cout<<"\n1.Insert";

cout<<"\n2.Delete";

cout<<"\n3.Display";

cout<<"\n4.Exit";

cout<<"\nEnter Your Choice";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\nEnter The Elements:->";

cin>>ch;

qu.insert(ch);

cout<<"\n\n\n";

break;

case 2:

qu.delet();

cout<<"\n\n\n";

break;

case 3:

qu.display();

cout<<"\n\n\n";

break;

case 4:

exit(0);

}

}

getch();

return 0;

}

20.Dynamic Queue#include<iostream.h>

#include<conio.h>

struct node

{

int data;

node *next;

};

class queue

{

node *rear,*front;

public:

queue()

{

rear=NULL;

front=NULL;

}

void qinsert();

void qdelete();

void qdisplay();

~queue();

};

void queue::qinsert()

{

node *temp;

temp=new node;

cout<<"Data";

cin>>temp->data;

temp->next=NULL;

if(rear==NULL)

{

rear=temp;

front=temp;

}

else

{

rear->next=temp;

rear=temp;

}

}

void queue::qdelete()

{

if(front!=NULL)

{

node *temp=front;

cout<<front->data<<"Deleted\n";

front=front->next;

delete temp;

if(front==NULL)

{

rear=NULL;

}

else

{

cout<<"Queue Empty";

}

void queue::qdisplay()

{

node *temp=front;

while(temp!=NULL)

{

cout<<temp->data<<endl;

temp=temp->next;

}

}

queue::~queue()

{

while(front!=NULL)

{

node *temp=front;

front=front->next;

delete temp;

}

}

void main()

{

clrscr();

queue obj;

char ch;

do

{

cout<<"\ni.Insert";

cout<<"\nd.Delete";

cout<<"\ns.Display";

cout<<"\nq.Quit";

cin>>ch;

switch(ch)

{

case 'i':

obj.qinsert();

break;

case 'd':

obj.qdelete();

break;

case 's':

obj.qdisplay();

}

}

while(ch!='q');

getch();

return 0;

}

21. Armstrong Number#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int a,b,c,d,e;

cout<<"Enter The Limit:->";

cin>>b;

for(a=1;a<=b;a++)

{

c=a/100;

d=a/10-c*10;

e=a%10;

if(c*c*c+d*d*d+e*e*e==a)

cout<<a<<"\n";

}

getch();

return 0;

}