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

c++

Uploaded by

mohammadqadir085
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

c++

Uploaded by

mohammadqadir085
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Assignment-2

Name-Mohammed Qadir
Roll no.-204735
Q1) Create a Matrix class. Write a menu-driven program to perform following Matrix
operations:
a. Sum
b. Transpose
Ans:- #include <iostream>
using namespace std;
class Matrix {
private:
int rows, cols;
int** data;
public:
Matrix(int r, int c) : rows(r), cols(c) {
data = new int*[rows];
for (int i = 0; i < rows; ++i) {
data[i] = new int[cols]; }}
void input() {
cout << "Enter elements of the matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> data[i][j]; }}}
void display() const {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << data[i][j] << " ";}
cout << endl; }}
Matrix operator+(const Matrix& other) const {
Matrix result(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[i][j] = data[i][j] + other.data[i][j]; }}
return result; }
Matrix transpose() const {
Matrix result(cols, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[j][i] = data[i][j];} }
return result; }
~Matrix() {
for (int i = 0; i < rows; ++i) {
delete[] data[i];}
delete[] data; }};
int main() {
int rows, cols;
cout << "Enter the number of rows and columns of the matrix: ";
cin >> rows >> cols;
Matrix mat1(rows, cols);
cout << "Matrix 1:" << endl;
mat1.input();
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Sum of two matrices\n";
cout << "2. Transpose of the matrix\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
Matrix mat2(rows, cols);
cout << "Matrix 2:" << endl;
mat2.input();
Matrix sum = mat1 + mat2;
cout << "Sum of matrices:" << endl;
sum.display();
break;}
case 2: {
Matrix transposed = mat1.transpose();
cout << "Transpose of the matrix:" << endl;
transposed.display();
break;}
case 3:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl; }
} while (choice != 3);
return 0;}
Q2: Create a Matrix class. Write a menu-driven program to perform following
Matrix operations:
a. Product
b. Transpose
Ans:- #include <iostream>
using namespace std;
class Matrix {
private:
int rows, cols;
int** data;
public:
Matrix(int r, int c) : rows(r), cols(c) {
data = new int*[rows];
for (int i = 0; i < rows; ++i) {
data[i] = new int[cols]; }}
void input() {
cout << "Enter elements of the matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> data[i][j]; }}}
void display() const {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << data[i][j] << " ";}
cout << endl; }}
Matrix operator*(const Matrix& other) const {
if (cols != other.rows) {
throw invalid_argument("Matrix dimensions do not match for
multiplication."); }
Matrix result(rows, other.cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < other.cols; ++j) {
result.data[i][j] = 0;
for (int k = 0; k < cols; ++k) {
result.data[i][j] += data[i][k] * other.data[k][j]; }}}
return result; }
Matrix transpose() const {
Matrix result(cols, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[j][i] = data[i][j]; }}
return result; }
~Matrix() {
for (int i = 0; i < rows; ++i) {
delete[] data[i]; }
delete[] data; }};
int main() {
int rows, cols;
cout << "Enter the number of rows and columns of the matrix: ";
cin >> rows >> cols;
Matrix mat1(rows, cols);
cout << "Matrix 1:" << endl;
mat1.input();
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Product of two matrices\n";
cout << "2. Transpose of the matrix\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
Matrix mat2(rows, cols);
cout << "Matrix 2:" << endl;
mat2.input();
try {
Matrix product = mat1 * mat2;
cout << "Product of matrices:" << endl;
product.display();
} catch (const invalid_argument& e) {
cout << e.what() << endl;
}
break;
}
case 2: {
Matrix transposed = mat1.transpose();
cout << "Transpose of the matrix:" << endl;
transposed.display();
break;
}
case 3:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl; }
} while (choice != 3);
return 0;}
Q3: Define a class Person having name as a data member. Inherit two classes
Student and Employee from Person. Student has additional attributes as
course, marks and year and Employee has department and salary. Write
display() method in all the three classes to display the corresponding
attributes.
Ans:- #include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
public:
Person(string n) : name(n) {}
virtual void display() const {
cout << "Name: " << name << endl; }};
class Student : public Person {
private:
string course;
int marks;
int year;
public:
Student(string n, string c, int m, int y) : Person(n), course(c), marks(m), year(y) {}
void display() const override {
Person::display();
cout << "Course: " << course << endl;
cout << "Marks: " << marks << endl;
cout << "Year: " << year << endl; }};
class Employee : public Person {
private:
string department;
float salary;
public:
Employee(string n, string d, float s) : Person(n), department(d), salary(s) {}
void display() const override {
Person::display();
cout << "Department: " << department << endl;
cout << "Salary: " << salary << endl; }};
int main() {
Student student("Alice", "Computer Science", 90, 2024);
Employee employee("Bob", "HR", 50000);
cout << "Student Details:" << endl;
student.display();
cout << "\nEmployee Details:" << endl;
employee.display();
return 0;}

