programs fe c++

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

Upload: khadyeashwini

Post on 03-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 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** *

* * *

* * * *

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 2/36

   Ashwini C. Khadye Contact: 9773712416

2

3) WAP to print following pattern

P

P QP 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

PP QP Q R

P Q R S

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 3/36

   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

* * * ** * *

* **

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 4/36

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 5/36

   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 23 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

12 2

3 3 34 4 4 4

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 6/36

   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

** * *

* * * * ** * * * * * *

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 7/36

   Ashwini C. Khadye Contact: 9773712416

7

12)  WAP to print following pattern

A

A B A

A B C B AA 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:4A

A B A

A B C B AA B C D C B A

13)  WAP to print following pattern

1

2 1 A

3 2 1 A B4 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:41

2 1 A

3 2 1 A B4 3 2 1 A B C

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 8/36

   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:5Factorial 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. = "<<resul

getch();

}

 //Function Defination int addition(int n)

{if(n==0)

return 0;else

return n+addition(n-1);}

OUTPUT:

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

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 9/36

   Ashwini C. Khadye Contact: 9773712416

9

16)  WAP to calculate x raise to nusing 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;

elsereturn x*CalPower(x,n-1);

}

OUTPUT:

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

17)  WAP to display fibonacci series for 'nterms 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;

elsereturn fib(n - 1) + fib(n - 2);

}

OUTPUT:

Enter number of terms fibonacci series: 7

Fibonacci Series:

0 1 1 2 3 5 8

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 10/36

   Ashwini C. Khadye Contact: 9773712416

10

18)  WAP to convert decimal

number(integer number) to binarynumber

#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:123Reverse number = 321

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 11/36

   Ashwini C. Khadye Contact: 9773712416

11

20)  WAP to check whether givennumber 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";

elsecout<<dup_n<<" is not Palindrom";

getch();}

OUTPUT:

Enter the number:121121 is Palindrom

Enter the number:123

123 is not Palindrom

21)  WAP to check whether givennumber 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";

elsecout<<dup_n<<" is not Palindrom";

getch();}

OUTPUT:

Enter the number:153153 is Armstrong number

Enter the number:1234

1234 is not Armstrong number

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 12/36

   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:55 is prime number

Enter the number:100100 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 number5 is prime number7 is prime number

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 13/36

   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 8GCD = 4

LCM = 8

Enter the two numbers:5 8GCD = 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

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 14/36

   Ashwini C. Khadye Contact: 9773712416

14

26)  WAP to sort given array OrWAP to sort given numbers in ascendingOrder

#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:5Enter the numbers:34 21 76 3 15Ascending Order:3 15 21 34 76

 //NOTE:For descending order,just reverse >

sign i.e.write if(a[j]<a[j+1]) inplace ofif(a[j]>a[j+1]) 

27)  WAP to sort given array of strings OrWAP to sort given strings in ascendingOrder

#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:5Enter the Strings:AshuKhadyeAnuSonaPinkyAscending Order:Anu Ashu Khadye Pinky Sona

 //NOTE:For descending order,just reverse >

sign i.e.write if(strcmp(a[j],a[j+1])<0) inplaceof if(strcmp(a[j],a[j+1])>0)

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 15/36

   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

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 16/36

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

}}

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 17/36

   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 23 4

MARTIX:5 6 78 9 01 2 3

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 18/36

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

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 19/36

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

}}

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 20/36

   Ashwini C. Khadye Contact: 9773712416

20

 //Matrix Multiplicationvoid 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 matrixvoid 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 23 4

MARTIX2:5 64 3

Addition:6 87 7

Subtraction:-4 -4-1 1

Multiplication:13 1231 30

Transponse:1 32 4

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 21/36

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

}}

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 22/36

   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 34 5 6

7 8 9

Transponse:

1 4 72 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);

elsecout<<"\nMatrix does not form Diagonal";

getch();}

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 23/36

   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 34 5 67 8 9

Sum of Diagonal Elements = 15

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 24/36

   Ashwini C. Khadye Contact: 9773712416

24

