KEMBAR78
Oops Assignment Minor 1 Lab | PDF | Namespace | Computer Programming
0% found this document useful (0 votes)
30 views45 pages

Oops Assignment Minor 1 Lab

This document is a submission for the Bachelor of Technology degree in Computer Science & Design by Yogesh Sanodiya, containing a series of programming exercises. Each exercise includes a program description, code, and expected output, covering various topics such as palindrome checking, Armstrong numbers, Fibonacci series, and matrix operations. The document serves as a practical demonstration of programming skills under the supervision of professors from the Department of Computer Science & Engineering.

Uploaded by

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

Oops Assignment Minor 1 Lab

This document is a submission for the Bachelor of Technology degree in Computer Science & Design by Yogesh Sanodiya, containing a series of programming exercises. Each exercise includes a program description, code, and expected output, covering various topics such as palindrome checking, Armstrong numbers, Fibonacci series, and matrix operations. The document serves as a practical demonstration of programming skills under the supervision of professors from the Department of Computer Science & Engineering.

Uploaded by

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

Subject code: 29241207

ooPS
Submitted for the partial fulfilment of the degree of

Bachelor of Technology

In

COMPUTER SCIENCE & DESIGN

Submitted By
Yogesh Sanodiya

BTCD24O1077

UNDER THE SUPERVISION AND GUIDANCE OF

Prof. Vivek Sharma

Assistant Professor

Department of computer science & engineering

Pro. Manisha Pathak

Assistant Professor

Department of computer science & engineering


March 2025

INDEX

Sno. PROGRAMS DATE SIGNATURE


1. Write a program to check whether a number is palindrome or not.

2. Write a program to check the armstrong number.

3. Write a program for sum of Fibonacci numbers.

4. Write a program to print triangle pattern.

5. Write a program to display factors of a natural numbers.

6. Write a program to declare a price of a book with if statement and


check its price. If its price is less than or equal to 600, print the
output with some comment else terminate.

7. Write a program to enter percentage of marks obtained by a student


and display the class obtained.

8. Write a program to enter age and display a message whether the


user is eligible or not.

9. Write a program to print largest of three number.

10. Write a program to demonstrate the function of goto statement.

11. Write a program to create a simple calculator using switch case.

12. Write a program for sum of a positive number using while.


13. Write a program for sum of positive number using do while.

14. Write a program for sum of positive numbers,


 if user enters a negative number, breaks end the loop.
 the negative number is entered is not added to sum using
break.

15. Write a program to find factorial of a no.

16. Write a program for addition multiplication of 2d array.

17. Write a program to find sum of natural no. using recursion.

18 Write a program to find transpose of a matrix.

19. Write a program to find the length of a string.

20. Write a program to ASCII value of character.

21. Write a program to check leap year.

22. Write a program for swapping using third variable.

23. Write a program for swapping without using third variable.

24. Write a program to print star in asterisk stars.

25. Write a program to reverse the string and variable.


26. Write a program using array within a class for processing a
shopping list
27. Write a program for call by value.

28. Write a program for call by reference.

29. Write a program using friend function to add data objects of two
different classes.
30. Write a program to demonstrate constructor.
Q.1) Write a program to check whether a number is palindrome or not.

PROGRAM:
#include <iostream>

using namespace std;

int main()

{ int n, num, digit, rev = 0;

cout << "Enter a positive number: ";

cin >> num;


n = num;

do{

digit = num % 10;

rev = (rev * 10) + digit;

num = num / 10;

} while (num != 0);

cout << " The reverse of the number is: " << rev << endl;

if (n == rev )

cout << " The number is a palindrome.";

else

cout << " The number is not a palindrome.";

return 0;

OUTPUT:
Q.2) Write a program to check the armstrong number.

PROGRAM:
include <iostream>

using namespace std;


int main() {

int num, originalNum, remainder, result = 0;

cout << "Enter a three-digit integer: ";

cin >> num;

originalNum = num;

while (originalNum != 0) {
remainder = originalNum % 10;

result += remainder * remainder * remainder;

originalNum /= 10;

if (result == num)

cout << num << " is an Armstrong number.";

else

cout << num << " is not an Armstrong number.";

return 0;

OUTPUT:
Q.3) Write a program for sum of Fibonacci numbers.
PROGRAM:
#include <iostream>
using namespace std;

int main() {

int t1 = 0, t2 = 1, nextTerm = 0, n;

cout << "Enter a positive number: ";

cin >> n;

cout << "Fibonacci Series: " << t1 << ", " << t2 << ", ";

nextTerm = t1 + t2;

while(nextTerm <= n) {

cout << nextTerm << ", ";

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;
}

return 0;

OUTPUT:
Q.4) Write a program to print triangle pattern.

PROGRAM:
#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;
}

