0% 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.

Uploaded by

khadye_ashwini
Copyright
© © All Rights Reserved
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% 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.

Uploaded by

khadye_ashwini
Copyright
© © All Rights Reserved
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
You are on page 1/ 36

Ashwini C.

Khadye Contact: 9773712416


1

1) WAP to print following pattern

*
**
***
****
........
........


#include<iostream.h>
#include<conio.h>

void main()
{
int n,i,j;
clrscr();

cout<<"Enter number of rows:";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cout<<"*";

cout<<"\n";
}

getch();
}


OUTPUT:
Enter number of rows:4
*
**
***
****

2) WAP to print following pattern

*
* *
* * *
* * * *
.......
........


#include<iostream.h>
#include<conio.h>

void main()
{
int n,i,j;
clrscr();

cout<<"Enter number of rows:";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cout<<"* ";

cout<<"\n";
}

getch();
}


OUTPUT:
Enter number of rows:4
*
* *
* * *
* * * *





Ashwini C. Khadye Contact: 9773712416
2


3) WAP to print following pattern

P
P Q
P Q R
P Q R S
.......
........


#include<iostream.h>
#include<conio.h>

void main()
{
int n,i,j;
clrscr();

cout<<"Enter number of rows:";
cin>>n;

char ch='P';
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cout<<char(ch+j-1)<<" ";

cout<<"\n";

}
getch();

}

OUTPUT:
Enter number of rows:4
P
P Q
P Q R
P Q R S





Ashwini C. Khadye Contact: 9773712416
3


4) WAP to print following pattern

........
......
****
***
**
*

#include<iostream.h>
#include<conio.h>

void main()
{
int n,i,j;
clrscr();

cout<<"Enter the value of n:";
cin>>n;

for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
cout<<"*";

cout<<"\n";
}

getch();
}

OUTPUT:
Enter the value of n:4
****
***
**
*

5) WAP to print following pattern

........
......
* * * *
* * *
* *
*
#include<iostream.h>
#include<conio.h>

void main()
{
int n,i,j;
clrscr();

cout<<"Enter number of rows:";
cin>>n;

for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
cout<<"* ";

cout<<"\n";
}

getch();
}

OUTPUT:
Enter number of rows:4
* * * *
* * *
* *
*




Ashwini C. Khadye Contact: 9773712416
4



6) WAP to print following pattern

*
**
***
****

..

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,k;
cout<<"Enter number of rows:";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=n-1;j>=i;j--)
{
cout<<" ";
}

for(k=1;k<=i;k++)
{
cout<<"*";
}

cout<<"\n";
}
getch();
}

OUTPUT:
Enter number of rows:4
*
**
***
****

7) WAP to print following pattern

*
* *
* * *
* * * *
.
.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,k;
cout<<"Enter number of rows:";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=n-1;j>=i;j--)
{
cout<<" ";
}

for(k=1;k<=i;k++)
{
cout<<"* ";
}

cout<<"\n";
}
getch();
}

OUTPUT:
Enter number of rows:4
*
* *
* * *
* * * *

Ashwini C. Khadye Contact: 9773712416
5


8) WAP to print following pattern

*
* *
* * *
* * * *

..

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,k;
cout<<"Enter number of rows:";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=2*n-1;j>=2*i;j--)
{
cout<<" ";
}

for(k=1;k<=i;k++)
{
cout<<"* ";
}

cout<<"\n";
}

getch();
}

OUTPUT:
Enter number of rows:4
*
* *
* * *
* * * *
9) WAP to print following pattern

1
2 2
3 3 3
4 4 4 4
.
.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,k;
cout<<"Enter number of rows:";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=2*n-1;j>=2*i;j--)
{
cout<<" ";
}

for(k=1;k<=i;k++)
{
cout<<i<<" ";
}

cout<<"\n";
}

getch();
}

OUTPUT:
Enter number of rows:4
1
2 2
3 3 3
4 4 4 4


Ashwini C. Khadye Contact: 9773712416
6



10) WAP to print following pattern

*
* * *
* * * * *
* * * * * * *
.
.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,k;
cout<<"Enter number of rows:";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=2*n-1;j>=2*i;j--)
{
cout<<" ";
}

for(k=1;k<=(2*i-1);k++)
{
cout<<"* ";
}

cout<<"\n";
}
getch();
}

OUTPUT:
Enter number of rows:4
*
* * *
* * * * *
* * * * * * *


11) WAP to print following pattern

*
* * *
* * * * *
* * * * * * *
.
.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,k;
cout<<"Enter number of rows:";
cin>>n;

