Content
2. Addition of two number Input using cin example in c++ // Input using cin example
3. Program to explain Cascading of Insertion ( << )
4. /* This program is used to explain Multiple line comment and Single line comment.*/
7. Program to Swap two numbers using Third Variable
1. Simple cout example in c++
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
cout << "Welcome Program.";
getch();
return 0;
}
Output:-
Welcome Program.
---------------------------------------------------------------------
2. Addition of two number Input using cin example in c++
// Input using cin example
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n1, n2, s;
cout << "Enter First Num : ";
cin >> n1;
cout << "Enter Second Num : ";
cin >> n2;
s = n1 + n2;
cout << "\n Sum = ";
cout << s;
getch();
}
Output:-
Enter First Num : 10
Enter Second Num : 20
Sum = 30
----------------------------------------------------------------------
3. Program to explain Cascading of Insertion ( << )
// operator and Cascading of Extraction ( >> ) operator#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int num1, num2, sum;
cout << "\n Enter Two Numbers = ";
cin >> num1 >> num2;
sum = num1 + num2;
cout << "\n " << num1 << " + " << num2 << " = " << sum;
getch();
}
Output:
Enter Two Numbers = 23 93
23 + 93 = 116
----------------------------------------------------------------------
4. /*
This program is used to explain Multiple line
comment and Single line comment.
*/
#include <iostream.h>#include <conio.h>
void main()
{
clrscr();
float r, h ; // Variable Declaration.
cout << " Enter Radius & Height of Cylinder = ";
cin >> r >> h ; // Reading data from KB.
float v = 3.14 * r * r * h ; // Calculation of volume.
cout << "\n\n Volume of Cylinder = " << v << '\n';
getch();
}
Output:
Enter Radius & Height of Cylinder = 10 10
Volume of Cylinder = 3140
----------------------------------------------------------------------
5. Use of endl example
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a = 12, b = 123, c = 1234;
cout << "Numbers : " << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
getch();
}
Output:
Numbers :
12
123
1234
----------------------------------------------------------------------
6. using setw() example.
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int main ()
{
clrscr();
cout<<setw(10)<<"Ram" << endl;
cout<<setw(10)<<"Shyam Lala";
getch();
return 0;
}
Output:
Ram
Shyam Lala
----------------------------------------------------------------------
7. Program to Swap two numbers using Third Variable
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n1, n2, t;
cout << "\n Enter Two Numbers = ";
cin >> n1 >> n2;
cout << "\n n1 = " << n1 << ", n2 = " << n2;
t = n1;
n1 = n2;
n2 = t;
cout << "\n n1 = " << n1 << ", n2 = " << n2;
getch();
}
Output:
Enter Two Numbers = 10 20
n1 = 10, n2 = 20
n1 = 20, n2 = 10
----------------------------------------------------------------------
8. Swap two values using two variable
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"Enter the value of a and b:"<<endl;
cin>>a>>b;
cout<<"\nBefore Swap:\n"<<" A:"<<a<<"\n B:"<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"After Swap:\n"<<" A:"<<a<<"\n B:"<<b<<endl;
getch();
}
Output:
Enter the value of a and b:
15 25
Before Swap:
A:15
B:25
After Swap:
A:25
B:15
----------------------------------------------------------------------
9. Program to show structure of C++ Program
// File Inclusion Section
#include <iostream.h>
#include <conio.h>
// Class Declaration & Definition
class Distance
{
private:
int feet;
float inches;
public:
Distance()
{
feet = 0;
inches = 0.0;
}
Distance(int ft, float in)
{
feet = ft;
inches = in;
}
void getDist();
void showDist();
void addDist(Distance, Distance);
};
// Member Function Definition
void Distance :: getDist()
{
cout << "Enter Feet & Inches : ";
cin >> feet >> inches;
}
void Distance :: showDist()
{
cout << feet << "\'-" << inches << "\"";
}
void addDist( Distance dd1, Distance dd2 )
{
feet = dd1.feet + dd2.feet;
inches = dd1.inches + dd2.inches;
if( inches >= 12.0 )
{
inches -= 12.0;
feet++;
}
}
// Global Declaration & Definition Section
// main Function
void main()
{
clrscr();
Distance d1;
Distance d2(10, 6.0);
Distance d3;
d1.getDist();
d1.showDist();
cout << " + ";
d2.showDist();
cout << " = ";
d3.addDist(d1, d2);
d3.showDist();
getch();
}
Output:
Enter Feet & Inches : 15 9.0
15'-9" + 10'-6" = 26'-3"
----------------------------------------------------------------------
0 Comments