0% found this document useful (0 votes)
102 views31 pages

Program Using Input and Output Functions

The document contains C++ code to perform the following tasks: 1. It sorts two input arrays, merges them into a single array, and then sorts the merged array. 2. Functions are used to accept input arrays, sort each array, merge the sorted arrays, and finally sort the merged array. 3. The output shows the sorted individual arrays, merged array, and final sorted merged array.

Uploaded by

Anuj Papneja
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views31 pages

Program Using Input and Output Functions

The document contains C++ code to perform the following tasks: 1. It sorts two input arrays, merges them into a single array, and then sorts the merged array. 2. Functions are used to accept input arrays, sort each array, merge the sorted arrays, and finally sort the merged array. 3. The output shows the sorted individual arrays, merged array, and final sorted merged array.

Uploaded by

Anuj Papneja
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 31

/* Program using input and output functions */

#include<fstream.h> #include<conio.h> #include<iostream.h> void main( ) { ofstream out; out.open("abc"); out<< "hello "; out<<" how r u"; out.close(); out.open("def"); out<<"hi "; out<<"i m fine"; out.close(); ifstream in; char s[50]; in.open("abc"); in.getline(s,50); cout<<s<<endl; in.close(); in.open("def"); in.getline(s,50); cout<<s; in.close(); getch(); }

OUTPUT IS : Hello How r u Hi I m fine Press any key to continue ..

/* Program to read the strings using getline */


#include<conio.h> #include<iostream.h> void main( ) { int size=20; char city[20]; cout<<"enter the city name:"<<endl; cin>>city; cout<<"city name:"<<city<<"<<endl<<endl; cout<<"Enter city name again:"<<endl; cin.getline(city,size); cout<<"City name now:"<<city<<endl<<endl; cout<<"Enter another city name : "<<endl; cin.getline(city,size); cout<<"New city name:"<<city<<endl<<endl; getch( ); }

OUTPUT IS : Enter the name of city: Chandigarh City name : Chandigarh Enter the city now again: City name now: Enter another city name: Delhi New name: Delhi Press any key to continue..

/* Program using file pointers */


#include<conio.h> #include<iostream.h> #include<fstream.h> void main( ) { struct a { int i; char c; }b; b.i=50,b.c='a'; ofstream o; o.open("abc"); o.write((char*)&b,sizeof(b)); o.close(); ifstream i; i.open("abc"); i.read((char*)&b,sizeof(b)); cout<<b.i<<endl<<b.c; i.close(); getch(); } OUTPUT IS : 50 a Press any key to continue.

/* Program to find the sum of the elements of 3*3 matrix */


#include<stdio.h> #include<conio.h> #include<iostream.h> class vijay { public: int a[3][3],b[3][3],i,j,sum; void suri() { cout<<"enter the elements:\n"; sum=0; for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { cin>>a[i][j]; } } for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { sum=sum+a[i][j]; } } cout<<"The sum of the elments is = "<<sum; } }; void main() { clrscr(); vijay obj; obj.suri(); getch(); }

***************************************************************** OUTPUT IS : Enter the elements: 1 2 3 4 5 6 7 8 9 The sum of elements is: 45 Press any key to continue.

/* Program to create a 2-d array and find the position of the value entered */
#include<stdio.h> #include<conio.h> #include<iostream.h> class vijay { public: int a[3][3],b,c,i,j; void suri( ) { c=0; cout<<"enter the elements for the matrix:\n"; for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { cin>>a[i][j]; } } cout<<"enter the value whose positon you want to find:\n"; cin>>b; for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { if(b!=a[i][j]) c=c+1; else cout<<"The position is = "<<c; } } cout<<endl; } };

void main() { clrscr(); vijay obj; obj.suri(); getch(); } ***************************************************************** OUTPUT IS : Enter the elements of a matrix : 1 2 3 4 5 6 7 8 9 Enter the value whose position u wants: 3 The position is: 2 Press any key to continue.

/* Program to implement the diamond pattern */


# include<conio.h> # include<iostream.h> void main() { int num,a,b,c,x,y,i; cout<<"Enter the odd number of lines\t"; cin>>num; y=(num-1)/2; x=(num+1)/2; c=x-1; int lines=0,count=1; while(lines!=x) { a=0; while(a<c) { cout<<" "; a++; } b=0; while(b<count) { cout<<"*"; b++; } count=count+2; lines=lines+1; c=c-1; cout<<"\n"; } int m=0,t=num-2; while(m!=y) { for(i=0;i<=m;i++) { cout<<" "; }

for(int j=0;j<t;j++) { cout<<"*"; } m++; t=t-2; cout<<"\n"; } getch(); } ***************************************************************** OUTPUT IS : Enter the odd number of lines: 9 * *** ***** ******* ********** ******* ***** *** * Press any key to continue.

/* Program to find area of triangle,square and rectangle using switch */


