KEMBAR78
S.No. Projects Page No. 1 | PDF | C++ | Namespace
0% found this document useful (0 votes)
11 views32 pages

S.No. Projects Page No. 1

The document outlines a series of programming projects and tasks primarily in C++ and includes code snippets for each task. Topics covered include loops, conditional statements, functions, classes, operator overloading, and memory management. The document serves as a guide for implementing various programming concepts and techniques.

Uploaded by

Ankit Kumar
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)
11 views32 pages

S.No. Projects Page No. 1

The document outlines a series of programming projects and tasks primarily in C++ and includes code snippets for each task. Topics covered include loops, conditional statements, functions, classes, operator overloading, and memory management. The document serves as a guide for implementing various programming concepts and techniques.

Uploaded by

Ankit Kumar
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/ 32

S.No. PROJECTS PAGE NO.

1. Develop a program that uses a


`while` loop to print all prime
numbers between 1 and 100
2. Write a program that reads three
numbers and uses an `nesting if-else`
statement to determine the largest
number.
3. Create a function that takes an array
of integers and returns the sum of all
prime numbers.
4. Write a recursive function to calculate
the Fibonacci series up to `n` terms.

5. Implement a function to search for a


value in a two dimensional array and
return its position.
6. Implement a program to calculate the
square root of a number using a
mathematical function.

1
INDEX

2
7. Describe how you would use
mathematical functions to solve a
quadratic equation.

8. Write a program to create an array of


objects for a class that stores student
information.

9. Write a program to implement this


pointer in c++.

10. Write a program to demonstrate the


use of a friend function to access
private members of a class.

11. Write a program that demonstrates


object assignment and passing
objects to functions in C++.

12. Develop a program that


demonstrates the use of dynamic
allocation operators `new` and
`delete` to manage memory in a
class.

13. Create a class with a destructor that


releases dynamically allocated
memory.

3
14. Develop a program that
demonstrates function overloading
by creating multiple functions with
the same name but different
parameters.

15. Create a base class Person with


member variables for name and
age, and a method displayInfo().
Derive a class Student from Person
with an additional member variable
for the
student ID and override the
displayInfo() method to include the
student.

16. Write a program to overload the '+'


operator to add two complex
numbers.

17. Implement a program that


demonstrates the concept of virtual
functions and late binding.

18. Write a program to overload the


'==' operator using a friend
function to compare two strings.

4
19. Write a class Student with private
data members name and age.
Implement a copy constructor that
initializes a new Student object with
the values of an existing Student
object. Test your copy constructor in
a main function.

20 Print the following pattern :


A
ABC
ABCDE
ABCDEFG
ABCDE
ABC
A

5
1. Develop a program that uses
a `while` loop to print all
prime numbers between 1
and 100
#include <iostream>
using namespace std;

int main() {
int num = 2;

while (num <= 100) {


bool isPrime = true;
int i = 2;
while (i * i <= num) {
if (num % i == 0) {
isPrime = false;
break;
}
i++;
}

if (isPrime) {
cout << num << " ";
}

num++;
}

return 0;
}

6
2. Write a program that reads
three numbers and uses an
`nesting if-else` statement to
determine the largest number.
#include <iostream>
using namespace std;

int main() {
int a, b, c;

cout << "Enter three numbers: ";


cin >> a >> b >> c;
if (a >= b) {
if (a >= c)
cout << "The largest number is: " << a <<
endl;
else
cout << "The largest number is: " << c <<
endl;
} else {
if (b >= c)
cout << "The largest number is: " << b <<
endl;
else
cout << "The largest number is: " << c <<
endl;
}

return 0;
}

7
3. Create a function that takes an
array of integers and returns the
sum of all prime numbers.
#include <iostream>
using namespace std;
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i*i <= n; ++i) {
if (n % i == 0)
return false;
}
return true;
}int sumOfPrimes(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; ++i) {
if (isPrime(arr[i])) {
sum += arr[i];
}
}
return sum;
}

int main() {
int arr[] = {3, 4, 5, 6, 7, 8, 9, 11};
int size = sizeof(arr) / sizeof(arr[0]);

int primeSum = sumOfPrimes(arr, size);

cout << "Sum of prime numbers in the array: " <<


primeSum << endl;

return 0;
}

8
4. Implement a function to search
for a value in a two dimensional
array and return its position.
#include <iostream>
using namespace std;

bool search(int arr[][3], int rows, int target, int &r, int &c)
{
for (int i = 0; i < rows; ++i)
for (int j = 0; j < 3; ++j)
if (arr[i][j] == target) {
r = i; c = j;
return true;
}
return false;
}

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}, x, r, c;
cout << "Enter value: "; cin >> x;
if (search(arr, 2, x, r, c))
cout << "Found at (" << r << ", " << c << ")\n";
else
cout << "Not found\n";
return 0;
}