for(i=1;i<=n;i++)
{
for(j=2*n-1;j>=2*i;j--)
{
cout<<" ";
}

for(k=1;k<=(2*i-1);k++)
{
cout<<"* ";
}

cout<<"\n";
}
getch();
}

OUTPUT:
Enter number of rows:4
*
* * *
* * * * *
* * * * * * *








Ashwini C. Khadye Contact: 9773712416
7





12) WAP to print following pattern

A
A B A
A B C B A
A B C D C B A
.
.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,k;
cout<<"Enter number of rows:";
cin>>n;

char ch='A';
for(i=1;i<=n;i++)
{
for(j=2*n-1;j>=2*i;j--)
{
cout<<" ";
}

for(k=1;k<=i;k++)
{
cout<<char(ch+k-1)<<" ";
}

for(k=i-1;k>=1;k--)
{
cout<<char(ch+k-1)<<" ";
}

cout<<"\n";
}

getch();
}

OUTPUT:
Enter number of rows:4
A
A B A
A B C B A
A B C D C B A






13) WAP to print following pattern

1
2 1 A
3 2 1 A B
4 3 2 1 A B C
.
.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,k;
cout<<"Enter number of rows:";
cin>>n;

char ch='A';
for(i=1;i<=n;i++)
{
for(j=2*n-1;j>=2*i;j--)
{
cout<<" ";
}

for(k=i;k>=1;k--)
{
cout<<k<<" ";
}

for(k=1;k<=i-1;k++)
{
cout<<char(ch+k-1)<<" ";
}

cout<<"\n";
}

getch();
}

OUTPUT:
Enter number of rows:4
1
2 1 A
3 2 1 A B
4 3 2 1 A B C

Ashwini C. Khadye Contact: 9773712416
8


Recursion :
14) WAP to calculate factorial of
given number using recursion

#include<iostream.h>
#include<conio.h>

int factorial(int n); //Function Prototype

void main()
{
clrscr();
int n,result;

cout<<"Enter the number:";
cin>>n;

result=factorial(n); //Function Call

cout<<"Factorial of "<<n<<" = "<<result;

getch();
}

//Function Defination
int factorial(int n)
{
if(n == 0)
return 1;
else
return n*factorial(n-1);
}

OUTPUT:
Enter the number:5
Factorial of 5 = 120

15) WAP to calculate addition of first 'n'
number using recursion

#include<iostream.h>
#include<conio.h>

int addition(int n); //Function Prototype

void main()
{
clrscr();
int n,result;

cout<<"Enter the number:";
cin>>n;

result=addition(n); //Function Call

cout<<"Addition of first "<<n<<" nos. = "<<result;

getch();
}

//Function Defination
int addition(int n)
{
if(n==0)
return 0;
else
return n+addition(n-1);
}

OUTPUT:
Enter the number:5
Addition of first 5 nos. = 15




Ashwini C. Khadye Contact: 9773712416
9



16) WAP to calculate x raise to n
using recursion


#include<iostream.h>
#include<conio.h>

int CalPower(int x,int n);

void main()
{
clrscr();
int x,n,result;

cout<<"Enter the base:";
cin>>x;
cout<<"Enter the power:";
cin>>n;

result=CalPower(x,n);

cout<<x<<"^"<<n<<" = "<<result;

getch();
}

int CalPower(int x,int n)
{
if(n==0)
return 1;
else
return x*CalPower(x,n-1);
}

OUTPUT:
Enter the base:2
Enter the power:4
2^4 = 16

17) WAP to display fibonacci series for 'n'
terms using recursion


#include<iostream.h>
#include<conio.h>

int fib(int n);

void main()
{
clrscr();
int n,i;

cout<<"Enter number of terms fibonacci series: ";
cin>>n;

cout<<"\nFibonacci Series:\n";
for(i=0;i<=n-1;i++)
{
cout<<fib(i)<<" ";
}

getch();
}

int fib(int n)
{
if(n == 0 || n == 1)
return n;
else
return fib(n - 1) + fib(n - 2);
}

OUTPUT:
Enter number of terms fibonacci series: 7

Fibonacci Series:
0 1 1 2 3 5 8



Ashwini C. Khadye Contact: 9773712416
10


18) WAP to convert decimal
number(integer number) to binary
number

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,j,rem[100];

cout<<"Enter the number: ";
cin>>n;

i=0;
while(n != 0)
{
rem[i]=n%2;
n = n/2;
i++;
}

cout<<"Binary number: ";
for(j=i-1;j>=0;j--)
cout<<rem[j];
getch();
}

OUTPUT:
Enter the number: 25
Binary number: 11001