#include<stdio.h> #include<conio.h> #include<iostream.h> class vijay { public: int a,b,l,c; void suri( ) { cout<<"Press 1 to find the area of triangle :<<endl; cout<<"Press 2 to find the area of square :"<<endl; cout<<"Press 3 to find the area of rectangle :<<endl; cout<<"\nEnter your choice :"<<endl; cin>>a; switch(a) { case 1: { cout<<"Enter length of the triangle:"<<endl; cin>>l; cout<<"Enter breadth of the triangle:"<<endl; cin>>b; c=0.5*l*b; cout<<"Area of triangle is = "<<c; break; } case 2: { cout<<"Enter length of the square:"<<endl; cin>>l; c=l*l; cout<<"Area of square is = "<<c; break; }

case 3: { cout<<"Enter length of the rectangle:"<<endl; cin>>l; cout<<"Enter breadth of the rectangle:"<<endl; cin>>b; c=l*b; cout<<"Area of rectangle is = "<<c; break; } default: cout<<"Enter correct choice"; } } }; void main( ) { clrscr( ); vijay obj; obj.suri( ); getch( ); } ***************************************************************** OUTPUT IS: Press 1 to find the area of triangle: Press 2 to find the area of square: Press 3 to find the area of rectangle: Enter your choice: 1 Enter the length of triangle: 12 Breath of triangle: 12 Area is: 72

/* Program to check whether the number is palindrome or not */


#include<stdio.h> #include<conio.h> #include<iostream.h> class vijay { public: int a,b,c,d,e; void suri( ) { cout<<"Enter any number : "; cin>>a; e=a; c=0; while(a>=1) { b=a%10; d=c+b; c=d*10; a=a/10; } if(d==e) { cout<<e<<" is a palindrome number"<<endl; } else { cout<<e<<" is not a palindrome number"<<endl; } } };

void main( ) { clrscr(); vijay obj; obj.suri( ); getch( ); } ***************************************************************** OUTPUT IS : Enter any number: 121 121 is a palindrome number Press any key to continue..

/* Program to sort the elements of an array */


#include<stdio.h> #include<conio.h> #include<iostream.h> class vijay { public: int a[10],b,i,j; void suri( ) { cout<<"Enter the elements of an array : "; cout<<endl; for(i=0;i<=9;i++) cin>>a[i]; for(i=0;i<=9;i++) { for(j=i+1;j<=9;j++) { if(a[i]>a[j]) { b=a[i]; a[i]=a[j]; a[j]=b; } } } cout<<"The sorted order of the elements is:-" <<endl; for(i=0;i<=9;i++) cout<<a[i]<<endl; } };

void main() { vijay obj; obj.suri( ); getch( ); } ***************************************************************** OUTPUT IS : Enter the elements of an array: 12 34 42 56 85 99 12 27 6 1 The sorted elements are: 1 6 12 12 27 34 42 56 85 99 Press any key to continue.

/* Program to concatenate two strings */


#include<stdio.h> #include<conio.h> #include<iostream.h> #include<string.h> class vijay { public: char a[10],b[10],c[20]; int i,j,k,l,m; void suri( ) { cout<<"Enter first string : "; cin>>a; cout<<endl; cout<<"Enter second string : "; cin>>b; cout<<endl; j=strlen(a); k=strlen(b); for(i=0;i<=j;i++) c[i]=a[i]; for(i=0,l=j;l<=j+k;i++,l++) { c[l]=b[i]; } m=strlen(c); for(i=0;i<=m;i++) cout<<c[i]; cout<<endl<<endl; } };

void main() { clrscr( ); vijay obj; obj.suri( ); getch(); } ***************************************************************** OUTPUT IS : Enter the first string: Major Enter the second string: Singh MajorSingh Press any key to continue.

/* Program to write the text into file and read it out */


# include<conio.h> # include<iostream.h> # include<fstream.h> void main() { ofstream suri; suri.open("abc"); suri<<" Hi,myself Vijay Suri,"; suri<<"I am 23 years old"; suri.close(); suri.open("xyz"); suri<<"I am studying in BCA-II,"; suri<<"DAV College"; suri.close(); ifstream vijay; vijay.open("abc"); char s[70]; vijay.getline(s,70); cout<<s<<endl; vijay.close(); vijay.open("xyz"); vijay.getline(s,70); cout<<s<<endl; vijay.close(); getch(); } ***************************************************************** OUTPUT IS : Hi, myself Major Singh, I am Studying in BCA 2nd year in DAV College Press any key to continue.

/* Menu driven program of prime numbers, fiboncci series , armstrong number */


#include<stdio.h> #include<conio.h> #include<iostream.h> #include<process.h> class vijay { public: int a,b,c,d,e,f,i; void suri() { cout<<"Press 1 to find prime numbers : "<<endl; cout<<"Press 2 to create Fibonacci series : "<<endl; cout<<"Press 3 to find Armstrong number : "<<endl; cout<<"Press 4 to exit program : "<<endl; cout<<"\n Enter your choice : "<<endl; cin>>a; switch(a) { case 1: { cout<<"Enter any number: "; cin>>b; for(i=2;i<b;i++) { c=b%i; if(c==0) { cout<<"The number is not prime"; break; } } if(i==b) { cout<<"The number is prime";

} break; } case 2: { cout<<"Enter the limit : "; cin>>b; c=0,d=1; e=c+d; cout<<c<<endl<<d; while(b>c+d) { c=d; d=e; cout<<endl<<e; e=c+d; } break; } case 3: { cout<<"Enter any number\n"; cin>>b; f=b,e=0; while(b>=1) { c=b%10; d=c*c*c; b=b/10; e=e+d; } if(e==f) cout<<"The number is armstrong"; else cout<<"The number is not armstrong"; break; }