OUTPUT:
Q.5) Write a program to display factors of a natural numbers.

PROGRAM:
#include <iostream>

using namespace std;

int main() {

int n, i;
cout << "Enter a natural integer: ";

cin >> n;

cout << "Factors of " << n << " are: ";

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

if(n % i == 0)

cout << i << " ";


} return 0;
}
OUTPUT:

Q.6) Write a program to declare a price of a book with if statement and check its price. If its
price is less than or equal to 600, print the output with some comment else terminate.

PROGRAM:
#include <iostream>

using namespace std;

int main() {

int price;

cout<<"Enter the price of book";

cin>>price;

if(price<=600)

{ cout<<"The price is less than 600,BRO.";


}

return 0;

OUTPUT:
Q.7) Write a program to enter percentage of marks obtained by a student and display the class
obtained.

PROGRAM:
#include <iostream>

using namespace std;


int main()

float percent;

cout<<"Enter the percentage of marks:";

cin>>percent;

if(percent>100||percent<0)

{cout<<"Invalid percentage"<<endl;}

else if(percent>75)

{cout<<"Class obtained:A"<<endl;}

else if(percent>=60)
{cout<<"Class obtained:B"<<endl;}
else if(percent>=50)

{cout<<"Class obtained:C"<<endl;}

else if(percent>=40)

{cout<<"Class obtained:D"<<endl;}

else

{cout<<"failure, Haa" ;}

return 0;

OUTPUT:
Q.8) Write a program to enter age and display a message whether the user is eligible or not.

PROGRAM:
#include <iostream>

using namespace std;

int main(){

int age;

cout<<"Enter the age";

cin>>age;

if(age>=18)

cout<<"Eligible for vote";

else

cout<<"Not eligible for Vote";


return 0;

OUTPUT:
Q.9) Write a program to print largest of three number.

PROGRAM:
#include <iostream>

using namespace std;

int main() {

double n1, n2, n3;

cout << "Enter three numbers: ";

cin >> n1 >> n2 >> n3;


if(n1 >= n2 && n1 >= n3)

cout << "Largest number: " << n1;

else if(n2 >= n1 && n2 >= n3)

cout << "Largest number: " << n2;

else

cout << "Largest number: " << n3;


return 0;
}

OUTPUT:

Q.10) Write a program to demonstrate the function of goto statement.

PROGRAM:
#include <iostream>

using namespace std;

int main()

cout << "First Statement" << endl;

goto skip_in_between;
cout << "Second Statement" << endl;

skip_in_between:

cout << "Third Statement" << endl;

return 0;

OUTPUT:
Q.11) Write a program to create a simple calculator using switch case.

PROGRAM:
# 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;
break;

case '-':

cout << num1 << " - " << num2 << " = " << num1 - num2;

break;

case '*':

cout << num1 << " * " << num2 << " = " << num1 * num2;

break;

case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;

break;

default:

cout << "Error! operator is not correct";

break;

return 0;}

OUTPUT:
Q.12) Write a program for sum of a positive number using while.

PROGRAM:
#include <iostream>

using namespace std;

int main() {

int num1, num2, sum = 0, i = 0;


cout << "Enter two numbers: ";
cin >> num1 >> num2;

sum = num1 + num2;

while (i < 1) {

cout << "Sum = " << sum << endl;

i++; }

return 0;
}
OUTPUT:

Q.13) Write a program for sum of positive number using do while.

PROGRAM:
#include <iostream>

using namespace std;

int main() {

int number, sum = 0;

do {

cout << "Enter a positive number ";

cin >> number;

if (number > 0) {

sum += number; }

} while (number != 0);

cout << "The sum of the positive numbers is: " << sum << endl;
return 0;
}
OUTPUT:

Q.14) Write a program for sum of positive numbers,


 if user enters a negative number, breaks end the loop.
 the negative number is entered is not added to sum using break.

PROGRAM:
#include <iostream>

using namespace std;

int main() {
int number, sum = 0;

cout << "Enter a positive number: ";

cin >> number;

while (number >= 0) {

sum += number;

cout << "Enter a positive number: ";


cin >> number;
if (number < 0) {

break;

} }

cout << "The sum of the positive numbers is: " << sum << endl;

return 0;}

OUTPUT:

Q.15) Write a program to find factorial of a no.

PROGRAM:
#include <iostream>

using namespace std;


int main() {

int num;

double factorial = 1;

cout << "Enter a number: ";

cin >> num;

if (num < 0) {

cout << "Factorial is not defined for negative numbers."<<endl;

} else {

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


factorial *= i;
} }