Q4: Create a Triangle class. Add exception handling statements to ensure the
following conditions: all sides are greater than 0 and sum of any two sides is
greater than the third side.
Ans:- #include <iostream>
#include <stdexcept>
using namespace std;
class Triangle {
private:
double side1, side2, side3;
public:
Triangle(double s1, double s2, double s3) {
if (s1 <= 0 || s2 <= 0 || s3 <= 0) {
throw invalid_argument("All sides must be greater than 0."); }
if (s1 + s2 <= s3 || s1 + s3 <= s2 || s2 + s3 <= s1) {
throw invalid_argument("The sum of any two sides must be greater than
the third side."); }
side1 = s1;
side2 = s2;
side3 = s3; }
void display() const {
cout << "Triangle sides: " << side1 << ", " << side2 << ", " << side3 << endl;}};
int main() {
try {
double s1, s2, s3;
cout << "Enter the sides of the triangle: ";
cin >> s1 >> s2 >> s3;
Triangle triangle(s1, s2, s3);
triangle.display();
} catch (const invalid_argument& e) {
cout << "Error: " << e.what() << endl; }
return 0;}
Q5: Create a class Student containing fields for Roll No., Name, Class, Year and
Total Marks. Write a program to store 5 objects of Student class in a file.
Retrieve these records from the file and display them.
Ans:- #include <iostream>
#include <fstream>
using namespace std;
class Student {
private:
int rollNo;
string name;
string studentClass;
int year;
int totalMarks;
public:
Student() {}
Student(int r, string n, string c, int y, int m) : rollNo(r), name(n),
studentClass(c), year(y), totalMarks(m) {}
void input() {
cout << "Enter Roll No.: ";
cin >> rollNo;
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Class: ";
getline(cin, studentClass);
cout << "Enter Year: ";
cin >> year;
cout << "Enter Total Marks: ";
cin >> totalMarks; }
void display() const {
cout << "Roll No.: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Class: " << studentClass << endl;
cout << "Year: " << year << endl;
cout << "Total Marks: " << totalMarks << endl; }
friend ofstream& operator<<(ofstream& ofs, const Student& s);
friend ifstream& operator>>(ifstream& ifs, Student& s); };
ofstream& operator<<(ofstream& ofs, const Student& s) {
ofs << s.rollNo << endl;
ofs << s.name << endl;
ofs << s.studentClass << endl;
ofs << s.year << endl;
ofs << s.totalMarks << endl;
return ofs; }
ifstream& operator>>(ifstream& ifs, Student& s) {
ifs >> s.rollNo;
ifs.ignore();
getline(ifs, s.name);
getline(ifs, s.studentClass);
ifs >> s.year;
ifs >> s.totalMarks;
return ifs; }
int main() {
Student students[5];
ofstream outFile("students.txt");
cout << "Enter details of 5 students:" << endl;
for (int i = 0; i < 5; ++i) {
students[i].input();
outFile << students[i]; }
outFile.close();
ifstream inFile("students.txt");
Student s;
cout << "\nDetails of students from file:" << endl;
while (inFile >> s) {
s.display();
cout << endl; }
inFile.close();
return 0; }

Q6: Create a class Vehicle having mileage (int data type) and price (float data
type) as public data members.
(i) Create a sub class Car which is publicly inherited from Vehicle class having
fuel type(char data type) and warranty (int data type) as public data
members.
(ii) Derive another subclass Audi which is publicly inherited from class Car
having data member model type (char data type) and member function
getdata() which takes the input from the user and putdata() to display all
the information of Audi car.
(iii) Define main() function to print all information (mileage, price, fuel type,
warranty, model type) about an object of class Audi.
Ans:- #include <iostream>
using namespace std;
class Vehicle {
public:
int mileage;
float price;
Vehicle(int m, float p) : mileage(m), price(p) {}};
class Car : public Vehicle {
public:
char fuelType;
int warranty;
Car(int m, float p, char f, int w) : Vehicle(m, p), fuelType(f), warranty(w) {}};
class Audi : public Car {
public:
char modelType;
Audi(int m, float p, char f, int w, char mt) : Car(m, p, f, w), modelType(mt) {}
void getdata() {
cout << "Enter mileage: ";
cin >> mileage;
cout << "Enter price: ";
cin >> price;
cout << "Enter fuel type (P for Petrol, D for Diesel): ";
cin >> fuelType;
cout << "Enter warranty (in years): ";
cin >> warranty;
cout << "Enter model type: ";
cin >> modelType; }
void putdata() const {
cout << "Mileage: " << mileage << endl;
cout << "Price: " << price << endl;
cout << "Fuel Type: " << fuelType << endl;
cout << "Warranty: " << warranty << endl;
cout << "Model Type: " << modelType << endl; }};
int main() {
Audi myAudi(0, 0.0, ' ', 0, ' ');
myAudi.getdata();
cout << "\nAudi Car Details:" << endl;
myAudi.putdata();
return 0; }

Q7: Write a program to create a class Complex which has two data members: real
part a
(integer) and imaginary part b (float). Write the following member functions for
this class:
(i) A default constructor to initialize a and b to 0.
(ii) A parameterized constructor to initialize a and b to passed values.
(iii) A function print() to display the complex number in the form a + ib i.e. for a = 4
and b = 5 the output of print should be 4 +i5.
Ans:- #include <iostream>
using namespace std;
class Complex {
private:
int a;
float b;
public:
Complex() : a(0), b(0.0) {}
Complex(int real, float imag) : a(real), b(imag) {}
void print() const {
cout << a << " +i" << b << endl; }};
int main() {
Complex c1;
Complex c2(4, 5.0);
c1.print();
c2.print();
return 0; }

Q8: Define a class Student with following data members:


Name: Name of the student
Age Age of the student
M1 : Marks in English
M2 : Marks in Hindi
M3: Marks in Mathematics
M4 : Marks in Science
This class should support the following methods:
(i) get_Data() to get student's information from user.
(ii) display() to show student's information.
Ans:- #include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
int m1, m2, m3, m4;
public:
void get_Data() {
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
cout << "Enter Marks in English: ";
cin >> m1;
cout << "Enter Marks in Hindi: ";
cin >> m2;
cout << "Enter Marks in Mathematics: ";
cin >> m3;
cout << "Enter Marks in Science: ";
cin >> m4; }
void display() const {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Marks in English: " << m1 << endl;
cout << "Marks in Hindi: " << m2 << endl;
cout << "Marks in Mathematics: " << m3 << endl;
cout << "Marks in Science: " << m4 << endl;
}};
int main() {
Student s;
cin.ignore();
s.get_Data();
s.display();
return 0;
}

Q9: Write a C++ program that defines a class Item with the following
specifications:
Private Data Members: code (depicting item unique number), price (price of the
item).Public Member Functions: getData(): A function defined outside the class
that assigns valuesto the code and price variables, displayData(): A function to
display the values of code and price on the console.
Ans:- #include <iostream>
using namespace std;
class Item {
private:
int code;
float price;
public:
void getData();
void displayData() const; };
void Item::getData() {
cout << "Enter code: ";
cin >> code;
cout << "Enter price: ";
cin >> price;
}
void Item::displayData() const {
cout << "Code: " << code << endl;
cout << "Price: " << price << endl;
}
int main() {
Item item;
item.getData();
item.displayData();
return 0;
}

Q10: Define a class Circle with a member function to calculate the circumference
of a circle.
Use the formula circumference = 2 * 3.14 * radius. The radius should be a private
member variable.
Ans:- #include <iostream>
using namespace std;
class Circle {
private:
float radius;
public:
void setRadius(float r) {
radius = r; }
float circumference() const {
return 2 * 3.14 * radius; }};
int main() {
Circle c;
float r;
cout << "Enter radius: ";
cin >> r;
c.setRadius(r);
cout << "Circumference: " << c.circumference() << endl;
return 0;
}

Q11: Create a class Rectangle with private members length and width. Write a
member function to calculate and display the area of the rectangle.
Ans:- #include <iostream>
using namespace std;
class Rectangle {
private:
float length;
float width;
public:
void setDimensions(float l, float w) {
length = l;
width = w; }
float area() const {
return length * width; }
void displayArea() const {
cout << "Area: " << area() << endl; }};
int main() {
Rectangle rect;
float l, w;
cout << "Enter length and width: ";
cin >> l >> w;
rect.setDimensions(l, w);
rect.displayArea();
return 0;
}

Q12: Write a simple program using a function template to swap two variables of
any data type.
Ans:- #include <iostream>
using namespace std;
template <typename T>
void swapVariables(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
float a = 5.5, b = 10.5;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swapVariables(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;
cout << "Before swap: a = " << a << ", b = " << b << endl;
swapVariables(a, b);
cout << "After swap: a = " << a << ", b = " << b << endl;
return 0;}

}
Q13: Define a function template to calculate the maximum of two numbers
(integers or floats).
Ans:- #include <iostream>
using namespace std;
template <typename T>
T maximum(T a, T b) {
return (a > b) ? a : b; }
int main() {
int x = 5, y = 10;
float a = 5.5, b = 10.5;
cout << "Maximum of " << x << " and " << y << " is " << maximum(x, y) << endl;
cout << "Maximum of " << a << " and " << b << " is " << maximum(a, b) << endl;
return 0;
}

Q14: Create a template function to calculate the square of a number. Test the
function with different data types.
Ans:- #include <iostream>
using namespace std;
template <typename T>
T square(T x) {
return x * x; }
int main() {
int x = 5;
float y = 5.5;
cout << "Square of " << x << " is " << square(x) << endl;
cout << "Square of " << y << " is " << square(y) << endl;
return 0;
}
Q15: Write a program that demonstrates how a single function template can be
used to perform addition on integers, floats, and doubles.
Ans:- #include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
int x = 5, y = 10;
float a = 5.5, b = 10.5;
double p = 5.55, q = 10.55;
cout << "Addition of " << x << " and " << y << " is " << add(x, y) << endl;
cout << "Addition of " << a << " and " << b << " is " << add(a, b) << endl;
cout << "Addition of " << p << " and " << q << " is " << add(p, q) << endl;
return 0;
}