19) WAP to reverse number entered
by user


#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,rem,rev=0;

cout<<"Enter the number:";
cin>>n;

while(n!=0)
{
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}

cout<<"Reverse number = "<<rev;
getch();
}


OUTPUT:
Enter the number:123
Reverse number = 321








Ashwini C. Khadye Contact: 9773712416
11



20) WAP to check whether given
number is palindrom or not

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,rem,rev=0;
int dup_n;

cout<<"Enter the number:";
cin>>n;
dup_n=n; //Backup of n

while(n!=0)
{
rem=n%10;
rev=(rev*10)+rem;
n=n/10;
}

if(dup_n == rev)
cout<<dup_n<<" is Palindrom";
else
cout<<dup_n<<" is not Palindrom";

getch();
}

OUTPUT:
Enter the number:121
121 is Palindrom

Enter the number:123
123 is not Palindrom

21) WAP to check whether given
number is armstrong or not

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,rem,rev=0;
int dup_n;

cout<<"Enter the number:";
cin>>n;
dup_n=n; //Backup of n

while(n!=0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}

if(dup_n == rev)
cout<<dup_n<<" is Palindrom";
else
cout<<dup_n<<" is not Palindrom";

getch();
}

OUTPUT:
Enter the number:153
153 is Armstrong number

Enter the number:1234
1234 is not Armstrong number





Ashwini C. Khadye Contact: 9773712416
12


22) WAP to decide whether given
number is prime or not

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,flag;
cout<<"Enter the number:";
cin>>n;

flag=1;
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
flag=0;
break;
}
}

if(flag==1)
cout<<n<<" is prime number";
else
cout<<n<<" is not prime number";

getch();
}

OUTPUT:
Enter the number:5
5 is prime number

Enter the number:100
100 is not prime number


23) WAP to find all prime numbers
from 1 to n.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i,flag,j;
cout<<"Enter the value of n:";
cin>>n;

for(j=2;j<n;j++)
{
flag=1;
for(i=2;i<=j-1;i++)
{
if(j%i==0)
{
flag=0;
break;
}
}

if(flag==1)
cout<<j<<" is prime number\n";
}

getch();
}

OUTPUT:
Enter the value of n:10
2 is prime number
3 is prime number
5 is prime number
7 is prime number




Ashwini C. Khadye Contact: 9773712416
13

24) WAP to calculate GCD and LCM of
two numbers

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b,dup_a,dup_b;
cout<<"Enter the two numbers:";
cin>>a>>b;

dup_a=a;
dup_b=b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}

cout<<"GCD = "<<a<<endl;
//Here a is nothing but GCD of two numbers
cout<<"LCM = "<<(dup_a*dup_b)/a;

getch();
}



OUTPUT:
Enter the two numbers:4 8
GCD = 4
LCM = 8

Enter the two numbers:5 8
GCD = 1
LCM = 40



25) WAP to display fibonacci series for
'n' terms

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i;
int f1,f2,f3;
f1=0;
f2=1;

cout<<"Enter number of terms
for fibonacci series: ";
cin>>n;

cout<<"\nFibonacci Series:\n";
cout<<f1<<" "<<f2<<" ";
for(i=0;i<=n-2;i++)
{
f3=f1+f2;
cout<<f3<<" ";
f1=f2;
f2=f3;
}

getch();
}

OUTPUT:
Enter number of terms for fibonacci series: 7

Fibonacci Series:
0 1 1 2 3 5 8



Ashwini C. Khadye Contact: 9773712416
14

26) WAP to sort given array Or
WAP to sort given numbers in ascending
Order

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[50],n,i,j,temp;


cout<<"How many numbers:";
cin>>n;
cout<<"Enter the numbers:";
for(i=0;i<n;i++)
{
cin>>a[i];
}

for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

cout<<"Ascending Order:";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}

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

for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(a[j],a[j+1])>0)
{
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp);
}
}
}

cout<<"Ascending Order:";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}

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

temp=a[n-1];
for(i=n-2;i>=0;i--)
{
a[i+1]=a[i];
}
a[0]=temp;

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

#include<iostream.h>
#include<conio.h>

void Input(int a[50][50],int m,int n);
void Display(int a[50][50],int m,int n);
void Add(int a[50][50],int b[50][50],int c[50][50],int m,int n);
void Sub(int a[50][50],int b[50][50],int c[50][50],int m,int n);
void Mul(int a[50][50],int b[50][50],int c[50][50],int m,int n,int p);
void Transpose(int a[50][50],int c[50][50],int m,int n);

