[Document title]
1. // Your First C++ Program
#include <iostream>
int main() {
cout << "Hello World!";
return 0;}
2. // sum of two numbers
#include <iostream>
using namespace std;
int main()
{ int x, y, z;
X=5,y=10;
z = x + y;
cout<<"the sum of x and y = "<< z<<endl;
return 0;}
3. // sum of two numbers when the value is accepted from the user
#include <iostream>
using namespace std;
int main()
{ int x, y, z;
cout << "Enter two integers: ";
cin>>x>>y;
z = x + y;
cout<<"the sum of x and y = "<< z<<endl;
return 0;}
4. /* C++ Program to Find Sum and Average of three numbers */
#include<iostream>
using namespace std;
pg. 1 LAB-ONE
[Document title]
int main()
{ float a,b,c,sum,avg;
cout<<"Enter 1st number :: ";
cin>>a;
cout<<"\nEnter 2nd number :: ";
cin>>b;
cout<<"\nEnter 3rd number :: ";
cin>>c;
sum=a+b+c;
avg=sum/3;
cout<<"\nThe SUM of 3 Numbers [ "<<a<<" + "<<b<<" + "<<c<<" ] = "<<sum<<"\n";
cout<<"\nThe AVERAGE of 3 Numbers [ "<<a<<", "<<b<<", "<<c<<" ] = "<<avg<<"\n";
return 0;}
5. // find area of circle
#include<iostream>
using namespace std;
#define pi 3.14
int main()
{ float r=5,area;
area=pi*r*r;
cout<<"\n Area of circle is="<<area<<endl;
return 0;}
6. // C++ Program to calculate area of Parallelogram
#include <iostream>
using namespace std;
int main(){
float base, height, area;
cout << "Enter the base and height parallelogram\n";
pg. 2 LAB-ONE
[Document title]
cin >> base >> height;
// Area of parallelogram = base X height
area = base * height;
cout << "Area of parallelogram : " << area;
return 0;}
7. // C++ Program to calculate perimeter of Parallelogram
#include <iostream>
using namespace std;
int main(){
float side1, side2, perimeter;
cout << "Enter the length of adjacent sides of parallelogram\n";
cin >> side1 >> side2;
// Perimeter of parallelogram = 2X(side1 + side2)
perimeter = 2*(side1 + side2);
cout << "Perimeter of parallelogram : " << perimeter;
return 0;}
8. // Compute quotient and remainder
#include <iostream>
using namespace std;
int main()
{ int divisor, dividend, quotient, remainder;
cout << "Enter dividend: ";
cin >> dividend;
cout << "Enter divisor: ";
cin >> divisor;
quotient = dividend / divisor;
remainder = dividend % divisor;
cout << "Quotient = " << quotient << endl;
pg. 3 LAB-ONE
[Document title]
cout << "Remainder = " << remainder;
return 0;}
9. // C++ program to find the size of int, float, double and char in your System
#include <iostream.h>
int main()
{ cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;}
10. //Swap Numbers (Using Temporary Variable)
#include <iostream>
using namespace std;
int main()
{int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;}
11. //Swap Numbers Without Using Temporary Variables
#include <iostream.h>
int main()
{int a = 5, b = 10;
cout << "Before swapping." << endl;
pg. 4 LAB-ONE
[Document title]
cout << "a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;}
12. /* C++ Program to Display ASCII (American Standard Code For Information Interchange)Value of
a Character */
#include <iostream>
using namespace std;
int main()
{char a;
cout<<"Enter any Character :: ";
cin>>a;
cout<<"\nThe ASCII Value of Character ["<< a <<"] is :: "<<int(a)<<"\n";
return 0;}
13. /* C++ Program to Convert Days Into Years Weeks and Days */
#include<iostream>
using namespace std;
int main()
{ int y,d,w;
cout<<"Enter No. of days :: ";
cin>>d;
y=d/365;
d=d%365;
w=d/7;
d=d%7;
pg. 5 LAB-ONE
[Document title]
cout<<"\nNo. of Years: : "<<y<<"\nNo. of Weeks :: "<<w<<"\nNo. of Days :: "<<d<<"\n";
return 0;}
14. // frefix and postfix
#include<iostream>
using namespace std;
int main()
{ int x=10;
cout<<"\n x is:"<<x+5<<endl;
cout<<"\n x is:"<<++x+5<<endl;
cout<<"\n x is:"<<x+++5<<endl;
cout<<"\n x is:"<<--x+5<<endl;
cout<<"\n x is:"<<x--+5<<endl;
cout<<"\n x is:"<<x+5<<endl;
return 0;}
15. //Operators Precedence
#include <iostream>
using namespace std;
int main() {
int num1 = 5 - 17 * 6;
int num2 = 5 - (17 * 6);
int num3 = (5 - 17) * 6;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num3 = " << num3 << endl;
return 0;}
pg. 6 LAB-ONE