Q16: WAP to draw the following pattern


*****
****
***
**
*
Ans:- #include <iostream>
using namespace std;
int main() {
for (int i = 5; i > 0; --i) {
for (int j = 0; j < i; ++j) {
cout << "* ";}
cout << endl; }
return 0;
}
Q17: WAP to draw the following pattern
*
**
***
****
*****
Ans:- #include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
for (int j = 0; j < i; ++j) {
cout << "* ";}
cout << endl; }
return 0;
}

Q18: Write a program to check if a given character is vowel or consonants


Ans:- #include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
cout << ch << " is a vowel." << endl;
} else {
cout << ch << " is a consonant." << endl; }
return 0;
}

Q19: Write A Program to Check Whether a Number is Positive or Negative or


Zero.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0) {
cout << num << " is positive." << endl;
} else if (num < 0) {
cout << num << " is negative." << endl;
} else {
cout << num << " is zero." << endl; }
return 0; }

Q20: Check input character is alphabet, digit or special character. (65-90 ,97-122,
(a-z, A-Z)48-57(0-9).
Ans:- #include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
cout << ch << " is an alphabet." << endl;
} else if (ch >= '0' && ch <= '9') {
cout << ch << " is a digit." << endl;
} else {
cout << ch << " is a special character." << endl; }
return 0; }

