70
Program File 2015- 2016 Manav Singh XII B

Computer Program File class 12

Embed Size (px)

DESCRIPTION

20 computer programs

Citation preview

Page 1: Computer Program File class 12

Manav Singh

XII B

2015-2016

Program File

Page 2: Computer Program File class 12

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

Page 3: Computer Program File class 12

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>

Page 4: Computer Program File class 12

#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++)

Page 5: Computer Program File class 12

{

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

Page 6: Computer Program File class 12

#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);

Page 7: Computer Program File class 12

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);

Page 8: Computer Program File class 12

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;

}

Page 9: Computer Program File class 12

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++)

Page 10: Computer Program File class 12

{

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;

}

Page 11: Computer Program File class 12

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:->";

Page 12: Computer Program File class 12

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;

}

Page 13: Computer Program File class 12

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')

Page 14: Computer Program File class 12

{

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)

{

Page 15: Computer Program File class 12

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()

Page 16: Computer Program File class 12

{

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];

Page 17: Computer Program File class 12

}

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:->";

Page 18: Computer Program File class 12

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";

Page 19: Computer Program File class 12

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;

Page 20: Computer Program File class 12

}

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()

Page 21: Computer Program File class 12

{

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++)

Page 22: Computer Program File class 12

{

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

}

cout<<"\n";

}

Diagonal(x,m);

getch();

return0;

}

Page 23: Computer Program File class 12

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();

Page 24: Computer Program File class 12

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]<<" ";

Page 25: Computer Program File class 12

}

cout<<"\n";

}

Diagonal(x,m);

getch();

return0;

}

Page 26: Computer Program File class 12

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";

Page 27: Computer Program File class 12

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;

Page 28: Computer Program File class 12

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;

}

Page 29: Computer Program File class 12

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()

Page 30: Computer Program File class 12

{

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++)

Page 31: Computer Program File class 12

{

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

}

cout<<"\n";

}

sumrow(x,m,n);

getch();

return0;

}

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

Page 32: Computer Program File class 12

#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();

Page 33: Computer Program File class 12

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]<<" ";

Page 34: Computer Program File class 12

}

cout<<"\n";

}

sumcol(x,m,n);

getch();

return0;

}

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

#include<conio.h>

void main()

{

Page 35: Computer Program File class 12

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()

{

Page 36: Computer Program File class 12

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");

Page 37: Computer Program File class 12

ofstream fout;

fout.open("sample.txt");

char ch;

while(!fin.eof())

{

fin.get(ch);

fout<<ch;

}

fin.close();

getch();

return 0;

}

Page 38: Computer Program File class 12

17. Static Stack#include<iostream.h>

#include<conio.h>

#define size 4

class stack

{

private:

int data[size];

int top;

public:

stack()

Page 39: Computer Program File class 12

{

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];

Page 40: Computer Program File class 12

}

}

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)

Page 41: Computer Program File class 12

{

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)

{

Page 42: Computer Program File class 12

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>

Page 43: Computer Program File class 12

#include<conio.h>

struct node

{

int data;

node *next;

};

class stack

{

node *top;

public:

stack()

{

top=NULL;

}

void push();

void pop();

void display();

~stack();

};

Page 44: Computer Program File class 12

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)

Page 45: Computer Program File class 12

{

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;

Page 46: Computer Program File class 12

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;

Page 47: Computer Program File class 12

case 3:

st.display();

break;

}

}

while(ch!=4);

getch();

return0;

}

Page 48: Computer Program File class 12

19. Static Queue#include<iostream.h>

#include<conio.h>

class queue

{

private:

int queue1[5];

int rear,front;

public:

queue()

{

rear=-1;

front=-1;

Page 49: Computer Program File class 12

}

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;

Page 50: Computer Program File class 12

}

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()

{

Page 51: Computer Program File class 12

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);

Page 52: Computer Program File class 12

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;

Page 53: Computer Program File class 12

}

20.Dynamic Queue#include<iostream.h>

#include<conio.h>

Page 54: Computer Program File class 12

struct node

{

int data;

node *next;

};

class queue

{

node *rear,*front;

public:

queue()

{

rear=NULL;

front=NULL;

}

void qinsert();

void qdelete();

void qdisplay();

~queue();

};

Page 55: Computer Program File class 12

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()

Page 56: Computer Program File class 12

{

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)

Page 57: Computer Program File class 12

{

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;

Page 58: Computer Program File class 12

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();

}

Page 59: Computer Program File class 12

}

while(ch!='q');

getch();

return 0;

}

Page 60: Computer Program File class 12

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";

Page 61: Computer Program File class 12

}

getch();

return 0;

}