33)  WAP that reads any positive integer n and prints the digits inwords (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 numberwhile(n!=0)

{ rem=n%10;rev=(rev*10)+rem;n=n/10;

}

 //Get unit place,ten place... from reverse number and print itcout<<"In words: ";while(rev!=0){

rem=rev%10;

cout<<Digit[rem]<<" ";rev=rev/10;

}

getch();}

OUTPUT:Enter the number: 9756802In words: Nine Seven Five Six Eight Zero Two

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 25/36

   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]: 3Month Name: March

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 26/36

   Ashwini C. Khadye Contact: 9773712416

26

STRUCTURE:35)  Define structure consisting of following elements

i)  Employee IDii)  Employee nameiii)  Employee salaryWAP 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:SheetalEnter the Employee id:1

Enter the Employee salary:5000

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 27/36

   Ashwini C. Khadye Contact: 9773712416

27

Enter the Employee name:VidyaEnter the Employee id:2Enter the Employee salary:10000

Enter the Employee name:AshwiniEnter the Employee id:3

Enter the Employee salary:4000

Enter the Employee name:NiketaEnter the Employee id:4

Enter the Employee salary:15000

Employee name:Sheetal

Employee id:1Salary:5000

Employee name:VidyaEmployee id:2

Salary:10000

Employee name:AshwiniEmployee id:3

Salary:4000

Employee name:Niketa

Employee id:4Salary:15000 

36)  Define structure within structure consisting of following elements

i)  Employee IDii)  Employee name

iii)  Employee salaryWAP 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;

};

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 28/36

   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:201Enter the Employee salary:15000Enter the day of Joining Date:1

Enter the month of Joining Date:3Enter the year of Joining Date:2009

Enter the Employee name:AnitaEnter the Employee id:202Enter the Employee salary:30000

Enter the day of Joining Date:1

Enter the month of Joining Date:5Enter the year of Joining Date:2006

Employee name:AshwiniEmployee id:201

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 29/36

   Ashwini C. Khadye Contact: 9773712416

29

Salary:15000Date of Joining:1/3/2009

Employee name:AnitaEmployee id:202Salary: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 toCountry name\n3.List According to number of matches

played\n4.List According to number of goals scored";

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

cin>>choice;

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 30/36

   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;

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 31/36

   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:EnglandEnter the number of matches played:15Enter the number of goals scored:8

Enter the player name:SachinEnter the country name:IndiaEnter the number of matches played:25

Enter the number of goals scored:23

Enter the player name:Vidya

Enter the country name:AmericaEnter the number of matches played:12Enter the number of goals scored:9

Enter the player name:NiketaEnter the country name:LondonEnter the number of matches played:20

Enter the number of goals scored:11

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 32/36

   Ashwini C. Khadye Contact: 9773712416

32

1.List According to Player name2.List According to Country name3.List According to number of matches played

4.List According to number of goals scoredPlease enter your choice:1

player name:Ashwin

country name:England

No. of Matches:15No.Of Goals:8

player name:Niketacountry name:London

No. of Matches:20

No.Of Goals:11

player name:Sachin

country name:IndiaNo. of Matches:25

No.Of Goals:23

player name:Vidyacountry name:America

No. of Matches:12No.Of Goals:9

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 33/36

   Ashwini C. Khadye Contact: 9773712416

33

38)  WAP to find how many objects of class has been created usingstatic 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=1Demo d2; //i=2

Demo d3; //i=3

Demo::display(); //Static member function calling using classnamegetch();

}

OUTPUT:Number of objects = 3

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 34/36

   Ashwini C. Khadye Contact: 9773712416

34

39)  WAP to find area of circle,area of rectangle and area of triangleusing 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 = 0Area of Rectangle = 6

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 35/36

   Ashwini C. Khadye Contact: 9773712416

35

40)  WAP to find area of circle,area of rectangle and area of triangleusing 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.14Area of Triangle = 0

Area of Rectangle = 6

8/12/2019 Programs FE C++

http://slidepdf.com/reader/full/programs-fe-c 36/36

   Ashwini C. Khadye Contact: 9773712416

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.Subtraction3.Multiplication4.Division

Enter your choice: 4Division= 0.5