Q21: Create a Calculator using the switch Statement.


Ans:- #include <iostream>
using namespace std;
int main() {
char op;
float num1, num2;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0)
cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
else
cout << "Division by zero is not allowed." << endl;
break;
default:
cout << "Invalid operator." << endl;
break; }
return 0; }

Q22: Program to Print a Half-Pyramid Using Numbers


1
12
123
1234
12345
Ans:- #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << j << " ";
}
cout << endl; }
return 0; }
Q23: Program to Print a Half-Pyramid Using *
*
**
***
****
*****
Ans:- #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << "* ";}
cout << endl; }
return 0; }

Q24: Print the following Pattern:


A
BB
CCC
DDDD
EEEEE
Ans:- #include <iostream>
using namespace std;
int main() {
char ch = 'A';
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= i; ++j) {
cout << ch << " ";}
cout << endl;
++ch; }
return 0; }
Q25: Print the following Pattern:
*****
****
***
**
*
Ans:- #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
cout << "* ";}
cout << endl; }
return 0; }

Q26: Print the following Pattern:


*********
*******
*****
***
*
Ans:- #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
cout << "* ";}
cout << endl;
for (int k = 0; k < n - i + 1; ++k) {
cout << " ";}}
return 0; }

Q27: Print the following Pattern:


