PRACTICAL 1
OBJECTIVE:
Write a C++ program to swap two numbers without using the
third variable.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout << "Enter two numbers (a and b): ";
cin >> a >> b;
cout << "Before swapping: a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
}
OUTPUT:
PRACTICAL 2
OBJECTIVE:
Write a C++ program which takes input from user and shows
whether a number is positive, negative or zero.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "\n Enter a number: ";
cin >> n;
if (n > 0)
cout << n << " is a positive number" << endl;
else if (n < 0)
cout << n << " is a negative number" << endl;
else
cout << n << " is equal to zero" << endl;
return 0;
}
OUTPUT:
PRACTICAL 3
OBJECTIVE:
Write a program which takes input for 3 numbers and find the greatest
among them.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3, greatest;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
if (num1 >= num2 && num1 >= num3)
greatest = num1;
else if (num2 >= num1 && num2 >= num3)
greatest = num2;
else
greatest = num3;
cout << "The greatest number is: " << greatest << endl;
return 0; }
OUTPUT:
PRACTICAL 4
OBJECTIVE:
Write a program which takes marks as an input and calculates the
grades of students based on marks.
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
int marks;
char grade;
cout<<"\n Enter the marks (max 100):";
cin>>marks;
if (marks >= 80) grade = 'A';
else if (marks >=70) grade = 'B';
else if (marks >=60) grade = 'C';
else if (marks >=50) grade = 'D';
else grade = 'F';
cout<<"\n The grade is "<<grade;
return 0;
}
OUTPUT:
PRACTICAL 5
OBJECTIVE:
Write a program in C++ and show whether the input character is vowel
or consonant using switch case.
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"\n Enter an upper-case letter:";
cin>>ch;
switch(ch) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout<< "\n You entered a vowel.";
break;
default:
cout<< "\n You entered a consonant."; }
return 0;
}
OUTPUT:
PRACTICAL 6
OBJECTIVE:
Write a C++ program to calculate the sum of the first 10 positive odd
numbers.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int i, sum = 0;
for (int i = 1; i <= 10; i++)
{
sum += 2 * i - 1;
}
cout << "The sum of the first 10 positive odd numbers is: " << sum <<
endl;
return 0;
}
OUTPUT
PRACTICAL 7
OBJECTIVE:
Write the program to print the factorial of an input number.
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
int a,f=1;
cout<<"Enter any number:";
cin>>a;
cout<<"The factorial of" <<a<<"is"<<endl;
cout<<a<<"!=";
for(;a>0;a--) {
f*=a;
cout<<a<<"x"; }
cout<<"\b\="<<f;
return 0;
}
OUTPUT:
PRACTICAL 8
OBJECTIVE:
Generate the following pattern by using nested for loop.
**********
**********
**********
**********
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=10;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
OUTPUT:
PRACTICAL 9
OBJECTIVE:
Print the following format using nested for loop.
1
12
123
1234
12345
123456
1234567
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=7;i++)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
cout<<endl;
}
return 0;
}
OUTPUT:
PRACTICAL 10
OBJECTIVE:
Print the following output by using nested for loop.
*
***
*****
*******
*********
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
int i,j,n= 5;
for ( i = 1; i <=n; i++)
{
for (j = 1; j <= (2 * i - 1); j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
OUTPUT:
PRACTICAL 11
OBJECTIVE:
Print the following output by using nested for loop.
*
**
***
****
*****
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int i,j,k,n = 5;
for (i = 1; i <=n; i++)
{
for (j = 1; j <= n - i; j++)
{
cout << " ";
}
for (k = 1; k <= i; k++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
OUTPUT:
PRACTICAL 12
OBJECTIVE:
Write a C++ program which stores numeric values in a one-dimensional
array using for loop and finds the highest, lowest and average values.
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
if (n <= 0) {
cout << "Invalid number of elements." << endl;
return 1;
}
double numbers[n], sum = 0, highest, lowest;
cout << "Enter " << n << " numbers:" << endl;
for (int i = 0; i < n; ++i) {
cin >> numbers[i];
sum += numbers[i];
if (i == 0) {
highest = lowest = numbers[i];
}
else
{
if (numbers[i] > highest)
highest = numbers[i];
if (numbers[i] < lowest)
lowest = numbers[i];
}
}
cout << "Highest: " << highest << "\n Lowest: " << lowest
<< "\n Average: " << sum / n << endl;
return 0;
}
OUTPUT:
PRACTICAL 13
OBJECTIVE:
Write a program to make a signup interference which takes user name,
password, retype-password as an input and compare the password and
re-enter password of user for sign up, using strings library.
SOURCE CODE:
#include<iostream>
#include<string>
int main()
{
std::string username, password,confirmpassword;
std::cout<<"Enter your username:";
std::cin>>username;
std::cout<<"Enter your password (8 characters long):";
std::cin>>password;
if(password.length()!=8){
std::cout<<"password must be exactly 8 characters long"<<std::endl;
return 1;
}
std::cout<<"Re-enter your password to confirm:";
std::cin>>confirmpassword;
if(password==confirmpassword)
{
std::cout<<"Sign-up successful"<<std::endl;
std::cout<<"Password confirmed successfully"<<std::endl;
}
else
{
std::cout<<"Passwords do not match. Sign-up failed"<<std::endl;
}
return 0;
}
OUTPUT:
PRACTICAL 14
OBJECTIVE:
Write a program to add two matrices of order up to 4×4 using a two
dimensional array and show the ordered output in third matrix.
SOURCE CODE:
#include <iostream>
using namespace std;
int main() {
int rows, columns;
cout << "Enter the number of rows and columns for the matrix (up to 4
x 4):" << endl;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> columns;
int M1[4][4], M2[4][4], sum[4][4];
cout << "Enter the elements of the first matrix :" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cin >> M1[i][j];
}}
cout << "Enter the elements of the second matrix :" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cin >> M2[i][j];
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum[i][j] = M1[i][j] + M2[i][j];
}
}
cout << "The addition of the two matrices is:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << sum[i][j] << "\t";
}
cout << endl;
}
return 0;
}
OUTPUT:
PRACTICAL 15
OBJECTIVE:
Write a program involving use of user defined function to calculate
volume of cylinder, sphere and cube.
SOURCE CODE:
#include <iostream>
#include <cmath>
using namespace std;
double volcyl(double r, double h, double PI);
double volsph(double r, double PI);
double volcube(double s);
int main() {
const double PI = 3.14;
double r, h, s;
cout << "Enter radius and height of the cylinder: ";
cin >> r >> h;
cout << "Volume of the cylinder: " << volcyl(r, h, PI) << endl;
cout << "Enter radius of the sphere: ";
cin >> r;
cout << "Volume of the sphere: " << volsph(r, PI) << endl;
cout << "Enter side length of the cube: ";
cin >> s;
cout << "Volume of the cube: " << volcube(s) << endl;
return 0;
}
double volcyl(double r, double h, double PI) {
return PI * r * r * h;
}
double volsph(double r, double PI) {
return (4.0 / 3.0) * PI * pow(r, 3);
}
double volcube(double s) {
return pow(s, 3);
}
OUTPUT:
PRACTICAL 16
OBJECTIVE:
Write a program involving user defined function to calculate average of
given numbers.
SOURCE CODE:
#include<iostream>
using namespace std;
void avg(int a, int b,int c)
{
float avg=(a+b+c)/3.0;
cout<<"\n average:"<<avg;
}
int main()
{
float a,b,c;
cout<<"enter any three numbers:";
cin>>a>>b>>c;
cout<<"the numbers are:"<<a<< " "<<b<<" "<<c;
avg(a,b,c);
}
OUTPUT:
PRACTICAL 17
OBJECTIVE:
Write a program involving a user defined function to check whether the
input number is a prime number or not.
SOURCE CODE:
#include<iostream>
using namespace std;
void primeno();
int main()
{
primeno();
return 0;
}
void primeno()
{
int number, j, flag = 0;
cout << "Enter any integer and press enter to check: ";
cin >> number;
for(j = 2; j <= number/2; ++j)
{
if(number % j == 0)
{
flag = 1;
break; }
}
if (flag == 1)
{
cout << number << " is not a prime number.";
} else
{cout << number << " is a prime number.";
}}
OUTPUT:
PRACTICAL 18
OBJECTIVE:
Write a simple program using referencing and dereference operator the
find the value and address of each element of an array using pointers.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
float a[5]={1.9,2.9,3.1,3.7,4.2};
float*ptr;
ptr=a;
for(int i=0;i<5;i++)
{
cout << "The address of element " << i << " is: " << (ptr + i) << endl;
cout << "The value of element " << i << " is: " << *(ptr + i) << endl;
}
return 0;
}
OUTPUT:
PRACTICAL 19
OBJECTIVE:
Write a C++ program in which a class uses both public and
private access specifiers.
Source code:
#include<iostream>
using namespace std;
class Rectangle
{
private:
int length,width;
public:
Rectangle(int l, int w)
{
length=l;
width=w;
}
int area()
{
return length*width;
}
int peri()
{
return (2*length)+(2*width);
}
};
int main()
{
Rectangle r1(8,4);
Rectangle r2(12,3);
cout<<"Area of Rectangle 1 is: "<<r1.area()<<endl;
cout<<"Perimeter of Rectangle 1 is: "<<r1.peri()<<endl;
cout<<"Area of Rectangle 2 is: "<<r2.area()<<endl;
cout<<"Perimeter of Rectangle 2 is: "<<r2.peri()<<endl;
return 0;
}
OUTPUT:
PRACTICAL 20
OBJECTIVE:
Write a C++ program to implement the concept of multiple
inheritance in object oriented programming.
SOURCE CODE:
#include<iostream>
using namespace std;
class Mammal
{
public:
Mammal()
{
cout<<"I'm a Mammal."<<endl; } };
class MarineAnimal {
public:
MarineAnimal()
{
cout<<"I'm a Marine Animal."<<endl; } };
class BlueWhale:public Mammal, public MarineAnimal
{ public:
BlueWhale()
{ cout<<"I'm a Blue Whale, a Mammal and a Marine
Animal."<<endl; } };
int main()
{
BlueWhale BW;
return 0;
}
OUTPUT:
PRACTICAL 21
OBJECTIVE:
Write a C++ program to read and print employee information
using multiple inheritance.
SOURCE CODE:
#include<iostream>;
using namespace std;
class basicInfo
{
protected:
char name[30];
int empid;
char gender[10];
public:
void getBasicInfo(void)
{
cout<<"Enter Name: ";
cin.getline(name,30);
cout<<"Enter Emp. Id: ";
cin>>empid;
cout<<"Enter Gender: ";
cin.ignore(1);
cin.getline(gender,10);
}
};
class deptInfo
{
protected:
char deptname[30];
char assignwrk[30];
int comptime;
public:
void getDeptInfo(void)
{
cout<<"Enter Department Name: ";
cin.getline(deptname,30);
cout<<"Enter assigned work: ";
cin.getline(assignwrk,30);
cout<<"Enter time in hours to complete work: ";
cin>>comptime; } };
class employee : private basicInfo, private deptInfo
{
public:
void getEmployeeInfo(void)
{
cout<<"\t\t\tEnter Employee's Basic Information: \n"<<endl;
getBasicInfo();
cout<<"\n\n\t\t\tEnter Employee's Department Information: \
n\n";
getDeptInfo();
}
void printEmployeeInfo(void)
{
cout<<"\n\n\t\t\tEmployee's Information: "<<endl;
cout<<"\nBasic Information: "<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Employee ID: "<<empid<<endl;
cout<<"Gender: "<<gender<<endl;
cout<<"\nDepartment Information: "<<endl;
cout<<"Department Name: "<<deptname<<endl;
cout<<"Assigned Work: "<<assignwrk<<endl;
cout<<"Time to complete work: "<<comptime<<endl;
}
};
int main()
{
employee emp;
emp.getEmployeeInfo();
emp.printEmployeeInfo();
return 0;
}
OUTPUT: