Академический Документы
Профессиональный Документы
Культура Документы
Department of Physics
Lab record
Of
Certificate
Date:
Place: NIT Warangal Faculty-In-Charge
2|Page
CONTENTS
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
1.WAP 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-
--------------------------------
#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-
#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-
#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
7|Page
5.Program 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
{
Output
#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-
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-
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-
------------------------
#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)
11 | P a g e
return 0;
}
Output-
#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-
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-
#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
13.WAP 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-
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
Output-
16 | P a g e
return 0;
Output-
}
Output-
17 | P a g e
17.WAP to print left half pyramid-
#include <iostream>
using namespace std;
int main()
{
int rows;
Output:
Enter number of rows: 4
*
**
***
****
#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
*
***
*****
*******
*********
--------------------------------
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-
*********
*******
*****
***
*
--------------------------------
#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-
--------------------------------
#include <iostream>
using namespace std;
int main()
{
int rows, coef = 1;
21 | P a g e
coef = coef*(i-j+1)/j;
Output-
Enter number of rows:
5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
--------------------------------
return 0;
22 | P a g e
}
Output-
1
12
123
1234
12345
#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);
Output-
23 | P a g e
--------------------------------
Output-
bye bye nit
bye bye nits
bye bye
Welcome to NIT
--------------------------------
Output-
Output-
hello
world
hello world
--------------------------------
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[100];
25 | P a g e
char k;
cout<<"enter your string:\n";
cin.getline(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
--------------------------------
#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;
}
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
--------------------------------
}
}
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<<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
--------------------------------
#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
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<<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-
--------------------------------
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
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
--------------------------------
// 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-
#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-
}
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-
36 | P a g e
after swapping the value of b is : 25
--------------------------------
}
}
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
--------------------------------
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-
}
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
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
--------------------------------
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
42.WAP to swap two numbers using pointers-
Output-
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-
Output-
Enter the number : 2
The square of given number is : 4
43 | P a g e
45.WAP 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-
--------------------------------
44 | P a g e
46.WAP 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
--------------------------------
#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-
46 | P a g e
48.WAP 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
47 | P a g e