C++ Assignment - No-2: Name-Shaikh Altamash Asif Roll - No-04
C++ Assignment - No-2: Name-Shaikh Altamash Asif Roll - No-04
C++ Assignment - No-2: Name-Shaikh Altamash Asif Roll - No-04
No-2
Name-Shaikh Altamash Asif Roll.No-04
Q1.WAP in C++ to exchange the content of two variables using call by reference.
#include<iostream.h>
#include<conio.h>
class swapp
{
int a,b;
public:
void accept()
{
cout<<"Enter the value of a and b:"<<endl;
cin>>a>>b;
}
void display()
{
cout<<"A="<<a<<"\t"<<"B="<<b<<endl;
swap(a,b);
}
void swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
};
void main()
{
swapp s;
clrscr();
s.accept();
cout<<"Before Swapping:"<<endl;
s.display();
cout<<"After Swapping:"<<endl;
s.display();
getch();
}
/*OUTPUT
Enter the value of a and b:
12
23
Before Swapping:
A=12 B=23
After Swapping:
A=23 B=12
*/
int a=10;
int b=20;
int c=30;
int d=5,e;
e=(a+b)*c/d;//(10+20)*30/5;
cout<<"Value of (a+b)*c/d is = "<<e<<endl;
e=((a+b)*c)/d;//((10+20)*30/5;
cout<<"Value of ((a+b)*c)/d is = "<<e<<endl;
e=(a+b)*(c/d);//(10+20)*(30/5);
cout<<"Value of (a+b)*(c/d) is = "<<e<<endl;
e=a+(b*c)/d;//10+(20*30)/5;
cout<<"Value of a+(b*c)/d is = "<<e<<endl;
getch();
}
/*OUTPUT
Value of (a+b)*c/d is = 180
Value of ((a+b)*c)/d is = 180
Value of (a+b)*(c/d) is = 180
Value of a+(b*c)/d is = 130
*/
Name-Shaikh Altamash Asif Roll.No-04
Q3.WAP to access a global variable when there is a local variable with same name.
#include<iostream.h>
#include<conio.h>
int a=10;
void main()
{
int a=20;
clrscr();
cout<<"\nLocal Variable a= "<<a<<"\nGlobal Variable a= "<<::a;
::a=30;
cout<<"\nLocal Variable a= "<<a<<"\nGlobal Variable a= "<<::a;
getch();
}
/*OUTPUT
Local Variable a= 20
Global Variable a= 10
Local Variable a= 20
Global Variable a= 30
*/
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"This is Line 1:"<<endl;
cout<<"This is Line 2:"<<endl;
cout<<"This is Line 3:"<<endl;
cout<<"This is Line 4:"<<endl;
cout<<"AAA"<<endl<<"BBB"<<endl<<"CCC"<<endl;
getch();
}
/*OUTPUT
This is Line 1:
This is Line 2:
This is Line 3:
This is Line 4:
AAA
BBB
CCC
*/
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
clrscr();
char str[5]="ABCD";
char str1[5]="IJ";
cout<<str<<setw(5)<<str1;
getch();
}
/*OUTPUT
ABCD IJ
*/
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
int a=123;
cout<<setw(5)<<setfill('$')<<a;
getch();
}
/*OUTPUT
$$123
*/
Name-Shaikh Altamash Asif Roll.No-04
Q7.WAP a C++ program to demonstrate example of setprecision manipulator.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
float a=9.775412;
cout<<setprecision(2)<<a;
getch();
}
/*OUTPUT
9.78
*/
case 4:cout<<cur<<123<<endl<<endl;
break;
case 5:break;
}
}while(ch!=5);
getch();
}
/*OUTPUT
1.Column width 10
2. Right justification
3.Trailing zeros:
4.Fill unused spaces with '*'
Enter your choice:
3
Before using showpoint
9.9900 prints as: 9.99
1.Column width 10
2. Right justification
3.Trailing zeros:
4.Fill unused spaces with '*'
Enter your choice:
4
**123
1.Column width 10
2. Right justification
3.Trailing zeros:
4.Fill unused spaces with '*'
Enter your choice:
5
*/