The document contains 17 code snippets that use recursion to print patterns or calculate values. Each code snippet includes the code, input/output example, and a brief description of what the code is doing (e.g. calculating factorial, power, Fibonacci series recursively). The patterns include triangles, stars, numbers and letters arranged in triangular fashion. The calculations include factorial, addition, power and Fibonacci series using recursion.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
76 views36 pages
Programs FE C++
The document contains 17 code snippets that use recursion to print patterns or calculate values. Each code snippet includes the code, input/output example, and a brief description of what the code is doing (e.g. calculating factorial, power, Fibonacci series recursively). The patterns include triangles, stars, numbers and letters arranged in triangular fashion. The calculations include factorial, addition, power and Fibonacci series using recursion.
OUTPUT: How many numbers:5 Enter the numbers:34 21 76 3 15 Ascending Order:3 15 21 34 76
//NOTE:For descending order,just reverse > sign i.e.write if(a[j]<a[j+1]) inplace of if(a[j]>a[j+1]) 27) WAP to sort given array of strings Or WAP to sort given strings in ascending Order
#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); int n,i,j; char a[20][30]; char temp[30]; cout<<"How many Strings:"; cin>>n; cout<<"Enter the Strings:"; for(i=0;i<n;i++) { cin>>a[i]; }
OUTPUT: How many Strings:5 Enter the Strings:Ashu Khadye Anu Sona Pinky Ascending Order:Anu Ashu Khadye Pinky Sona //NOTE:For descending order,just reverse > sign i.e.write if(strcmp(a[j],a[j+1])<0) inplace of if(strcmp(a[j],a[j+1])>0)
Ashwini C. Khadye Contact: 9773712416 15
28) WAP to shift array to the right by 1 position
#include<iostream.h> #include<conio.h>
void main() { clrscr(); int a[50],n,i,temp;
cout<<"How many numbers:"; cin>>n; cout<<"Enter the numbers:"; for(i=0;i<=n-1;i++) { cin>>a[i]; }
cout<<"After right shifting:"; for(i=0;i<=n-1;i++) { cout<<a[i]<<" "; } getch(); }
OUTPUT: How many numbers:5 Enter the numbers:10 20 30 40 50 After right shifting:50 10 20 30 40
Ashwini C. Khadye Contact: 9773712416 16
Two Dimension(2-D) Array: 29) WAP to take two matrices of any order and display them void Input(int a[50][50],int m,int n); void Display(int a[50][50],int m,int n);
void main() { clrscr(); int a[50][50],b[50][50],m1,n1,m2,n2;
cout<<"\nEnter order of matrix:"; cin>>m1>>n1; Input(a,m1,n1);
cout<<"\nEnter order of matrix:"; cin>>m2>>n2; Input(b,m2,n2);
Display(a,m1,n1); Display(b,m2,n2);
getch(); }
void Input(int a[50][50],int m,int n) { int i,j; cout<<"\nEnter Elements of matrix:"; for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { cin>>a[i][j]; } } } void Display(int a[50][50],int m,int n) { int i,j; cout<<"\nMARTIX:\n"; for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { cout<<a[i][j]<<" "; } cout<<"\n"; } } Ashwini C. Khadye Contact: 9773712416 17
OUTPUT:
Enter order of matrix:2 2
Enter Elements of matrix:1 2 3 4
Enter order of matrix:3 3
Enter Elements of matrix:5 6 7 8 9 0 1 2 3
MARTIX: 1 2 3 4
MARTIX: 5 6 7 8 9 0 1 2 3
Ashwini C. Khadye Contact: 9773712416 18
30) WAP to perform matrix addition, subtraction, multiplication, transpose operations
cout<<"\nEnter order of matrix:"; cin>>m1>>n1; cout<<"\nEnter Elements of matrix:"; Input(a,m1,n1);
cout<<"\nMARTIX:\n"; Display(a,m1,n1);
if(m1==m2 && n1==n2) { Transpose(a,m1,n1); cout<<"\nTransponse:\n"; Display(a,m1,n1); } else cout<<"Matrix transponse cant be displayed as matrix is not square matrix";
void SumOD(int a[50][50],int m,int n) { int i,j,sum; sum=0; for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { if(i==j) sum=sum+a[i][j]; } } cout<<"\nSum of Diagonal Elements = "<<sum; }
OUTPUT:
Enter order of matrix:3 3
Enter Elements of matrix:1 2 3 4 5 6 7 8 9
MARTIX: 1 2 3 4 5 6 7 8 9
Sum of Diagonal Elements = 15
Ashwini C. Khadye Contact: 9773712416 24
33) WAP that reads any positive integer n and prints the digits in words (For E.g. If input is 1265 then output is one two six Five)
#include<iostream.h> #include<conio.h>
void main() { clrscr(); int n,rem,rev; rev=0; char Digit[10][7]={"Zero","One","Two","Three","Four","Five","Six", "Seven","Eight","Nine"};
cout<<"Enter the number: "; cin>>n;
//Reverse the number while(n!=0) { rem=n%10; rev=(rev*10)+rem; n=n/10; }
//Get unit place,ten place... from reverse number and print it cout<<"In words: "; while(rev!=0) { rem=rev%10; cout<<Digit[rem]<<" "; rev=rev/10; }
getch(); }
OUTPUT: Enter the number: 9756802 In words: Nine Seven Five Six Eight Zero Two
Ashwini C. Khadye Contact: 9773712416 25
34) WAP to accept a month number and display the month name
#include<iostream.h> #include<conio.h>
void main() { clrscr(); int n; char months[12][20]={"January","February","March","April","May","June", "July","October","November","December"};
cout<<"Enter the number[1-12]: "; cin>>n;
cout<<"Month Name: "<<months[n-1]; getch(); }
OUTPUT: Enter the number[1-12]: 3 Month Name: March
Ashwini C. Khadye Contact: 9773712416 26
STRUCTURE: 35) Define structure consisting of following elements i) Employee ID ii) Employee name iii) Employee salary WAP C++ program to read n records and display them
#include<iostream.h> #include<conio.h>
struct Employee { char ename[50]; int eid; float sal; };
void main() { struct Employee E[20]; int n,i;
cout<<"Enter number of Employees:"; cin>>n;
for(i=0;i<=n-1;i++) { cout<<"Enter the Employee name:"; cin>>E[i].ename; cout<<"Enter the Employee id:"; cin>>E[i].eid; cout<<"Enter the Employee salary:"; cin>>E[i].sal; cout<<endl; }
OUTPUT: Enter number of Employees:4 Enter the Employee name:Sheetal Enter the Employee id:1 Enter the Employee salary:5000
Ashwini C. Khadye Contact: 9773712416 27
Enter the Employee name:Vidya Enter the Employee id:2 Enter the Employee salary:10000
Enter the Employee name:Ashwini Enter the Employee id:3 Enter the Employee salary:4000
Enter the Employee name:Niketa Enter the Employee id:4 Enter the Employee salary:15000
Employee name:Sheetal Employee id:1 Salary:5000
Employee name:Vidya Employee id:2 Salary:10000
Employee name:Ashwini Employee id:3 Salary:4000
Employee name:Niketa Employee id:4 Salary:15000
36) Define structure within structure consisting of following elements i) Employee ID ii) Employee name iii) Employee salary WAP C++ program to read n records and display them
void main() { clrscr(); struct Employee E[20]; int n,i; cout<<"Enter number of Employees:"; cin>>n; for(i=0;i<=n-1;i++) { cout<<"Enter the Employee name:"; cin>>E[i].ename; cout<<"Enter the Employee id:"; cin>>E[i].eid; cout<<"Enter the Employee salary:"; cin>>E[i].sal; cout<<"Enter the day of Joining Date:"; cin>>E[i].JDate.day; cout<<"Enter the month of Joining Date:"; cin>>E[i].JDate.month; cout<<"Enter the year of Joining Date:"; cin>>E[i].JDate.year; cout<<endl; }
/*OUTPUT: Enter number of Employees:2 Enter the Employee name:Ashwini Enter the Employee id:201 Enter the Employee salary:15000 Enter the day of Joining Date:1 Enter the month of Joining Date:3 Enter the year of Joining Date:2009
Enter the Employee name:Anita Enter the Employee id:202 Enter the Employee salary:30000 Enter the day of Joining Date:1 Enter the month of Joining Date:5 Enter the year of Joining Date:2006
Employee name:Ashwini Employee id:201 Ashwini C. Khadye Contact: 9773712416 29
Salary:15000 Date of Joining:1/3/2009
Employee name:Anita Employee id:202 Salary:30000 Date of Joining:1/5/2006 */
struct Hocky { char pname[50]; char cname[20]; int nmatches; int ngoals; };
void main() { struct Hocky H[20]; struct Hocky temp; int n,i,j,choice; cout<<"Enter number of players:"; cin>>n;
for(i=0;i<=n-1;i++) { cout<<"Enter the player name:"; cin>>H[i].pname; cout<<"Enter the country name:"; cin>>H[i].cname; cout<<"Enter the number of matches played:"; cin>>H[i].nmatches; cout<<"Enter the number of goals scored:"; cin>>H[i].ngoals; cout<<endl; }
cout<<"\n1.List According to Player name\n2.List According to Country name\n3.List According to number of matches played\n4.List According to number of goals scored";
OUTPUT: Enter number of players:4 Enter the player name:Ashwin Enter the country name:England Enter the number of matches played:15 Enter the number of goals scored:8
Enter the player name:Sachin Enter the country name:India Enter the number of matches played:25 Enter the number of goals scored:23
Enter the player name:Vidya Enter the country name:America Enter the number of matches played:12 Enter the number of goals scored:9
Enter the player name:Niketa Enter the country name:London Enter the number of matches played:20 Enter the number of goals scored:11
Ashwini C. Khadye Contact: 9773712416 32
1.List According to Player name 2.List According to Country name 3.List According to number of matches played 4.List According to number of goals scored Please enter your choice:1
player name:Ashwin country name:England No. of Matches:15 No.Of Goals:8
player name:Niketa country name:London No. of Matches:20 No.Of Goals:11
player name:Sachin country name:India No. of Matches:25 No.Of Goals:23
player name:Vidya country name:America No. of Matches:12 No.Of Goals:9
Ashwini C. Khadye Contact: 9773712416 33
38) WAP to find how many objects of class has been created using static member function