0% found this document useful (0 votes)
12 views

annual_cpp_file

Uploaded by

shreyansh YEOLE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

annual_cpp_file

Uploaded by

shreyansh YEOLE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Answer:

Source Code
#include<bits/stdc++.h>
using namespace std;
void printPatern1(int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 7; j++)
{
if (j < (4 - i) || j >= 7 - (4 - i))
{
cout << (char)('A' + j);
}
else
{
cout << " ";
}
}
cout << endl;
}
}

void printPatern2(int n)
{
for (int i = 0; i < n; i++)
{
// spaces
for (int j = 0; j < (n - i - 1); j++)
{
cout << " ";
}

// stars
for (int k = 0; k <= i; k++)
{
cout << "* ";
}

// spaces
for (int j = 0; j < (n - i - 1); j++)
{
cout << " ";
}

cout << endl;


}
}

void printPatern3(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << endl;
}
}
void printPatern4(int n)
{
for (int i = 0; i < n; i++)
{
cout << pow(11, i) << endl;
}
}
int main()
{
/*Practical 1*/

printPatern1(5);
printPatern2(3);
printPatern3(4);
printPatern4(5);
return 0;
}
Output
Answer:
Source Code
#include<bits/stdc++.h>
using namespace std;
/*Practical 2*/
void pattern_printer()
{
int n=0;
cout << "Enter code of patterns: " << endl;
cin >> n;
switch (n)
{
case 11:
printPatern1(5);
break;

case 12:
{
int i = 0;
while (i < 5)
{
int j = 0;
while (j < 7)
{
if (j < (4 - i) || j >= 7 - (4 - i))
{
cout << (char)('A' + j);
}
else
{
cout << " ";
}
j++;
}
i++;
cout<<endl;
}
}
break;

case 13:
{
int i = 0;
do
{
int j = 0;
do
{
if (j < (4 - i) || j >= 7 - (4 - i))
{
cout << (char)('A' + j);
}
else
{
cout << " ";
}
j++;
}while(j < 7);
i++;
cout<<endl;
}while (i < 5);
}
break;

case 21:
printPatern2(3);
break;
case 22:
{
int i =0;
while (i < 3)
{
int j = 0;
int k = 0;
//print spaces
while (j< (2 - i))
{
cout<<" ";
j++;
}

while (k<=i)
{
cout<<"* ";
k++;
}

i++;
cout<<endl;
}

}
break;
case 23:
{
int i =0;
do
{
int j = -1;
int k = 0;
//print spaces
do
{
cout<<" ";
j++;
}while (j< (2 - i));

do
{
cout<<"* ";
k++;
}while (k<=i);

i++;
cout<<endl;
}while (i < 3);
}
break;
default:
cout<<"Wrong input"<<endl;
break;
}
}
int main()
{
pattern_printer();
return 0;
}

Answer:
Source Code
#include<bits/stdc++.h>
using namespace std;
/*Practical 3*/
void printNumberSystem(int n){
cout << left << setw(10) << "Decimal"<< setw(10)
<< "Octal"<< setw(10) << "Hexadecimal" << endl;

for (int i = 0; i <= n; ++i) {


cout << setw(10) << i << setw(10) << oct << i
<< setw(10) << hex << i << endl;

// Reset stream format to decimal for the next


iteration
cout << dec;
}
}
int main()
{
printNumberSystem(10);
return 0;
}
Output
Answer:

Source Code
#include<bits/stdc++.h>
using namespace std;

int** add_matrices(int a[3][3], int b[3][3], int


**destination){
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
destination [i][j] = a[i][j] + b[i][j];
}
}
return destination;
}

int** sub_matrices(int a[3][3], int b[3][3], int


**destination){
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
destination [i][j] = a[i][j] - b[i][j];
}
}
return destination;
}

int** mul_matrices(int a[3][3], int b[3][3], int


**destination) {
// Initialize the destination matrix with 0
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
destination[i][j] = 0;
// Multiply corresponding elements and sum them
up
for (int k = 0; k < 3; k++) {
destination[i][j] += a[i][k] * b[k][j];
}
}
}
return destination;
}