cout << "Factorial of " << num << " is " << factorial << endl;

return 0;

OUTPUT:

Q.16) Write a program for addition multiplication of 2d array.

PROGRAM:
#include <iostream>

using namespace std;

int main() {

int row, col;

cout << "Enter number of rows: ";

cin >> row;

cout << "Enter number of columns: ";

cin >> col;

int A[row][col], B[row][col], sum[row][col], product[row][col];

cout << "Enter elements of first matrix (A):\n";


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

for (int j = 0; j < col; j++) {


cin >> A[i][j];

}}

cout << "Enter elements of second matrix (B):\n";

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

for (int j = 0; j < col; j++) {

cin >> B[i][j];

}}

cout << "\nSum of the two matrices:\n";


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

for (int j = 0; j < col; j++) {

sum[i][j] = A[i][j] + B[i][j];

cout << sum[i][j] << " ";

cout << endl;

cout << "\nProduct of the two matrices:\n";

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

for (int j = 0; j < col; j++) {

product[i][j] = 0;

for (int k = 0; k < row; k++) {

product[i][j] += A[i][k] * B[k][j]; }


cout << product[i][j] << " ";}

cout << endl;


}

return 0;

OUTPUT:
17.) Write a program to find sum of natural no. using recursion.
PROGRAM:

#include <iostream>
using namespace std;

int main() {

int number, sum = 0;

cout << "Enter a natural number: ";

cin >> number;

if (number == 0) {

sum = 0;
} else {
sum = number + (number - 1);

cout << "The sum of first " << number << " natural numbers is:” << sum << endl;

return 0;

OUTPUT:

Q.18.) Write a program to find transpose of a matrix.

PROGRAM:
#include <iostream>

using namespace std;

int main() {

int a[10][10], transpose[10][10], row, column, i, j;

cout << "Enter rows and columns of matrix: ";

cin >> row >> column;


cout << "\nEnter elements of matrix: " << endl;

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

for (int j = 0; j < column; ++j) {


cout << "Enter element a" << i + 1 << j + 1 << ": ";

cin >> a[i][j];


}}

cout << "\nEntered Matrix: " << endl;

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

for (int j = 0; j < column; ++j) {

cout << " " << a[i][j];

if (j == column - 1)

cout << endl << endl;

}
}

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

for (int j = 0; j < column; ++j) {

transpose[j][i] = a[i][j];

cout << "\nTranspose of Matrix: " << endl;

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

for (int j = 0; j < row; ++j) {

cout << " " << transpose[i][j];

if (j == row - 1)

cout << endl << endl;

return 0;
}

OUTPUT:
Q.19) Write a program to find the length of a string.

PROGRAM:
#include <iostream>

#include <cstring>

using namespace std;