9
5. Implement a program to
calculate the square root of a
number using a
mathematical function.
#include <iostream>

#include <cmath>

using namespace std;

int main() {

double num;

cout << "Enter a number: ";

cin >> num;

if (num < 0)

cout << "Square root of negative number is not real.\n";

else

cout << "Square root: " << sqrt(num) << endl;

10
6. Describe how you would use
mathematical functions to solve
a quadratic equation.
#include <iostream>

#include <cmath>

using namespace std;

int main() {

double a, b, c, D, x1, x2;

cout << "Enter a, b, c: ";

cin >> a >> b >> c;

D = b*b - 4*a*c;

if (D > 0) {

x1 = (-b + sqrt(D)) / (2*a);

x2 = (-b - sqrt(D)) / (2*a);

cout << "Real and distinct roots: " << x1 << ",
" << x2 << endl;

} else if (D == 0) {

x1 = -b / (2*a);

cout << "Real and equal root: " << x1 << endl;

} else {

double realPart = -b / (2*a);

double imagPart = sqrt(-D) / (2*a);

11
cout << "Complex roots: " << realPart << " ± "
<< imagPart << "i" << endl;

return 0;

12
7. Write a program to implement
this pointer in c++.

#include <iostream>
using namespace std;

class Student {
private:
int id;
string name;

public:
// Setter method using 'this' pointer
void setData(int id, string name) {
this->id = id; // 'this->id' refers to class member
this->name = name; // 'name' is the parameter
}

void display() {
cout << "ID: " << id << ", Name: " << name <<
endl;
}
};

int main() {
Student s1;
s1.setData(101, "Rahul");
s1.display();

return 0;
}

13
8. Write a program to create an
array of objects for a class that
stores student information
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
string name;
public:
void input() {
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter name: ";
cin >> name;
}
void display() {
cout << "Roll No: " << rollNo << ", Name: " << name <<
endl;
}
};

int main() {
const int size = 3; // Number of students
Student students[size];

cout << "\n--- Enter Student Information ---\n";


for (int i = 0; i < size; ++i) {
cout << "\nStudent " << i + 1 << ":\n";
students[i].input();
}

cout << "\n--- Displaying Student Information ---\n";


for (int i = 0; i < size; ++i) {
students[i].display();
}

return 0;
}

14
9. Write a program to demonstrate
the use of a friend function to
access private members of a
class.

#include <iostream>
using namespace std;
class Student {
private:
string name;
int marks;
public:
Student(string n, int m) {
name = n;
marks = m;
}
friend void displayStudentInfo(Student s);
};

void displayStudentInfo(Student s) {
// Can access private members directly
cout << "Name: " << s.name << ", Marks: " << s.marks << endl;
}

int main() {
Student s1("Rahul", 85);
displayStudentInfo(s1); // Call friend function

return 0;
}

15
10. Write a program that
demonstrates object
assignment and passing objects
to functions in C++.
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string n = "Unknown", int a = 0)
{
name = n;
age = a;
}
void display() const {
cout << "Name: " << name << ", Age:
" << age << endl;
}
void setData(string n, int a) {
name = n;
age = a;
}
};
void showByValue(Person p) {
cout << "Inside showByValue(): ";
p.display();
}

16
void showByReference(const Person
&p) {
cout << "Inside showByReference():
";
p.display();
}
int main() {
Person person1("jonathan", 25);
cout << "Original object (person1): ";
person1.display();
Person person2;
person2 = person1;
cout << "After assignment (person2
= person1): ";
person2.display();
showByValue(person1);
showByReference(person1);
return 0;
}

17
11. Create a class with a destructor that
releases dynamically allocated memory.

#include <iostream>
using namespace std;

class Sample {
private:
int* data;
int size;

public:
Sample(int s) {
size = s;
data = new int[size];
for (int i = 0; i < size; ++i)
data[i] = i * 10;
}

void display() const {


for (int i = 0; i < size; ++i)
cout << data[i] << " ";
cout << endl;
}

~Sample() {
delete[] data;
cout << "Destructor called. Memory released." <<
endl;
}
};

int main() {
Sample obj(5); obj.display();
return 0;
}

18
12.student ID and override the
displayInfo() method to include
the student.

#include <iostream>
using namespace std;
class Person {
protected:
string name;
int age;
public:
Person(string n, int a) {
name = n;
age = a;
}

virtual void displayInfo() {


cout << "Name: " << name << ", Age: " << age << endl;
}
};