void main()
{
clrscr();
int a[50][50],b[50][50],c[50][50],m1,n1,m2,n2;

cout<<"\nEnter order of matrix1:";
cin>>m1>>n1;
cout<<"\nEnter Elements of matrix1:";
Input(a,m1,n1);

cout<<"\nEnter order of matrix2:";
cin>>m2>>n2;
cout<<"\nEnter Elements of matrix2:";
Input(b,m2,n2);

cout<<"\nMARTIX1:\n";
Display(a,m1,n1);

cout<<"\nMARTIX2:\n";
Display(b,m2,n2);

if(m1==m2 && n1==n2)
{
Add(a,b,c,m1,n1);
cout<<"\nAddition:\n";
Display(c,m1,n1);
Sub(a,b,c,m1,n1);
cout<<"\nSubtraction:\n";
Display(c,m1,n1);
}
else
cout<<"Matrix addition or subtraction can't be done";

if(n1==m2)
{
Mul(a,b,c,m1,n1,n2);
cout<<"\nMultiplication:\n";
Display(c,m1,n2);
}
else
cout<<"Matrix multiplication can't be done";

Transpose(a,c,m1,n1);
cout<<"\nTransponse:\n";
Display(c,m1,n1);

getch();
}
Ashwini C. Khadye Contact: 9773712416
19



void Input(int a[50][50],int m,int n)
{
int i,j;
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;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}

//Matrix Addtion
void Add(int a[50][50],int b[50][50],int c[50][50],int m,int n)
{
int i,j;

for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j] + b[i][j];
}
}
}

//Matrix Subtraction
void Sub(int a[50][50],int b[50][50],int c[50][50],int m,int n)
{
int i,j;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j] - b[i][j];
}
}
}









Ashwini C. Khadye Contact: 9773712416
20

//Matrix Multiplication
void Mul(int a[50][50],int b[50][50],int c[50][50],int m,int n,int p)
{
int i,j,k;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=p-1;j++)
{
c[i][j] = 0;
for(k=0;k<=n-1;k++)
c[i][j] = c[i][j] + a[i][k]*b[k][j];
}
}
}

//Transponse of matrix
void Transpose(int a[50][50],int c[50][50],int m,int n)
{
int i,j;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[j][i];
}
}
}

OUTPUT:

Enter order of matrix1:2 2

Enter Elements of matrix1:1 2 3 4

Enter order of matrix2:2 2

Enter Elements of matrix2:5 6 4 3

MARTIX1:
1 2
3 4

MARTIX2:
5 6
4 3


Addition:
6 8
7 7

Subtraction:
-4 -4
-1 1

Multiplication:
13 12
31 30

Transponse:
1 3
2 4
Ashwini C. Khadye Contact: 9773712416
21

31) WAP to display transponse of matrix using single 2-D matrix

#include<iostream.h>
#include<conio.h>

void Input(int a[50][50],int m,int n);
void Display(int a[50][50],int m,int n);
void Transpose(int a[50][50],int m,int n);

void main()
{
clrscr();
int a[50][50],m1,n1;

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

getch();
}

void Input(int a[50][50],int m,int n)
{
int i,j;
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;
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
22

void Transpose(int a[50][50],int m,int n)
{
int i,j,temp;
for(i=0;i<=m-1;i++)
{
for(j=i+1;j<=n-1;j++)
{
temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}

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

Transponse:
1 4 7
2 5 8
3 6 9

32) WAP to calculate sum of diagonal elements or trace of matrix

#include<iostream.h>
#include<conio.h>

void Input(int a[50][50],int m,int n);
void Display(int a[50][50],int m,int n);
void SumOD(int a[50][50],int m,int n);

void main()
{
int a[50][50],m1,n1;
clrscr();
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 == n1)
SumOD(a,m1,n1);
else
cout<<"\nMatrix does not form Diagonal";

getch();
}
Ashwini C. Khadye Contact: 9773712416
23


void Input(int a[50][50],int m,int n)
{
int i,j;
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;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}

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


for(i=0;i<=n-1;i++)
{
cout<<"\nEmployee name:"<<E[i].ename;
cout<<"\nEmployee id:"<<E[i].eid;
cout<<"\nSalary:"<<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

#include<iostream.h>
#include<conio.h>

struct SDate
{
int day;
int month;
int year;
};

struct Employee
{
char ename[50];
int eid;
float sal;
struct SDate JDate;
};


Ashwini C. Khadye Contact: 9773712416
28

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


for(i=0;i<=n-1;i++)
{
cout<<"\nEmployee name:"<<E[i].ename;
cout<<"\nEmployee id:"<<E[i].eid;
cout<<"\nSalary:"<<E[i].sal;
cout<<"\nDate of Joining:"<<E[i].JDate.day<<"/"<<
E[i].JDate.month<<"/"<<E[i].JDate.year;
cout<<endl;
}
getch();
}

/*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
*/


37) WAP to create structure Hocky

