NATIONAL INSTITUTE OF TECHNOLOGY
(MHRD, Govt. of India)
Warangal, Telangana, India – 506004
Department of Physics
Lab record
Of
Problem solving in computer
programing
Submitted to : Dr. Vijiyalakshmi
1|Page
NATIONAL INSTITUTE OF TECHNOLOGY
(MHRD, Govt. of India)
Warangal, Telangana, India – 506004
Certificate
This is to certify that this is the bonafied record of Problem
solving in computer programing work done by
Ms , Roll No. of 2nd
semester [Link]. (tech) Engineering Physics during the year 2019.
Date:
Place: NIT Warangal Faculty-In-Charge
2|Page
CONTENTS
1) WAP to do basic mathematical operations
2) WAP program to find the given number odd or even
3) WAP to convert temp from Celsius to Fahrenheit’s or vice versa (taking choice from
the user)
4) Program to swap two numbers
5) Program to check If the person is eligible for voting
6) WAP to find biggest of n numbers
7) WAP to find smallest of n integers
8) WAP to find grade of a student
9) WAP to check if the given number is prime or not
10) WAP to find the sum of series
11) Program to find the roots of quadratic equation (without using switch)
12) WAP to solve quadratic equation using switch
13) WAP to do basic calculator operations
14) WAP to find factorial of a given number
15) Write a programme to print N numbers in a line
16) WAP to print right half pyramid
17) WAP to print left half pyramid
18) WAP to print pyramid of stars
19) WAP to print a star pyramid
20) WAP to print inverted half pyramid
21) WAP to print Pascal’s triangle
22) WAP to print number triangle
23) WAP to swap two strings
24) WAP to using pushback command of string
25) WAP using erase ,insert and replace command in string
26) WAP to append two strings
27) WAP to find character in a string
28) WAP to copy two strings
29) WAP to Print table
30) WAP for Matrix Subtraction
31) WAP to sort a array in ascending order
32) WAP for Matrix addition
33) WAP to find largest element using array
34) WAP to find square of a number using call by value
35) WAP to find product of two numbers using function
3|Page
36) WAP to swap two numbers using functions by ‘call by value’
37) WAP to swap two numbers using functions by ‘call by reference’
38) WAP program to add two arrays using functions
39) WAP to find factorial using recursion functions
40) WAP to do subtraction of2*3 matric using functions
41) WAP to demonstrate various types of variables
42) WAP to demonstrate pointer definition
43) WAP to swap two numbers using pointers
44) WAP to sort an array using pointer
45) WAP to find square of a number using pointer arguments
46) WAP to subtract two matrices using functions
47) to find transpose of a WAP matrix using function
48) WAP to check if the given number is palindrome or not
49) WAP for Armstrong number
4|Page
[Link] to do basic mathematical operations-
#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
int a,b;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
cout<<"sum= "<<a+b<<"\n";
cout<<"differece= "<<a-b<<"\n";
cout<<"product= "<<a*b<<"\n";
cout<<"DIVISION= "<<a/b<<"\n";
cout<<"remainder= "<<a%b<<"\n";
cout<<"exponential= "<<pow(a,b)<<"\n";
return (0);
}
Output-
Enter first number:3
Enter second number: 6
sum= 9
differece= -3
product= 18
DIVISION= 0
remainder= 3
exponential= 729
--------------------------------
[Link] program to find the given number odd or even-
#include<iostream>
using namespace std;
main()
{
int a;
cout<<"Enter the number to be tested: ";
5|Page
cin>>a;
if (a%2==0)
cout<<a<<" is an even number.";
else
cout<<a<<" is an odd number.";
}
Output-
Enter the number to be tested:
9
9 is an odd number.
--------------------------------
[Link] to convert temp from Celsius to Fahrenheit’s or vice
versa (taking choice from the user)-
#include<iostream>
using namespace std;
main()
{
float T,p;
cout<<"enter the temperature:";"/n";
cin>>T;
cout<<"press 1 forF-->C press 2 for C-->F";"/n";
cin>>p;
if(p==1)
{
T=((T-32)*5/9);
cout<<"temperature in celsious is:"<<T;
}
else
{
T=((T*9)/5 +32);
cout<<"temperture in fahrenheits is:"<<T;
}
return 0;
}
6|Page
Output-
enter the temperature:
360
press 1 forF-->C press 2 for C-->F
1
temperature in celsious is:182.222
--------------------------------
4. Program to swap two numbers-
#include<iostream>
using namespace std;
main()
{
int a,b,c;
cout<<"Enter the first number\n";
cin>>a;
cout<<"Enter the second number\n";
cin>>bc=a;
a=b;
b=c;
cout<<"after swapping the first number is : "<<a<<endl;
cout<<"after swapping the second number is : "<<b<<endl;
return 0;
}
Output
Enter the first number
5
Enter the second number
6
after swapping the first number is : 6
after swapping the second number is : 5
7|Page
[Link] to check If the person is eligible for voting
#include<iostream>
using namespace std;
main()
{
int x;
cout<<"enter your age\n";
cin>>x;
if(x>=18)
{
cout<<"you are eligible for voting"
}
else
{
cout<<"sorry!!,You are not eligible for voting";
}
return 0;
Output
enter your age
5
sorry!!,You are not eligible for voting
[Link] to find biggest of n numbers-
#include<iostream>
using namespace std;
main()
{
int n,i,num,large=0;
cout<<"enter the total number of numbers to be examined";
cin>>n;
for(i=1;i<=n;i++)
{
8|Page
cout<<"Enter the number";
cin>>num;
if(num>large)
{
large = num;
}
}
cout<<"the largest number is : "<<large;
return 0;
Output-
Enter the total number of numbers to be examined
5
Enter the number2
Enter the number5
Enter the number8
Enter the number4
Enter the number3
the largest number is : 8
[Link] to find smallest of n integers
#include<iostream>
using namespace std;
main()
{
int a ,b, c, d, e;
cout<<"enter the number \n ";
cin>>a;
cout<<"enter the number \n ";
cin>>b;
cout<<"enter the number \n ";
cin>>c;
cout<<"enter the number \n ";
cin>>d;
cout<<"enter the number \n ";
cin>>e;
9|Page
if((a<b)&&(a<c)&&(a<d)&&(a<e))
cout<<"The smallest number is :"<<a;
if((b<a)&&(b<c)&&(b<d)&&(b<e))
cout<<"The smallest number is :"<<b;
if((c<b)&&(c<a)&&(c<d)&&(c<e))
cout<<"The smallest number is :"<<c;
if((d<b)&&(d<c)&&(d<a)&&(d<e))
cout<<"The smallest number is :"<<d;
if((e<b)&&(e<c)&&(e<d)&&(e<d))
cout<<"The smallest number is :"<<e;
return 0;
}
Output-
enter the number
5
enter the number
2
enter the number
3
enter the number
46
enter the number
8
The smallest number is :2
[Link] to find grade of a student-
#include<iostream>
using namespace std;
main()
{
int x;
cout<<"enter your marks\n";
cin>>x;
if(x>=90)
cout<<"You have got A+ grade";
if((x>=80) &&(x<90))
cout<<"you have got A grade";
if((x>=70) &&(x<80))
cout<<"you have got B grade";
if((x>=70) &&(x<80))
10 | P a g e
cout<<"you have got C grade";
if((x>=60) &&(x<70))
cout<<"you have got D grade";
if((x>=50) &&(x<60))
cout<<"you have got E grade";
if((x>=40) &&(x<50))
cout<<"you have got P grade";
if(x<40)
cout<<"You have failed the exam\n";
Output-
Enter your marks
45
you have got P grade
------------------------
[Link] to check if the given number is prime or not
#include<iostream>
using namespace std;
main()
{
int i,n,d;
cout<<"enter the number to be tested";
cin>>n;
for(i=2;i<=(n/2);i++)
{
if(n%i==0)
d==1;
}
if(d==1)
cout<<"The given number is prime number";
else
cout<<"the given number is composite";
11 | P a g e
return 0;
}
Output-
enter the number to be tested
3
The given number is prime number
--------------------------------
[Link] to find the sum of seires-
#include<iostream>
using namespace std;
main()
{
int N;
float i,sum=0;
cout<<"Enter the value of N";
cin>>N;
for(i=1;i<=N;i++)
{
sum =sum+(1/i);
}
cout<<"Sum of seires is :"<<sum;
return 0;
}
Output-
Enter the value of N
20
Sum of seires is :3.59774
--------------------------------
[Link] to find the roots of quadratic equation- (without using
switch)
12 | P a g e
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a, b, c, d,r1,r2;
cout<<"Enter the a,b,c of quadratic equations.\n";
cin>>a>>b>>c;
d= (b*b)-(4*a*c);
if (d<0)
{
cout<<"The roots are complex.\n";
r1=-b/(2*a);
r2=sqrt(-d)/(2*a);
cout<<"first root is."<<r1<<"+i"<<r2<<endl;
cout<<"second root is."<<r1<<"-i"<<r2<<endl;
}
else if (d>0)
{
cout<<"The roots are real and different.\n";
r1=(-b-sqrt(d))/(2*a);
r2=(-b+sqrt(d))/(2*a);
cout<<"first root is="<<r1<<endl;
cout<<"the second root is="<<r2<<endl;
}
else
{
cout<<"The roots are euqal.\n";
r1=-b/(2*a);
cout<<"the roots are= "<<r1;
}
return 0;
}
Output-
Enter the a,b,c of quadratic equations.
5
3
6
The roots are complex.
first root is.-0.3+i1.05357
second root is.-0.3-i1.05357
13 | P a g e
[Link] to solve quadratic equation using switch-
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,D;
int n;
cout<<"enter the coefficients of quad eqn of form ax^2+bx+c=0: \n";
cin>>a>>b>>c;
D=b*b-4*a*c;
if(D>0) n=1; else if(D<0) n=2; else n=3;
switch(n)
{
case 1 :
cout<<"the real & distinct roots are:\n"<<(-b/(2*a))+sqrt(D)/(2*a)<<"\n"<<(-b/(2*a))-sqrt(D)/(2*a);
break;
case 2 :
cout<<"the compplex & distinct roots are:\n"<<(-b/(2*a))<<"+j"<<sqrt(-D)/(2*a)<<"\n"<<(-b/(2*a))<<"-
j"<<sqrt(-D)/(2*a);
break;
case 3 :
cout<<"the equal roots are:\n"<<-b/(2*a);
break;
}
return 0;
}
Output:
enter the coefficients of quad eqn of form ax^2+bx+c=0:
2
4
5
the compplex & distinct roots are:
-1+j1.22474
-1-j1.22474
14 | P a g e
[Link] to do basic calculator operations-
#include<iostream>
using namespace std;
main()
{
int a,b,d;
float o;
cout<<"Enter the no.s";
cin>>a>>b;
cout<<"press 1 for addition,2 for subtraction ,3 for multiplication,4 for division";
cin>>d;
switch(d)
{
case 1:
o=a+b;
cout<<"Addition of given no.s is :"<<o;
break;
case 2:
o=a-b;
cout<<"subtraction of given no.s is :"<<o;
break;
case 3:
o=a*b;
cout<<"Multiplication of given no.s is :"<<o;
break;
case 4:
o=a/b;
cout<<"Division of given no.s is :"<<o;
break;
default :
cout<<"Invalid input";
}
return 0;
}
Output-
Enter the no.s
5
15 | P a g e
6
press 1 for addition,2 for subtraction ,3 for multiplication,4 for division
3
Multiplication of given no.s is :30
[Link] to find factorial of a given number-
#include<iostream>
using namespace std;
int main()
{
int n, i, fact=1;
cout<<"Enter the number: ";
cin>>n;
for(i=1; i<=n;i++)
fact=fact*i;
cout<<" The factorial of given number is: "<<fact;
return 0;
}
Output-
Enter the number: 5
The factorial of given number is: 120
-------------------------------
[Link] a programe to print N numbers in a line-
#include<iostream>
using namespace std;
main()
{
int n,i;
cout<<"Enter the total numbers to be printed";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<i<<" ";
}
16 | P a g e
return 0;
Output-
Enter the total numbers to be printed
12
1 2 3 4 5 6 7 8 9 10 11 12
--------------------------------
[Link] to print right half pyramid-
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
Output-
Enter number of rows:
5
*
**
***
****
*****
17 | P a g e
[Link] to print left half pyramid-
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int k=i; k<rows;k++)
cout<<" ";
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
Output:
Enter number of rows: 4
*
**
***
****
[Link] to print pyramid of stars-
#include<iostream>
using namespace std;
main()
18 | P a g e
{
int n,i,j,k;
cout<<"Enter the number of rows in pyramid";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=(n-i);j++) //Loop to print spaces
{
cout<<" ";
}
for(k=1;k<=(2*i)-1;k++) //to print stars
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}
Output-
Enter the number of rows in pyramid5
*
***
*****
*******
*********
--------------------------------
[Link] to print a star pyramid-
#include<iostream>
using namespace std;
main()
{
int n,i,j,k;
cout<<"Enter the number of rows in pyramid";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++) //Loop to print spaces
{
19 | P a g e
cout<<" ";
}
for(k=1;k<=2*n-(2*i-1);k++) //to print stars
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}
Output-
Enter the number of rows in pyramid
5
*********
*******
*****
***
*
--------------------------------
[Link] to print inverted half pyramid-
#include<iostream>
using namespace std;
main()
{
int n,i,j;
cout<<"Enter the number of rows in pyramid";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
cout<<"* ";
20 | P a g e
}
cout<<endl;
}
return 0;
}
Output-
Enter the number of rows in pyramid
5
*****
****
***
**
*
--------------------------------
[Link] to print pascals triangle-
#include <iostream>
using namespace std;
int main()
{
int rows, coef = 1;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 0; i < rows; i++)
{
for(int space = 1; space <= rows-i; space++)
cout <<" ";
for(int j = 0; j <= i; j++)
{
if (j == 0 || i == j)
coef = 1;
else
21 | P a g e
coef = coef*(i-j+1)/j;
cout << coef << " ";
}
cout << endl;
}
return 0;
}
Output-
Enter number of rows:
5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
--------------------------------
[Link] to print number triangle-
#include<iostream>
using namespace std;
int main()
{
int i,n,j;
cout<<"Enter the number of rows";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
cout<<endl;
return 0;
22 | P a g e
}
Output-
Enter the number of rows
5
1
12
123
1234
12345
[Link] to swap two strings-
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char S1[30],S2[30];
cout<<"Enter the First string: ";
gets(S1);
cout<<endl<<"The first string is: "<<S1<<endl;
cout<<"Enter the Second string: ";
gets(S2);
cout<<"The second string is: "<<S2<<endl;
swap(S1,S2);
cout<<"After swapping the strings are : ";
cout<<S1<<S2;
Output-
Enter the First string:NIT
The first string is: NIT
Enter the Second string: warangal
The second string is: warangal
After swapping the strings are : warangalNIT
23 | P a g e
--------------------------------
[Link] to using pushback command of string-
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s1="bye bye nit";
string s2="Welcome";
string s3=" to NIT";
cout<<s1<<endl;
s1.push_back('s');
cout<<s1;
[Link](8,4);
cout<<endl<<s1;
[Link](7,s3);
cout<<endl<<s2;
Output-
bye bye nit
bye bye nits
bye bye
Welcome to NIT
--------------------------------
[Link] using erase ,insert and replace command in string-
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s1="As Time by... ",s2=" goes";
[Link](7,s2);
cout<<endl<<s1;
[Link](15,3);
24 | P a g e
cout<<endl<<s1;
[Link](13,2,"will");
cout<<endl<<s1;
}
Output-
As Time goes by...
As Time goes by
As Time goes will
--------------------------------
[Link] to append two strings-
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s1="hello ",s2="world";
cout<<s1<<endl<<s2<<endl;
[Link](s2);
cout<<s1;
}
Output-
hello
world
hello world
--------------------------------
[Link] to find character in a string-
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[100];
25 | P a g e
char k;
cout<<"enter your string:\n";
[Link](str,100);
cout<<"type your character which you want to find ";
cin>>k;
if (strchr(str,k))
cout<<"your char "<<k<<" is present"<<endl;
else
cout<<"your char "<<k<<" is not present in string";
return 0;
}
Output:
enter your string:
this is a string
type your character which you want to find k
your char k is not present in string
--------------------------------
[Link] to copy two strings-
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s1[20]={'h','e','l','l','o'},s2[20]={'N','I','T'};
strcpy(s2,s1);
cout<<”After copying s2 is “<<s2<<endl<<” After copying s1 is “<<s1;
}
[Link] to Print table-
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
26 | P a g e
int d1=1,d2=5,d3=7;
float m1=8,m2=5,m3=4;
cout<<setw(5)<<"M1"<<setw(5)<<"m2"<<setw(5)<<"m3"<<endl;
cout<<setw(5)<<m1<<setw(5)<<m2<<setw(5)<<m3<<endl;
cout<<setw(5)<<d1<<setw(5)<<d2<<setw(5)<<d3;
return 0;
Output-
M1 m2 m3
8 5 4
1 5 7
--------------------------------
[Link] for Matrix Subtraction-
#include<iostream>
using namespace std;
int main()
{
int A[10][10],B[10][10], row,col,i,j,sub[10][10];
cout<<"Enter the mumber of rows and col. in the matrix"<<endl;
cin>>row>>col;
cout<<"Enter the elements in the first matrix :";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>A[i][j];
}
}
cout<<"The first matrix is :"<<endl;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<A[i][j]<<" ";
27 | P a g e
}
cout<<endl;
}
cout<<"Enter the elements in the second matrix :";
for(i=0;i<row;i++)
{
for(j=0;j<col;j+
{
cin>>B[i][j];
}
}
cout<<"The second matrix is :"<<endl;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<B[i][j]<<" ";
}
cout<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
sub[i][j]=A[i][j]-B[i][j];
}
}
cout<<"The subtraction of given matrices is "<<endl;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<sub[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
28 | P a g e
Output-
Enter the mumber of rows and col. in the matrix
2
3
Enter the elements in the first matrix :
235648
The first matrix is :
235
648
Enter the elements in the second matrix :
569236
The second matrix is :
5 6 9
2 3 6
The subtraction of given matrices is
-3 -3 -4
4 1 2
--------------------------------
[Link] to sort a array in ascending order-
#include<iostream>
using namespace std;
int main()
{
int i,j,n,asc,Bull[100];
cout<<"enter the size of array:";
cin>>n;
cout<<"enter your array: \n";
for(i=0;i<n;i++)
{
cin>>Bull[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(Bull[i]>Bull[j])
{
asc=Bull[i];
29 | P a g e
Bull[i]=Bull[j];
Bull[j]=asc;
}
}
}
cout<<"your array in ascending order is:\n";
for(i=0;i<n;i++)
{
cout<<Bull[i]<<"\t";
}
return 0;
}
Output:
enter the size of array:5
enter your array:
2
4
1
5
7
your array in ascending order is:
1 2 4 5 7
[Link] for Matrix addition-
#include<iostream>
using namespace std;
int main()
{
int A[10][10],B[10][10], row,col,i,j,sum[10][10];
cout<<"Enter the mumber of rows and col. in the matrix"<<endl;
cin>>row>>col;
cout<<"Enter the elements in the first matrix :";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>A[i][j];
30 | P a g e
}
cout<<"The first matrix is :"<<endl;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
cout<<"Enter the elements in the second matrix :";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>B[i][j];
}
}
cout<<"The second matrix is :"<<endl;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<B[i][j]<<" ";
}
cout<<endl;
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
sum[i][j]=A[i][j]+B[i][j];
}
}
cout<<"The sum of given matrices is "<<endl;
31 | P a g e
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<sum[i][j]<<" ";
cout<<endl;
return 0;
}
Output-
Enter the number of rows and col. in the matrix
2
3
Enter the elements in the first matrix :
123566
The first matrix is :
123
566
Enter the elements in the second matrix :
544689
The second matrix is :
5 4 4
6 8 9
The sum of given matrices is
6 6 7
11 14 15
--------------------------------
[Link] to find largest element using array-
#include<iostream>
using namespace std;
int main()
{
32 | P a g e
int i ,n;
float Num[100];
int arr[0];
cout<<"Enter the number of elements to be examined";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the number";
cin>>Num[i];
}
for(i=0;i<n;i++)
{
cout<< Num[i];
}
for(i = 0;i < n; i++)
{
// Change < to > if you want to find the Largest element
if(arr[0] < Num[i])
arr[0] = Num[i];
}
cout << "Largest element = " << arr[0];
return 0;
}
Output
Enter the number of elements to be examined -5
Enter the number5
Enter the number6
Enter the number2
Enter the number8
Enter the number6
56286
Largest element = 8
--------------------------------
[Link] find square of a number using call by value
#include<iostream>
using namespace std;
33 | P a g e
void sqr(int n)
{int c;
c=n*n;
cout<<"Square of the given number is :"<< c;
}
int main()
{
int n;
cout<<"Enter the number whose square has to be done : "<<endl;
cin>>n;
sqr(n);
return 0;
Output-
Enter the number whose square has to be done :
20
Square of the given number is :400
--------------------------------
[Link] to find product of two numbers using function-
#include<iostream>
using namespace std;
int product(int x,int y);
// since here I defined the function later hence it has to declared before,otherwise if i was defining
the function before calling its declartion would be unimportant//
int main()
{
int a,b,c;
cout<<"enter the two numbers :";
cin>>a>>b;
c=product(a,b);
cout<<"product is :"<<c;
return 0;
}
int product (int x,int y);
34 | P a g e
return x * y;
Output-
enter the two numbers :
5
6
product is :30
--------------------------------
[Link] to swap two numbers using functions by ‘call by value’--
//Call by value
#include<iostream>
using namespace std;
int swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}
int main()
{
int a ,b;
cout<<"Enter the numbers to be swaped : ";
cin>>a>>b;
cout<<"Before swapping the value of a is : "<<a<<endl<<"Before swapping the value of b is :
"<<b<<endl;
swap(a,b);
cout<<"after swapping the value of a is : "<<a<<endl<<"after swapping the value of b is : "<<b;
Output-
Enter the numbers to be swaped :
35 | P a g e
45
52
Before swapping the value of a is : 45
Before swapping the value of b is : 52
after swapping the value of a is : 52
after swapping the value of b is : 45
--------------------------------
[Link] to swap two numbers using functions by ‘call by refrence’-
-
//Call by reference
#include<iostream>
using namespace std;
int swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}
int main()
{
int a ,b;
cout<<"Enter the numbers to be swaped : ";
cin>>a>>b;
cout<<"Before swapping the value of a is : "<<a<<endl<<"Before swapping the value of b is :
"<<b<<endl;
swap(a,b);
cout<<"after swapping the value of a is : "<<a<<endl<<"after swapping the value of b is : "<<b;
}
Output-
Enter the numbers to be swaped :
25
45
Before swapping the value of a is : 25
Before swapping the value of b is : 45
after swapping the value of a is : 45
36 | P a g e
after swapping the value of b is : 25
--------------------------------
[Link] program to add two arrays using functions-
#include<iostream>
using namespace std;
void sum(int a1[4],int a2[4])
{
int temp[4],i;
for(i=0;i<=4;i++)
{
temp[i]=a1[i]+a2[i];
cout<<temp[i]<<" ";
}
}
int main()
{
int n1[]={1 ,2, 3, 4, 5},n2[]={6, 7, 8, 9, 2};
sum(n1,n2);
return 0;
}
Output-
7 9 11 13 7
--------------------------------
[Link] to find factorial using recursion fucntions-
#include<iostream>
using namespace std;
int fact(int n)
{
int c,d;
if(n==1)
return 1;
else
return n*fact(n-1);
37 | P a g e
}
int main()
{
int a;
cout<<"Enter the number";
cin>>a;
cout<<"Factorial of the given number is :"<< fact(a);
}
Output-
Enter the number
5
Factorial of the given number is :120
--------------------------------
[Link] to do subtraction of2*3 matric using functions-
//subtraction of2*3 matrix using fuctions
#include<iostream>
using namespace std;
void sub( int a1[2][3],int a2[2][3])
{
int i,j,temp[i][j];
for(i=0;i<2;i++)
{
for(j=0;j<=3;j++)
{
temp[i][j]=a1[i][j]-a2[i][j];
cout<<temp[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
int n1[2][3]={{1,2,3},{4,5,6}} ,n2[2][3]={{7,8,9},{10,11,12}};
sub(n1,n2);
38 | P a g e
return 0;
Output-
-6 -6 -6 -6
[Link] to demonstrate various types of variables-
//global ,normal and static variable
#include<iostream>
using namespace std;
int g=1;
void scope()
{
int i=1;
static int s=1;
i++ ;
s++ ;
g++ ;
cout<<"The value of i is : "<<i<<endl;
cout<<"The value of s is : "<<s<<endl;
cout<<"The value of g is : "<<g<<endl;
}
int main()
{
int j;
for(j=0;j<=5;j++)
{
g++ ;
cout<<"f("<<j<<")";
scope();
}
return 0;
39 | P a g e
Output-
f(0)The value of i is : 2
The value of s is : 2
The value of g is : 3
f(1)The value of i is : 2
The value of s is : 3
The value of g is : 5
f(2)The value of i is : 2
The value of s is : 4
The value of g is : 7
f(3)The value of i is : 2
The value of s is : 5
The value of g is : 9
f(4)The value of i is : 2
The value of s is : 6
The value of g is : 11
f(5)The value of i is : 2
The value of s is : 7
The value of g is : 13
--------------------------------
[Link] to demonstrate pointer definition-
#include<iostream>
using namespace std;
int main()
{
int x,*p;
x=20;
p= &x;
cout<<"The value of x is : "<<x<<endl<<"The address stored in p is :"<<p<<endl<<"The value of address
stored in p is :"<<*p;
Output-
The value of x is : 20
The address stored in p is :0x70fe34
The value of address stored in p is :20
40 | P a g e
[Link] to swap two numbers using pointers-
//swapping two no.s using pointers
#include<iostream>
using namespace std;
void swap(int *n1,int *n2)
{
int temp=*n1;
*n1=*n2;
*n2=temp;
}
int main()
{
int a=20,b=60;
cout<<"Before swapping value of a and b are : "<<endl<<a<<endl<<b<<endl;
swap(&a,&b);
cout<<"After swapping value of a and b are : "<<endl<<a<<endl<<b;
return 0;
}
Output-
Before swapping value of a and b are :
20
60
After swapping value of a and b are :
60
20
[Link] to sort an array using pointer-
//Sorting of array using pointer
#include<iostream>
using namespace std;
int arr[10];
void input(int n)
{
for(int i=0;i<n;i++)
{
41 | P a g e
cout<<"Enter the element "<<i+1<<" :";
cin>>arr[i];
}
cout<<endl;
}
void print(int n)
{
cout<<"Elements of the array are :";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}
void swap(int *n1,int*n2)
{
int temp=*n1;
*n1=*n2;
*n2=temp;
}
int main()
{
int n;
cout<<"Enter the size of array to be sorted :";
cin>>n;
input(n);
print(n);
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if( arr[j]<arr[i])
{
swap(&arr[i],&arr[j]);
}
}
}
cout<<endl;
cout<<"After sorting the array is : "<<endl;
print(n);
return 0;
}
42 | P a g e
Output-
Enter the size of array to be sorted :
5
Enter the element 1 :8
Enter the element 2 :23
Enter the element 3 :4
Enter the element 4 :6
Enter the element 5 :7
Elements of the array are :
8 23 4 6 7
After sorting the array is :
4 6 7 8 23
--------------------------------
[Link] to find square of a number using pointer arguments-
//square of a number using pointer arguments
#include<iostream>
using namespace std;
void sqr(int *n)
{
*n=(*n )* (*n);
}
int main()
{
int x;
cout<<"Enter the number : ";
cin>>x;
sqr(&x);
cout<<"The square of given number is : "<<x;
return 0;
}
Output-
Enter the number : 2
The square of given number is : 4
43 | P a g e
[Link] to subtract two matrices using functions-
//subtraction of matrix using functions
#include<iostream>
using namespace std;
void sub(int a1[2][3],int a2 [2][3])
{
int temp[2][3],i,j;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
temp[i][j]=a1[i][j]-a2[i][j];
cout<<temp[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
int n1[2][3]={{1,2,3},{4,5,6}};
int n2[2][3]={{6,7,9},{3,8,10}};
cout<<”The matrix after subtraction is-“;
sub(n1,n2);
return 0;
}
Output-
The matrix after subtraction is-
-5 -5 -6
1 -3 -4
--------------------------------
44 | P a g e
[Link] to find transpose of a matrix using function-
//Transpose of a matrix using subfuction
#include<iostream>
using namespace std;
void transpose(int a1[2][3])
{
int temp[3][2],i,j;
cout<<"After transpose the matrix is: \n";
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
temp[i][j]=a1[j][i];
cout<<temp[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
int i,j,n1[2][3]={{1,2,3},{4,5,6}};
cout<<"before transpose the matrix was-";
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<n1[i][j]<<" ";
}
cout<<endl;
}
transpose(n1);
return 0;
}
45 | P a g e
Output-
before transpose the matrix was-
1 2 3
4 5 6
After transpose the matrix is:
1 4
2 5
3 6
--------------------------------
[Link] to check if the given number is palindrome or not-
#include<iostream>
using namespace std;
int main()
{
int n,p,s,b=0;
cout<<"\n Enter the number to be checked: ";
cin>>n;
p=n;
while (n!=0)
{
s=n%10;
b= b*10+s;
n/=10;
}
if (p==b)
cout<<"\n The given number is pailindrome.";
else
cout<<"\n The number is not pailindrome.";
return 0;
}
Output-
Enter the number to be checked: 1221
The given number is pailindrome.
--------------------------------
46 | P a g e
[Link] for Armstrong number-
#include<iostream>
using namespace std;
int main()
{
int n,i,Num=0,rem,Q,m;
cout<<"enter the number:";
cin>>n;
m=n;
do
{
rem=m%10;
Num+=rem*rem*rem;
Q=m/10;
m=Q;
}while(rem!=0);
if(Num==n)
cout<<"the number is Amstrong number";
else
cout<<"the number is not Amstrong number";
return 0;
}
Output:
enter the number:123
the number is not Amstrong number
enter the number:371
the number is Amstrong number
47 | P a g e