int main()
{
int matrix1[3][3]= {{1,2,3},
{4,5,6},
{7,8,9}};
int matrix2[3][3]= {{1,2,3},
{4,5,6},
{7,8,9}};

// Allocate memory for the result matrix


int **addResult = new int*[3];
for (int i = 0; i < 3; i++) {
addResult[i] = new int[3];
}

add_matrices(matrix1,matrix2,addResult);

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout<<addResult[i][j]<<" ";
}
cout << endl;
}
cout<<endl<<endl;
int **subResult = new int*[3];
for (int i = 0; i < 3; i++) {
subResult[i] = new int[3];
}

sub_matrices(matrix1,matrix2,subResult);

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout<<subResult[i][j]<<" ";
}
cout << endl;
}

int **mulResult = new int*[3];


for (int i = 0; i < 3; i++) {
mulResult[i] = new int[3];
}

cout<<endl<<endl;

mul_matrices(matrix1,matrix2,mulResult);

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout<<mulResult[i][j]<<" ";
}
cout << endl;
}

cout<<endl<<endl;
// Deallocate memory
for (int i = 0; i < 3; i++) {
delete[] addResult[i];
delete[] subResult[i];
delete[] mulResult[i];
}
delete[] addResult;
delete[] subResult;
delete[] mulResult;
return 0;
}
Output
Answer:

