First Pu Lab Manual
First Pu Lab Manual
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int a , b ,temp;
clrscr( );
cout<<"enter two numbers"<<endl;
cin>>a>>b;
cout<<"before interchange a="<<a<<endl;
cout<<"before interchange b="<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<"after interchange a="<<a<<endl;
cout<<"after interchange b="<<b<<endl;
getch( );
}
output:
enter two numbers
10
20
before interchange a= 10
before interchange b= 20
after interchange a= 20
after interchange b= 10
1
// 1b.Write a C++ program to interchange the values of two
variables without using a third variable
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int a , b;
clrscr( );
cout<<"enter two numbers"<<endl;
cin>>a>>b;
cout<<"before interchange a="<<a<<endl;
cout<<"before interchange b="<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"after interchange a="<<a<<endl;
cout<<"after interchange b="<<b<<endl;
getch( );
}
output:
enter two numbers
10
20
before interchange a= 10
before interchange b= 20
after interchange a= 20
after interchange b= 10
2
// 2.Write a C++ program to area and circumference of a circle
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
float radius, area, circum;
clrscr( );
cout<<"enter the radius"<<endl;
cin>>radius;
area=3.142*radius*radius;
circum=2* 3.142* radius;
cout<<" the area is" <<area<<endl;
cout<<" the circumference is" <<circum<<endl;
getch( );
}
output:
enter the radius 10
the area is 314
the circumference is 62.84
3
//3.Write a C++ program to the area of a triangle given three
sides
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<math.h>
void main( )
{
double s1,s2,s3,s, area;
clrscr( );
cout<<"enter the length of 3 sides"<<endl;
cin>>s1>>s2>>s3;
s = (s1+s2+s3)/2;
area= (sqrt(s*(s-s1)*(s-s2)*(s-s3)));
cout<<" the area is " <<area<<endl;
getch( );
}
output:
enter the length of 3 sides
3
4
5
the area is 6
4
// 4. Write a C++ program to find the largest ,smallest and
second largest using simple if statement
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int a,b,c, largest, seclargest, smallest;
clrscr( );
cout<<"enter 3 numbers"<<endl;
cin>>a>>b>>c;
largest=a;
if(b>largest)
largest=b;
if(c>largest)
largest=c;
smallest=a;
if(b<smallest)
smallest=b;
if(c<smallest)
smallest=c;
seclargest=(a+b+c)-(largest+smallest);
cout<<"largest = "<<largest<<endl;
cout<<"smallest = "<<smallest<<endl;
cout<<"second largest = "<<seclargest<<endl;
getch( );
}
output:
enter 3 numbers
45
15
91
largest = 91
smallest = 15
second largest = 45
5
//5.Write a program to check whether a given year is leap year
or not using if..else statement
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int year;
clrscr( );
cout<<"enter the year"<<endl;
cin>>year;
if(year%4 == 0 && year %100 != 0 ||year %400 == 0)
cout<<"It is a leap year"<<endl;
else
cout<<"It is not a leap year" <<endl;
getch( );
}
output 1:
enter the year
1996
It is a leap year
output 2:
enter the year
1995
It is not a leap year
6
//6.Write a program to accept a character. Determine whether
the character is a lower case or upper case letter using if..else..if
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
char cs;
clrscr( );
cout<<"enter the character"<<endl;
cin>>cs;
if(cs>= 'A' && cs<='Z')
cout<<"It is an upper case character"<<endl;
else if(cs>= 'a' && cs<='z')
cout<<"It is a lower case character" <<endl;
else
cout<<"It is not an alphabet"<<endl;
getch( );
}
Output 1:
enter the character
T
It is an upper case character
Output 2:
enter the character
b
It is a lower case character
Output 3:
enter the character
%
It is not an alphabet
7
//7. Write a C++ program to find the sum of all digits of a
number using while statement
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int n,m,sum=0,rem;
clrscr( );
cout<<"enter the number"<<endl;
cin>>n;
m=n;
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
cout<<"the sum of digits in"<<m<<"is"<<sum<<endl;
getch( );
}
Output:
enter the number
123
the sum of digits in 123 is 6
8
//8. Write a C++ program to check whether a given number is
armstrong number using do..while statement
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int n,temp,rem,sum=0;
clrscr( );
cout<<"enter a number"<<endl;
cin>>n;
temp=n;
do
{
rem = temp%10;
sum=sum + rem * rem * rem;
temp=temp/10;
}while(temp!=0);
if(sum==n)
cout<<n<<" It is an armstrong number"<<endl;
else
cout<<n <<" It is not an armstrong number"<<endl;
getch( );
}
output 1:
enter a number
153
It is an armstrong number
output 2:
enter a number
123
It is not an armstrong number
9
//9. Write a C++ program to find factorial of number using for
statement
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
double n,fact=1,i;
clrscr( );
cout<<"enter the number"<<endl;
cin>>n;
for(i=1;i<=n;i++)
fact=fact *i;
cout<<n<<"!= "<<fact;
getch( );
}
Output 1:
enter the number
5
5 != 120
Output 1:
enter the number
0
0 != 1
10
//10. Write a C++ program to generate the fibonacci sequence
using for statements
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int n, first=0, second=1, i, next;
clrscr( );
cout<<"enter the limit"<<endl;
cin>>n;
cout<<first<<setw(4)<<second;
next=first+second;
for(i=2;next<=n;i++)
{
cout<<setw(4)<<next;
first=second;
second=next;
next=first+second;
}
getch( );
}
11
//11. Write a C++ program to find the sum and average of n
numbers
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int a[50],i,n,sum=0;
float avg;
clrscr( );
cout<<"enter the size of the array?"<<endl;
cin>>n;
cout<<"enter the array elements:"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
sum = sum + a[i];
avg = sum/n;
cout<<"sum = "<<sum<<endl;
cout<<"average = "<<avg<<endl;
getch( );
}
output :
enter the size of the array
3
enter the array elements:
10
20
30
sum = 60
average = 20
12
// 12. Write a C++ program to find the position of a given
number in an array
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int a[50],i,pos = -1,ele,n;
cout<<"enter the number of elements"<<endl;
cin>>n;
cout<<"enter the array elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter the search element"<<endl;
cin>>ele;
for(i=0;i<n;i++)
if(ele==a[i])
{
pos=i;
break;
}
if(pos>=0)
cout<<ele<<"is present at position "<<pos<<endl;
else
cout<<ele<<"is not present "<<endl;
getch( );
}
output 1 : output 2:
enter the number of enter the number of elements
elements 3 3
enter the array enter the array elements:
10
elements: 10 20
20 30
30 enter the search element
enter the search 50
element 10 50 is not present
10 is present at position 0
13
//13 .Write a C++ program to find sum of two compatible matrix
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main( )
{
int a[5][5],b[5][5],s[5][5],i,j,m1,n1,m2,n2;
clrscr( );
cout<<"enter the order of first matrix"<<endl;
cin>>m1>>n1;
cout<<"enter the order of second matrix"<<endl;
cin>>m2>>n2;
if(m1==m2 && n1==n2)
{
cout<<"enter the elements of first matrix"<<endl;
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
cin>>a[i][j];
cout<<"enter the elements of second matrix"<<endl;
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
cin>>b[i][j];
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
s[i][j]= a[i][j]+b[i][j];
cout<<"the resultant matrix after addition"<<endl;
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
cout<<setw(5)<<s[i][j];
cout<<endl;
}
}
else
cout<<"matrices are not compatible for addition"<<endl;
getch( );
}
14
output 1:
output 2:
enter the order of first matrix
2
2
enter the elements of second matrix
2
3
matrices are not compatible for addition
15
//14 . Write a program to input the marks of four students.
calculate the total percentage and output the result as either
"first class" or "second class" or "pass class" or "fail"
using switch statement
class range(%)
first class between 60 and
100 % second class between 50
and 59 % pass class between 40
and 49 %
fail less than 40 %
#include<iostream
.h>
#include<conio.h>
#include<iomanip.
h> void main( )
{
int m1,m2,m3,m4,total,choice; float percentage;
clrscr( ); cout<<"enter the marks of 4 subjects";
cin>>m1>>m2>>m3>>m4;
total=m1+m2+m3+m4;
percentage=total/4.0;
choice=(int)percentage/10;
cout<<"total marks
="<<total<<endl;
cout<<setprecision(2);
cout<<"percentage="<<percentage<
<endl; cout<<"the grade of the
student is ";
switch(choice)
{
case 10:
case 9:
case 8:
case 7:
case 6: cout<<"first class";
break;
case 5: cout<<"second class”;
break;
case 4: cout<<"pass class”;
16
break;
case 3:
case 2:
case 1: cout<<"fail"<<endl;
}
getch( );
}
Output 1:
enter the marks of 4 subjects60
90
90
100
total marks = 340
percentage = 85
the grade of the student is firstclass
17
//15 .Write a C++ program to check whether a given string is
palindrome or not
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<iomanip.h>
void main( )
{
char s[50], r[50];
clrscr( );
cout<<"enter the string:"<<endl;
gets(s);
strcpy(r,s);
strrev(r);
if(strcmpi(s,r)==0)
cout<<s<<" is palindrome"<<endl;
else
cout<<s<<" is not palindrome"<<endl;
getch( );
}
output 1
enter the string:
madam
madam is palindrome
output 2
enter the string:
hello
hello is not palindrome
18
//16. Write a C++ Program to find GCD and LCM of two
numbers using functions
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h> int
gcd(int,int);
void main( )
{
int a,b,g,lcm;
clrscr( );
cout<<"enter two numbers"<<endl;
cin>>a>>b;
g=gcd(a,b);
lcm=(a*b)/g;
cout<<"gcd ="<<g<<endl;
cout<<"lcm ="<<lcm<<endl;
getch( );
} output:
int gcd(int m,int n) enter two numbers
{ 24
int rem; 18
while(n!=0) gcd = 6
{ lcm = 72
rem=m % n;
m=n;
n=rem;
}
return m;
}
19
//17. Write a C++ program to find X ^y using functions
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<math.h>
void main( )
{
double x,y,p;
clrscr( );
cout<<"Enter x and y "<<endl;
cin>>x>>y;
p=pow(x,y);
cout<<x<<"^"<<y<<"="<<p<<endl;
getch( );
}
Output 1:
Enter x and y
3
3
3^3 = 27
Output 2:
Enter x and y
5
0
5^0 = 1
Output 3:
Enter x and y
16
1
16^1 = 16
20
//18.Write a program to input id and name of all students in a
class and display (using structure)
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int id;
char name[20];
};
void main( )
{
student s[50]; int
i,n;
clrscr( );
cout<<"Enter the number of students"<<endl;
cin>>n;
cout<<"enter the student details"<<endl;
for(i=0;i<n;i++)
{
cout<<"enter student id"<<endl;
cin>>s[i].id;
cout<<"enter the student name"<<endl;
gets(s[i].name);
}
cout<<endl;
cout<<"id" << "\t" <<"name"<<endl;
for(i=0;i<n;i++)
cout<<s[i].id<<"\t"<<s[i].name<<endl;
getch();
}
Output:
Enter the number of students
1
enter the student details
21
enter student id
123
enter the student name
Sowmya venkatesh
id name
123 Sowmya venkatesh
22
Algorithms
//1a.
step 1: input a,b
step 2: temp=a
a=b
b=temp
step 3:output a,b
step 4: stop
//1b.
step 1: input a,b
step 2: a= a+b
b= a-b
a= a-b
step 3:output a,b
step 4: stop
//2
step 1: input radius
step 2: area= 3.142 * radius * radius
circum= 2 * 3.142 * radius
step 3:output area, circum
step 4: stop
//3
step 1: input s1,s2,s3
step 2: s=(s1+s2+s3)/2
area= sqrt(s*(s-s1)*(s-s2)*(s-s3))
step 3:output area
step 4: stop
23
//4
step 1: input a,b,c
step 2: largest=a;
step 3: if(b>largest)
largest=b;
end if
step 4: if(c>largest)
largest=c;
end if
step 5: smallest=a;
step 6: if(b<smallest)
smallest=b;
end if
step 7: if(c<smallest)
smallest=c;
end if
step 8: seclargest=(a+b+c)-(largest+smallest)
step 9: output largest,seclargest,smallest
step 10: stop
//5.
step 1: input year
step 2: if(year%4 == 0 && year %100 != 0 ||year %400 == 0)
output "leap year"
else
output "not leap year"
step 3: stop
24
//6.
step 1: input cs;
step 2: if(cs>= 'A' && cs<='Z')
output"it is an upper case character"
else if(cs>= 'a' && cs<='z')
output "it is an lower case character"
else
output"it is not an alphabet"
step 3: stop
//7.
step 1: input n
step 2: sum=0
step 3: while(n!=0)
{
rem=n%10
sum=sum+rem
n=n/10
}
step 4: output sum
step 5: stop
//8.
step 1: input n
step 2: temp =n
sum=0
step 3: do
{
rem = temp%10
sum=sum + rem * rem * rem
temp=temp/10
}while(temp!=0);
step 4:if(sum==n)
output" It is an armstrong number"
else
output" It is not an armstrong number"
25
//9.
step 1: input n
step 2: fact = 1
step 3: for i= 1 to n
fact= fact * i
for end
step 4: output fact
step 5: stop
//10.
step 1: input n
step 2: first=0, second=1
step 3: output first, second
step 4: next=first+second
step 5: for(i=2;next<=n;i++)
output next;
first=second
second=next
next=first+second
for end
step 6: stop
//11.
step 1: input n
step 2: for i=0 to n-1
input a[i]
for end
step 3: sum=0
step 4: for i=0 to n-1
sum =sum + a[i]
for end
26
//12.
step 1: input n, ele
step 2: for i=0 to n-1
input a[i]
for end
step 3: pos= -1
step 4: for i=0 to n-1
if(a[i] == ele)
pos = a[i]
GOTO STEP 5
end if
step 5: if(pos>=0)
output ele "is present at position ",pos
else
output ele "is not present "
step 6: stop
27
//13.
step 1: input m1,n1
step 2: input m2,n2
step 3: for i=0 to m1-1
for j=0 to n1-1
input a[i][j]
for end
for end
step 7: stop
28
//14.
step 1: input m1,m2,m3,m4
step 2: total=m1+m2+m3+m4
step 3: percentage=total/4.0
step 4: choice=(int)percentage/10
step 5:switch(choice)
{
case 10:
case 9:
case 8:
case 7:
case 6: output "first class"
break;
case 5: output "second class"
break;
case 4: output "pass class"
break;
case 3:
case 2:
case 1: output "fail"
}
step 6: stop
//15.
step 1: input s[50]
step 2: strcpy(r,s);
step 3:strrev(r);
step 4: if(strcmpi(s,r)==0)
output " is palindrome"
else
output " is not palindrome"
step 5: stop
29
//16.
step 1: input a,b
step 2: g=gcd(a,b);
step 3: lcm=(a*b)/g;
step 4: output g, lcm
step 5: stop
gcd(m,n):
while(n!=0)
{
rem=m % n
m=n
n=rem
}
return m
//17.
step 1: input x,y
step 2: p=pow(x,y);
step 3:output p
step 4: stop
30
//18.
Step 3: for(i=0;i<n;i++)
Input s[i].id
gets(s[i].name)
Step 4: for(i=0;i<n;i++)
output s[i].id
s[i].name
Step 5: stop
31
Flowcharts
1a. 1b
Start
Start
Input a, b Input a, b
temp = a
a=a+b
a=b b=a–b
b = temp a=a–b
Output a, b
Output a, b
Stop
Stop
33 | P a g e
2 3.
Start Start
s = (s1 + s2 + s3) / 2
area = 3.142* radius*radius
area = sqrt(s*(s – s1)*(s – s2)*(s – s3)
circum = 2*3.142*radius
Stop
Stop
34 | P a g e
4.
Start
Input a, b, c
largest = a
F
if (b>largest)
largest = b
F
if (c > largest)
largest = c
smallest = a
F
if (b < smallest)
smallest = b
F
if (c < smallest)
T
smallest = c
Stop
35 | P a g e
5.
Start
Input year
Stop
36 | P a g e
6. Start
Input cs
T output upper
if (cs > =’A’ & & cs < = ’Z’)
case character
F
T output lower
else if (cs > = ’a’ & & cs < = ’z’)
case character
Stop
37 | P a g e
7.
Start
Input n
sum = 0
m=n
while(n!=0)
rem = n % 10
sum = sum + rem
n = n/10
Output sum
Stop
38 | P a g e
8.
Start
Input n
sum = 0
temp = n
rem = temp % 10
sum = sum + rem*rem*rem
temp = temp/10
T
while (temp!=0)
T F
if(sum==n)
Stop
39 | P a g e
9. 10.
Start
Start
Input n
Input n
fact=1 first = 0
second =1
fact = fact *i
first= second
second= next
Stop next= first + second
Stop
40 | P a g e
11.
Start
Input n
sum = 0
input a[i]
sum = sum+a[i]
average = sum/n
Stop
41 | P a g e
12.
Start
Input n,ele
pos = -1
input a[i]
T
if(ele= a[i] )
pos = i
F
T
if(pos > =0)
Stop
42 | P a g e
43 | Page
Start
F output not
if(m1 == m2 &&n1==n2 compatible
T
13. for(i = 0; i < m1; i++)
input a[i][j]
input b[i][j]
i
for(i = 0; i < m1; i++)
Output s[i][j]
j
44 | P a g e
i
Stop
Start
14.
Input m1,m2,m3,m4
switch(choice)
Output “first class” Output “second Output “pass class” output “fail”
class”
Stop
45 | P a g e
Start
15.
input gets(s)
strcpy(r,s)
strrev(r)
T if ( strcmpi(s,r)= = 0) F
Stop
46 | P a g e
16.
Start
Input a,b
g = gcd (a,b)
g = gcd (a,b)
lcm =(a*b)/g
while(n!=0)
Stop
return m
47 | P a g e
17.
Start
Input x,y
p = pow(x,y)
Output p
Stop
48 | P a g e |
Start
18.
declare structure
with data members
id and name
declare structure
variable
Stop
49 | P a g e