int main() {

string str;

cout << "Enter a string: ";


getline(cin, str);
int count = 0;

for (char ch : str) {

count++;

cout << "Length of the string : " << count <<endl;

return 0;

OUTPUT:

Q.20) Write a program to ASCII value of character.

PROGRAM:
#include <iostream>

using namespace std;

int main() {

char ch;

cout << "Enter a character: ";

cin >> ch;


cout << "ASCII value of '" << ch << "' is: " << int(ch) << endl;
return 0;

OUTPUT:

Q.21) Write a program to check leap year.

PROGRAM
#include <iostream>

using namespace std;

int main() {

int year;

cout << "Enter a year: ";


cin >> year;
if (year % 4 == 0) {
if (year % 100 == 0) {

if (year % 400 == 0) {

cout << year << " is a leap year.";

else {

cout << year << " is not a leap year.";

}}

else {
cout << year << " is a leap year.";

}}

else {

cout << year << " is not a leap year.";

return 0;

OUTPUT:
Q.22) Write a program for swapping using third variable.

PROGRAM
#include <iostream>

using namespace std;

int main()

{ int a, b, temp;

cout<<"Enter the two no.s for swapping:";

cin>>a;

cin>>b;
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;

OUTPUT:

Q.23) Write a program for swapping without using third variable.

PROGRAM
#include <iostream>

using namespace std;


int main()

{ int a = 5, b = 10;

cout << "Before swapping." << endl;

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;

OUTPUT:

Q.24) Write a program to print star in asterisk stars.

PROGRAM
#include <iostream>

using namespace std;

int main() {

int n, space;

cout << "Enter the number of rows: ";

cin >> n;

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

cout << " ";

space--;

for (int j = 1; j <= 2*i-1; j++) {

cout << "*";

cout << endl;


}

space = 1;

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

for (int j = 1; j <= space; j++) {

cout << " ";

space++;

for (int j = 1; j <= 2*(n-i)-1; j++) {

cout << "*";

cout << endl; }

return 0;

OUTPUT:
Q.25) Write a program to reverse the string and variable.

PROGRAM
#include <iostream>
using namespace std;

int main() {

char str[100], reversedStr[100];

int len = 0, endIndex;

cout << "Enter a string: ";

cin >> str;

while (str[len] != '\0') {

len++; }
endIndex = len - 1;

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

reversedStr[i] = str[endIndex];

endIndex--; }

reversedStr[len] = '\0';

cout << "Reversed string: " << reversedStr << endl;

return 0;

OUTPUT:

Q.26) Write a program using array within a class for processing a shopping list.

PROGRAM:
#include <iostream>
#include <string>

using namespace std;

class ShoppingList {

private:

static const int MAX_ITEMS = 10;

string items[MAX_ITEMS]; int itemCount;

public:
ShoppingList() { itemCount = 0; }

void addItem(const string& item) {

if (itemCount < MAX_ITEMS) {

items[itemCount++] = item;

cout << "Item added: " << item << "\n";

} else {

cout << "Shopping list is full!\n";

} }

void removeItem(const string& item) {

bool found = false;

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

if (items[i] == item) {
found = true;

for (int j = i; j < itemCount - 1; j++) {


items[j] = items[j + 1];

itemCount--;

cout << "Item removed: " << item << "\n";

break;
} }
if (!found) {

cout << "Item not found in the list!\n";

}}

void displayList() const {

if (itemCount == 0) {

cout << "Shopping list is empty!\n";

return;

}
cout << "Shopping List:\n";

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

cout << i + 1 << ". " << items[i] << "\n";

} };

int main() {

ShoppingList myList;

int choice;

string item;

do {

cout << "\nShopping List Menu:\n";

cout << "1. Add Item\n2. Remove Item\n3. Display List\n4. Exit\n";

cout << "Enter your choice: ";


cin >> choice;

cin.ignore(); // Clear input buffer

switch (choice) {

case 1:

cout << "Enter item to add: ";

getline(cin, item);
myList.addItem(item);
break;

case 2:

cout << "Enter item to remove: ";

getline(cin, item);

myList.removeItem(item);

break;

case 3:

myList.displayList();
break;

case 4:

cout << "Exiting program.\n";

break;

default:

cout << "Invalid choice. Please try again.\n";

} while (choice != 4);

return 0;

OUTPUT:
Q.27) Write a program for call by value.
PROGRAM:
#include <iostream>

using namespace std;

void square(int num) {

num = num * num;

cout << "Inside function: num = " << num << endl;

int main() {
int number = 5;

cout << "Before function call: number = " << number << endl;

square(number);

cout << "After function call: number = " << number << endl;

return 0;

OUTPUT:

Q.28.) Write a program for call by reference.


PROGRAM:
#include <iostream>

using namespace std;

void swap(int &x, int &y) {

int temp = x;

x = y;

y = temp;

}
int main() {

int a , b;

cout<<"Enter the numbers to swap";

cin>>a>>b;

cout << "Before swap: a = " << a << ", b = " << b << endl;

swap(a, b);

cout << "After swap: a = " << a << ", b = " << b << endl;

return 0;

OUTPUT:

Q.29) Write a program using friend function to add data objects of two different classes.
PROGRAM:
#include <iostream>

using namespace std;

class Two;

class One {

private:

int value;

public:
One(int v) {

value = v;

friend int addValues(const One& o, const Two& t);

};

class Two {

private:

int value;

public:

Two(int v) {

value = v;

friend int addValues(const One& o, const Two& t);


};

int addValues(const One& o, const Two& t) {


return o.value + t.value;

int main() {int value1, value2;

cout << "Enter value for object of class One: ";

cin >> value1;


cout << "Enter value for object of class Two: ";
cin >> value2;

One obj1(value1);

Two obj2(value2);

cout << "Sum of values: " << addValues(obj1, obj2) << endl;

return 0;

OUTPUT

Q.30) Write a program to demonstrate constructor.


PROGRAM:
#include <iostream>

using namespace std;

class cons {

private:

int value;

public:

cons() {
value = 0;

cout << "Default constructor called. Value is set to 0." << endl;

cons(int v) {

value = v;

cout << "Parameterized constructor called. Value is set to " << v << "." << endl;

void display() {

cout << "Value: " << value << endl;

}};

int main() {

int g;

cout<<"Enter the number";


cin>>g;

cons obj1;
cons obj2(g);

obj1.display();

obj2.display();

return 0;

OUTPUT

You might also like