1
23
456
7 8 9 10
Ans:- #include <iostream>
using namespace std;
int main() {
int num = 1;
for (int i = 1; i <= 4; ++i) {
for (int j = 1; j <= i; ++j) {
cout << num << " ";
++num; }
cout << endl; }
return 0; }
Q28: Print the following Pattern:
1
22
333
4444
55555
Ans:- #include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= i; ++j) {
cout << i << " ";}
cout << endl; }
return 0; }

Q29: WAP to Find HCF/GCD


Ans:- #include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b); }
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD of " << num1 << " and " << num2 << " is " << gcd(num1, num2) <<
endl;
return 0; }

Q30: WAP to Find LCM


Ans:- #include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b); }
int lcm(int a, int b) {
return (a * b) / gcd(a, b); }
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "LCM of " << num1 << " and " << num2 << " is " << lcm(num1, num2) <<
endl;
return 0; }

Q31: WAP to check whether a number is palindrome or not


Ans:- #include <iostream>
using namespace std;
int main() {
int num, reversedNum = 0, remainder, originalNum;
cout << "Enter an integer: ";
cin >> num;
originalNum = num;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10; }
if (originalNum == reversedNum)
cout << originalNum << " is a palindrome." << endl;
else
cout << originalNum << " is not a palindrome." << endl;
return 0; }

Q32: WAP to check whether a number is armstrong or not.


Ans:- #include <iostream>
#include <cmath>
using namespace std;
int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;
cout << "Enter an integer: ";
cin >> num;
originalNum = num;
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10; }
for (originalNum = num; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;
result += pow(remainder, n); }
if (round(result) == num)
cout << num << " is an Armstrong number." << endl;
else
cout << num << " is not an Armstrong number." << endl;
return 0; }
Q33: WAP to calculate sum of natural numbers.
Ans:- #include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i; }
cout << "Sum of natural numbers up to " << n << " is " << sum << endl;
return 0; }

Q34: WAP to find the sum of series. 1-2+3-4………..n. Take input n from the user
Ans:- #include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter the value of n: ";
cin >> n;

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


if (i % 2 == 0)
sum -= i;
else
sum += i; }
cout << "Sum of the series is " << sum << endl;
return 0; }

You might also like