Source Code
#include<bits/stdc++.h>
using namespace std;
//Practical 6(a)
void reverseString(string &s)
{
int n =s.size();
for (int i = 0; i < n; i++)
{
if(i<(n/2))
{
int temp = s[i];
s[i]= s[n-i-1];
s[n-i-1]= temp;
}
}

//Practical 6(b)
int count_characters(string &s)
{
int count =0;
int i =0;
while (s[i] != '\0')
{
count++;
i++;
}
return count;
}

//Practical 6(c)
void CopyString(char source[], char destination[])
{
int i=0;
while (source[i] != '\0')
{
destination[i]=source[i];
i++;
}
while (destination[i] != '\0')
{
destination[i]= ' ';
i++;
}

//Practical 6(d)
void count_Vow_Con(char str[])
{
int countVow=0;
int countCon=0;
int i = 0;
while (str[i]!='\0')
{
if (str[i] == 'a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||
str[i]=='u')
{
countVow++;
}
else if (str[i] != ' ')
{
countCon++;
}
i++;
}
cout<<"Number of vowels: "<<countVow<<endl;
cout<<"Number of consonent: "<<countCon<<endl;
}
int main()
{
char s1[] = "This is source";
char s2[] = "This is destination";

string s ="olleh";
reverseString(s);
cout <<"Reversed string: "<<s << endl;

cout <<"Number of characters in the string is :


"<<count_characters(s) << endl;

cout<<"s2 before copyString: "<<s2<<endl;


CopyString(s1,s2);
cout<<"s2 after copyString: "<<s2<<endl<<endl;

count_Vow_Con(s1);
return 0;
}

Output
Answer:

Source Code
#include <iostream>
using namespace std;
class Student
{
private:
int rollNo;
string name;
string subjects[3];
int max_Marks[3];
int min_Marks[3];
int obtained_Marks[3];

public:
Student(int rn, string nm)
{
rollNo = rn;
name = nm;
}
void setData(string sub1, string sub2, string sub3)
{
subjects[0] = sub1;
subjects[1] = sub2;
subjects[2] = sub3;
for (int i = 0; i < 3; i++)
{
cout << "Enter max marks for sub" << i << ": "
<< endl;
cin >> max_Marks[i];
cout << "Enter min marks for sub" << i << ": "
<< endl;
cin >> min_Marks[i];
cout << "Enter obtained marks for sub" << i<<
": " << endl;
cin >> obtained_Marks[i];
}
}
void getData()
{
cout << "\nStudent Roll Number: " << rollNo <<
endl;
cout << "Student Name: " << name << endl;
for (int i = 0; i < 3; i++)
{
cout << "\nSubject: " << subjects[i] << endl;
cout << "Maximum Marks " << max_Marks[i] <<
endl;
cout << "Minimum Marks: " << min_Marks[i] <<
endl;
cout << "Obtained Marks: " << obtained_Marks[i]
<< endl;
}
}
~Student()
{
cout<<"Destructor called"<<endl;
}
};
int main()
{
Student s1(100,"shazam");
s1.setData("Hindi","English","Maths");
s1.getData();
return 0;
}
Output
Answer:

Source Code
#include <iostream>
using namespace std;
class Student
{
private:
int rollNo;
string name;
string subjects[3];
int max_Marks[3];
int min_Marks[3];
int obtained_Marks[3];

public:
Student()
{
cout << "\nEnter your roll no.: ";
cin >> rollNo;
cout << "Enter your Name: ";
cin >> name;
}
void setData(string sub1, string sub2, string sub3)
{
subjects[0] = sub1;
subjects[1] = sub2;
subjects[2] = sub3;
cout << name << " Enter your data: " << endl;
for (int i = 0; i < 3; i++)
{
cout << "Enter max marks for " << subjects[i]
<< ": ";
cin >> max_Marks[i];
cout << "Enter min marks for " << subjects[i]
<< ": ";
cin >> min_Marks[i];
cout << "Enter obtained marks for " <<
subjects[i] << ": ";
cin >> obtained_Marks[i];
cout << endl
<< endl;
}
cout << name << "'s Data successfully inputted."
<< endl;
}
void getData()
{
cout << "\nStudent Roll Number: " << rollNo <<
endl;
cout << "Student Name: " << name << endl;
for (int i = 0; i < 3; i++)
{
cout << "\nSubject: " << subjects[i] << endl;
cout << "Maximum Marks " << max_Marks[i] <<
endl;
cout << "Minimum Marks: " << min_Marks[i] <<
endl;
cout << "Obtained Marks: " << obtained_Marks[i]
<< endl;
}
}
int getRollNo()
{
return rollNo;
}

~Student()
{
// cout << "Destructor called" << endl;
}
};
int main()
{
int input;
Student s[3];
s[0].setData("Hindi", "Maths", "English");
cout <<
"==================================
=============" << endl;
s[1].setData("Hindi", "Maths", "English");
cout <<
"==================================
=============" << endl;
s[2].setData("Hindi", "Maths", "English");

for (int i = 0; i < 3; i++)


{
s[i].getData();

cout<<"=============================
==================="<<endl;
}

// get data by querying the rollno of the student


int searchRollNo;
cout << "\nEnter roll number of the student whose
result you want to display: ";
cin >> searchRollNo;

bool found = false;


for (int i = 0; i < 3; i++)
{
if (s[i].getRollNo() == searchRollNo)
{
s[i].getData();
found = true;
break;
}
}
if (!found)
{
cout << "\nStudent with Roll Number " <<
searchRollNo << " not found.\n";
}

return 0;
}
Output
/
Answer:

Source Code
#include <iostream>
using namespace std;

class Sarray
{
private:
int arr[5];
int size = sizeof(arr) / sizeof(int);

public:
Sarray()
{
cout << "Enter numbers in array elements: " <<
endl;
for (int i = 0; i < size; i++)
{
cout << "at index " << i << endl;
cin >> arr[i];
}
}
void sortArr()
{
int mini;
for (int i = 0; i <= (size - 2); i++)
{
mini = i;
for (int j = i; j <= (size - 1); j++)
{
if (arr[j] < arr[mini])
{
mini = j;
}
}
int temp = arr[mini];
arr[mini] = arr[i];
arr[i] = temp;
}
cout << "Sorted Array: " << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
}
void findLargestElement()
{
int max = arr[0];
for (int i = 1; i < size; i++)
{
if (max < arr[i])
{
max = arr[i];
}
}
cout << "Largest Element of array: " << max <<
endl;
}
void searchValue(int val)
{
bool found = false;
for (int i = 0; i < size; i++)
{

if (val == arr[i])
{
cout<<"This value is present in the array at
index: "<<i<<endl;
found = true;
}
if (found)
{
break;
}
}

}
~Sarray() {}
};
int main()
{
Sarray s1;
s1.sortArr();
cout<<endl;
s1.findLargestElement();
cout<<endl;
s1.searchValue(34);
return 0;
}
Output
Answer:

Source Code
#include <bits/stdc++.h>
using namespace std;
struct student
{
string name;
int rollNo;
string subject[3];
int marks[3];
void setData()
{
cout << "Enter your Name: ";
cin >> name;
cout << "Enter your rollNo.: ";
cin >> rollNo;

for (int i = 0; i < 3; i++)


{
cout << "Enter name of Subject " << (i + 1) << "
: ";
cin >> subject[i];
}

for (int i = 0; i < 3; i++)


{
cout << "Enter marks of " <<subject[i] << " : ";
cin >> marks[i];
}
}
void getData()
{
cout<<"\t\tStudent Details\n"<<endl;
cout << "Student's Name: " << name << endl;
cout << "Student's rollNo: " << rollNo << endl;
for (int i = 0; i < 3; i++)
{
cout << "Marks of " << name << " in " <<
subject[i] << " is " << marks[i] << endl;
}
}
};

int main()
{
student s1;
s1.setData();
cout<<endl<<endl;
s1.getData();
return 0;
}
Output
Answer:

Source Code
#include<iostream>
using namespace std;
class Polar
{
private:
float radius;
float angle;
public:
Polar(float radius, float angles);
Polar(const Polar &obj);
void display() const {
cout << "Radius: " << radius << ", Angle: " <<
angle << " degrees" << endl;
}
~Polar();
};

Polar::Polar(float radius, float angles)


{
this->radius = radius;//The this pointer is a pointer to
the current object, and pointers require the use of the ->
operator to access members.
this->angle = angles;
cout << "Object intialized via Parameterized
Constructor" << endl;
}
Polar::Polar(const Polar &obj)
{
// Shallow Copy
this->radius = obj.radius;
this->angle = obj.angle;
cout<<"\nObject intialized via copy
construtor"<<endl;
}

Polar::~Polar()
{
}

int main()
{
Polar p1(3.5,40);
cout<<"p1 Details:"<<endl;
p1.display();

Polar p2(p1);
cout<<"p2 Details:"<<endl;
p2.display();
return 0;
}
Output
Answer:

Source Code
#include<iostream>
using namespace std;
class Polar
{
private:
float radius;
float angle;
public:
Polar(float radius, float angles);
Polar(const Polar &obj);
void display() const {
cout << "Radius: " << radius << ", Angle: " <<
angle << " degrees" << endl;
}
~Polar();
};

Polar::Polar(float radius= 2, float angles = 20)


{
this->radius = radius;
this->angle = angles;
cout << "\nObject intialized via Parameterized
Constructor" << endl;
}
Polar::Polar(const Polar &obj)
{
// Shallow Copy
this->radius = obj.radius;
this->angle = obj.angle;
cout<<"\nObject intialized via copy
construtor"<<endl;
}

Polar::~Polar()
{
}

int main()
{
Polar p0;
cout<<"p0 Details:"<<endl;
p0.display();

Polar p1(3.5,40);
cout<<"p1 Details:"<<endl;
p1.display();

Polar p2(p1);
cout<<"p2 Details:"<<endl;
p2.display();
return 0;
}
Output
Answer:

Source Code
#include<iostream>
#include<math.h>
using namespace std;
class Calculate
{
private:
/* data */
public:
static int area(int length, int bredth)
{
return length * bredth;
}
static float area(float base, float height)
{
return 0.5*(base * height);
}
static float area(float radius)
{
return M_PI*(radius * radius);
}
};

int main()
{

cout<<"The area of triangle whose base is 2.23cm and


height is 3.22cm:
is :"<<Calculate::area(2.23f,3.22f)<<"cm
sqare"<<endl;
cout<<"The area of a rectangle of length 2cm and
breadth 54cm is: "<<Calculate::area(2,54)<<"cm
sqare"<<endl;
cout<<"The area of circle whose radius is 2.23cm is :
"<<Calculate::area(2.23f)<<"cm sqare"<<endl;
return 0;
}
Output

Answer:

Source Code
#include<iostream>
using namespace std;
class Operation
{
private:
/* data */
public:
static void swap(int &x, int &y)
{
int temp = x;
x= y;
y =temp;
}
static void swap(float &x, float &y)
{
float temp = x;
x= y;
y =temp;
}
};

int main()
{
int a=10,b=30;
float c=4.3,d=3.4;
cout<<"Values before swap:\n a = "<<a<<"\t\tb =
"<<b<<"\n c = "<<c<<"\t d = "<<d<<endl;
Operation::swap(a,b);
Operation::swap(c,d);
cout<<"Values after swap:\n a = "<<a<<"\t\t b =
"<<b<<"\n c = "<<c<<"\t d = "<<d<<endl;
return 0;
}
Output

You might also like