class Student : public Person {


private:
string studentID;

public:
Student(string n, int a, string id) : Person(n, a) {
studentID = id;
}

void displayInfo() override {


cout << "Name: " << name << ", Age: " << age << ",
Student ID: " << studentID << endl;
}
};

int main() {
Student s("Rahul", 18, "R123456");
s.displayInfo();
return 0;

19
13. Write a program to overload
the '+' operator to add two complex
numbers.
#include <iostream>
using namespace std;

class Complex {
private:
float real;
float imag;

public:
Complex(float r = 0, float i = 0) {
real = r;
imag = i;
}

Complex operator+(const Complex& other) {


return Complex(real + other.real, imag + other.imag);
}

void display() const {


cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(3.5, 2.5);
Complex c2(1.5, 4.5);
Complex result = c1 + c2;
result.display();
return 0;
}

20
14. Implement a program that demonstrates
the concept of virtual functions and late
binding.

#include <iostream>

using namespace std;

class Animal {

public:

virtual void sound() {

cout << "Animal makes a sound" << endl;

};

class Dog : public Animal {

public:

void sound() override {

cout << "Dog barks" << endl;

};

class Cat : public Animal {

public:

void sound() override {

cout << "Cat meows" << endl;

21
};

int main() {

Animal* a;

Dog d;

Cat c;

a = &d;

a->sound();

a = &c;

a->sound();

return

22
15. Write
a program to overload the '==' operator
using a friend function to compare two strings.
#include <iostream>
#include <cstring>

using namespace std;

class MyString {

private:

char str[100];

public:

MyString(const char* s = "") {

strcpy(str, s);

friend bool operator==(const MyString& s1, const MyString& s2);

void display() const {

cout << str << endl;

};

bool operator==(const MyString& s1, const MyString& s2) {

return strcmp(s1.str, s2.str) == 0;

23
int main() {

MyString s1("hello");

MyString s2("hello");

MyString s3("world");

if (s1 == s2)

cout << "s1 and s2 are equal" << endl;

else

cout << "s1 and s2 are not equal" << endl;

if (s1 == s3)

cout << "s1 and s3 are equal" << endl;

else

cout << "s1 and s3 are not equal" << endl;

return 0;

24
16. Write a class student privite. Implement a copy
constructor that initializes a new Student object
with the existing Student object. Test your copy
constructor in a main function.

#include <iostream>

using namespace std;

class Student {

private:

string name;

int age;

public:

Student(string n, int a) {

name = n;

age = a;

Student(const Student& s) {

name = s.name;

age = s.age;

25
void display() const {

cout << "Name: " << name << ", Age: " << age << endl;

};

int main() {

Student s1("Rahul", 18);

Student s2 = s1;

s1.display();

s2.display();

return 0;

26
17. Print the following pattern :
A
ABC
ABCDE
ABCDEFG
ABCDE
ABC
A
#include <iostream>
using namespace std;

int main() {
int n = 4;

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


for (int j = n - i; j > 0; j--)
cout << " ";
for (char ch = 'A'; ch < 'A' + 2 * i - 1; ch++)
cout << ch << " ";
cout << endl;
}

for (int i = n - 1; i >= 1; i--) {


for (int j = 0; j < n - i; j++)
cout << " ";
for (char ch = 'A'; ch < 'A' + 2 * i - 1; ch++)
cout << ch << " ";
cout << endl;
}

return 0;
}

27
18. Write a recursive function to calculate the
Fibonacci series up to `n` terms.

#include<iostream>
using namespace std;

int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cout << fibonacci(i) << " ";
}
return 0;
}

28
Develop a program that demonstrates the use of
19.
dynamic allocation operators `new` and `delete` to
manage memory in a class.

#include<iostream>
using namespace std;

class Student {
string* name;
int* age;

public:
Student(const string& n, int a) {
name = new string(n);
age = new int(a);
}

void display() {
cout << "Name: " << *name << ", Age: " << *age << endl;
}

~Student() {
delete name;
delete age;
}

29
};

int main() {
Student* s = new Student("Rahul", 21);
s->display();
delete s;
return 0;
}

30
20. Develop
a program that demonstrates function
overloading by creating multiple functions with the
same name but different parameters.

#include<iostream>
using namespace std;

class Demo {
public:
void display() {
cout << "No parameters" << endl;
}

void display(int x) {
cout << "Integer parameter: " << x
<< endl;
}

void display(double x) {
cout << "Double parameter: " << x
<< endl;
}

void display(string str, int times) {


for (int i = 0; i < times; i++) {
cout << str << endl;
}
}

31
};

int main() {
Demo obj;
obj.display();
obj.display(10);
obj.display(3.14);
obj.display("Function Overloading", 2);
return 0;
}

32

You might also like