case 4: { exit(0); break; } default: cout<<"Enter the correct choice"; } } }; void main() { vijay obj; obj.suri(); getch(); } ***************************************************************** OUTPUT IS : Press 1 to find the prime number: Press 2 to find the Fibonacci series: Press 3 to find the Armstrong number: Enter your choice: 2 Enter the limit: 13 1 1 2 3 5 8 Press any key to continue..

/* Program to sort two arrays, merge them and sort the resultant array */
# include<conio.h> # include<iostream.h> class vijay { public: int a[5],b[5],c[10],i,j,temp; void suri() { cout<<"Enter the first array : "; for(i=0;i<5;i++) cin>>a[i]; } void vsuri() { for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[j]<a[i]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } cout<<endl<<"The sorted array is : <<endl; for(i=0;i<5;i++) { cout<<"\t"; cout<<a[i]; } }

void vs() { cout<<endl<<"Enter the second array : "<<endl; for(j=0;j<5;j++) cin>>b[j]; } void dj() { for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(b[j]<b[i]) { temp=b[i]; b[i]=b[j]; b[j]=temp; } } } cout<<endl<<"The sorted array is : " <<endl; for(i=0;i<5;i++) { cout<<"\t"; cout<<b[i]; } } void vjdj() { cout<<endl<<"After merging output of two arrays"<<endl<<endl; for(i=0,j=0;i<5;i++,j++) { c[j]=a[i]; } for(i=0,j=5;i<5;i++,j++) { c[j]=b[i]; }

for(i=0;i<10;i++) { cout<<c[i]; cout<<"\t"; } } void vj() { for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if(c[j]<c[i]) { temp=c[i]; c[i]=c[j]; c[j]=temp; } } } cout<<endl<<The sorted array is"<<endl<<endl; for(i=0;i<10;i++) { cout<<c[i]; cout<<"\t"; } } }; void main() { vijay obj; obj.suri(); obj.vsuri(); obj.vs(); obj.dj(); obj.vjdj(); obj.vj(); getch();

} OUTPUT IS :enter the first array: 1 3 5 7 9 Sorted array is : 1 3 5 Enter the value of second array: 7 4 6 8 10 8 2 6 9 2

Sorted array is : 2 4 6 After merging output of two: 1 3 5 7 9 Sorted array is: 1 2 3 4 5 Press any key to continue

10 4 7 6 8 8 9 10 10

/* Program to find a given word in a given sentence */


# include<conio.h> # include<iostream.h> # include<fstream.h> class vijay { public: char a[50],b[10],c[10]; int i,j,x; void suri( ) { cout<<"Enter the sentence upto 50 character long : "; cin.getline(a,50); cout<<"\nEnter the word which you want to find : "; cin.getline(b,10); cout<<"\nThe location of the word in the sentence is : "; for(i=0;i<50;i++) { if(b[0]==a[i]) { for(x=0;b[x]!='\n';x++) { if(b[x]!=a[i]) break; } j=i+1; cout<<j<<endl<<endl; } } } }; void main() { vijay obj; obj.suri();

getch(); } OUTPUT IS : Enter the sentence up to 50 characters long: Major Singh Enter the word which you want t find The location of the word is: 5 Press any key to continue..

/* Program to convert decimal no into binary and vice versa */


# include<conio.h> # include<iostream.h> class vijay { public: int num,mun,x; void suri() { cout<<"Enter the number in decimal\t"; cin>>num; cout<<"\nThe number in decimal is\t"; while(num>0) { x=num%2; cout<<x; num=num/2; } cout<<endl<<endl; } }; void main() { vijay obj; obj.suri(); getch(); } ***************************************************************** OUTPUT IS : Enter the number in decimals: 123 The number is binary is: 1101111 Press any key to continue

/* Program to find the sum of 2*2 matrix */


#include<iostream.h> #include<math.h> #include<conio.h> class vijay { public: int a[2][2],b[2][2],c[2][2],i,j; void suri() { cout<<"The value of first matrix is : "<<endl; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cin>>a[i][j]; } } cout<<endl; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<a[i][j]<<"\t"; } cout<<endl; } cout<<endl<<"The value of second matrix is : "<<endl; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cin>>b[i][j]; }

} cout<<endl; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<b[i][j]<<"\t"; } cout<<endl; } cout<<endl<<"The sum of two matrices is : "<<endl<<endl; for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=a[i][j]+b[i][j]; cout<<c[i][j]<<"\t"; } cout<<endl; } cout<<endl; } }; void main() { vijay obj; obj.suri(); getch(); } *****************************************************************

OUTPUT IS : The value of first matrix is: 1 3 The value of second is: 5 7 The sum of matrix is: 6 10

2 4 6 8 8 12

You might also like