#include<iostream.h>
#include<conio.h>
#include<string.h>

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

cout<<"\nPlease enter your choice:";
cin>>choice;




Ashwini C. Khadye Contact: 9773712416
30

switch(choice)
{
case 1:
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(H[j].pname,H[j+1].pname)>0)
{
temp=H[j];
H[j]=H[j+1];
H[j+1]=temp;
}
}
}
break;

case 2:
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(H[j].cname,H[j+1].cname)>0)
{
temp=H[j];
H[j]=H[j+1];
H[j+1]=temp;
}
}
}
break;


case 3: for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(H[j].nmatches < H[j+1].nmatches)
{
temp=H[j];
H[j]=H[j+1];
H[j+1]=temp;

}
}
}
break;









Ashwini C. Khadye Contact: 9773712416
31



case 4:
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(H[j].ngoals < H[j+1].ngoals)
{
temp=H[j];
H[j]=H[j+1];
H[j+1]=temp;
}
}
}
break;

default:
cout<<"Invalid Choice";

} //End of switch

for(i=0;i<=n-1;i++)
{
cout<<"\nplayer name:"<<H[i].pname;
cout<<"\ncountry name:"<<H[i].cname;
cout<<"\nNo. of Matches:"<<H[i].nmatches;
cout<<"\nNo.Of Goals:"<<H[i].ngoals;
cout<<endl;
}

}

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

#include<iostream.h>
#include<conio.h>

class Demo
{
public:
static int i;

Demo()
{
i++;
}

static void display()
{
cout<<"Number of objects = "<<i;
}
};

int Demo::i=0;

void main()
{
clrscr();
Demo d1; //i=1
Demo d2; //i=2
Demo d3; //i=3
Demo::display(); //Static member function calling using classname
getch();
}

OUTPUT:
Number of objects = 3















Ashwini C. Khadye Contact: 9773712416
34

39) WAP to find area of circle,area of rectangle and area of triangle
using function or method overloading

#include<iostream.h>
#include<conio.h>
#include<math.h> //Required for sqrt function

class Area
{
public:
void CalArea(float r)
{
cout<<"\nArea of Circle = "<<3.14*r*r;
}


void CalArea(int a,int b,int c)
{
float s;
s=(float)(a+b+c)/2;
cout<<"\nArea of Triangle = "<<sqrt(s*(s-a)*(s-b)*(s-c));
}

void CalArea(int l,int b)
{
cout<<"\nArea of Rectangle = "<<l*b;
}

};

void main()
{
clrscr();
Area a;
a.CalArea(1);
a.CalArea(1,2,3);
a.CalArea(2,3);
getch();
}

OUTPUT:

Area of Circle = 3.14
Area of Triangle = 0
Area of Rectangle = 6








Ashwini C. Khadye Contact: 9773712416
35

40) WAP to find area of circle,area of rectangle and area of triangle
using overloaded constuctor function

#include<iostream.h>
#include<conio.h>
#include<math.h> //Required for sqrt function

class Area
{
public:
Area(float r)
{
cout<<"Area of Circle = "<<3.14*r*r;
}


Area(float b,int h)
{
float s;
s=(float)(a+b+c)/2;
cout<<"Area of Triangle = "<<sqrt(s*(s-a)*(s-b)*(s-c));
}

Area(int l,int b)
{
cout<<"Area of Rectangle = "<<l*b;
}

};

void main()
{
clrscr();
Area C(1);
Area T(1,2,3);
Area R(2,3);
getch();
}

OUTPUT:

Area of Circle = 3.14
Area of Triangle = 0
Area of Rectangle = 6













Ashwini C. Khadye Contact: 9773712416
36

41) WAP to implement simple calculator

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b,choice;

cout<<"Enter two numbers: ";
cin>>a>>b;

cout<<"\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division";
cout<<"\nEnter your choice: ";
cin>>choice;

switch(choice)
{
case 1:cout<<"Addition= "<<a+b;
break;

case 2:cout<<"Subtraction= "<<a-b;
break;

case 3:cout<<"Multiplication= "<<a*b;
break;

case 4:cout<<"Division= "<<(float)a/b;
break;

default:cout<<"Invalid choice";
}
getch();
}

OUTPUT:
Enter two numbers: 1 2

1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 4
Division= 0.5

You might also like