SOCIAL MEDIA USER MANAGEMENT PROGRAM
#include <iostream>
#include <limits.h>
using namespace std;
#define MAX 100
// Class to represent a user profile
class User {
public:
string nick_name;
string user_name;
string password;
string bio;
string image_uri;
int followers;
int following;
int posts;
};
// Function prototypes
int getSize(User* users);
bool isFull(User* users);
void inputUser(User* users);
int getIndex(User* users, string user_name);
void deleteUser(User* users, string user_name);
void getUser(User* users, string user_name);
void getAllUser(User* users);
void space(int n);
void getUserProfile(User* users, string user);
void profileOptions(User* users, string user);
void modifyUserProfile(User* users, string user);
string getUserName(User* users, int index);
int main() {
// Initialization
int main_choice = INT_MIN;
int rep_main = 1;
User users[MAX] = {
{"John Doe", "johndoe123", "password123", "I love programming!", "image1.jpg", 100, 50, 25},
{"Alice Smith", "alicesmith", "pass123", "Photography enthusiast", "image2.jpg", 150, 75, 30},
{"Bob Johnson", "bobj", "securepass", "Nature lover and adventurer", "image3.jpg", 80, 40, 15},
{"Eva Williams", "evaw", "mypassword", "Foodie and chef", "image4.jpg", 120, 60, 20},
{"Sam Miller", "samm", "p@ssw0rd", "Fitness freak", "image5.jpg", 200, 100, 40}
};
cout << "================USER MANAGEMENT SOFTWARE=================" << endl;
do {
input_jump:
cout << "1. Insert Profile\n2. Search Profile\n3. Get All Profiles\n4. Modify Profile\n5. Delete
Profile\n6. Exit The Program" << endl;
space(0);
cout << "Input your choice : ";
cin >> main_choice;
cin.ignore();
switch(main_choice) {
case 1:
inputUser(users);
goto input_jump;
break;
case 2:
string user_search;
cout << "Input the name : ";
getline(cin, user_search);
getUser(users, user_search);
space(0);
profileOptions(users, user_search);
break;
case 3:
getAllUser(users);
space(2);
int ch;
cout << "Open Profile : ";
cin >> ch;
cin.ignore();
string u = getUserName(users, ch - 1);
getUserProfile(users, u);
profileOptions(users, u);
break;
case 4:
string user_mod;
cout << "Enter the User Name : ";
cin >> user_mod;
modifyUserProfile(users, user_mod);
break;
case 5:
string user_del;
cout << "Enter the User Name : ";
cin >> user_del;
deleteUser(users, user_del);
break;
case 6:
// Exit the program
break;
default:
cout << "INVALID INPUT!" << endl;
break;
cout << "Press 1 to Continue: ";
cin >> rep_main;
} while(rep_main == 1);
return 0;
// Function to get the user name based on index
string getUserName(User* users, int index) {
return users[index].user_name;
// Function to display profile options (Open Profile, Modify Profile, Delete Profile)
void profileOptions(User* users, string user) {
int n;
cout << "1. Open Profile\n2. Modify Profile\n3. Delete Profile\n\nInput your choice : ";
cin >> n;
switch(n) {
case 1:
getUserProfile(users, user);
break;
case 2:
modifyUserProfile(users, user);
break;
case 3:
deleteUser(users, user);
break;
default:
cout << "INVALID CHOICE!" << endl;
// Function to modify user profile
void modifyUserProfile(User* users, string user) {
int c;
int index = getIndex(users, user);
User* u = &(users[index]);
cout << "Modifying User : " << u->user_name << endl << endl;
cout << "1. Name\n2. User Name\n3. Password\n4. Bio\n5. Profile Image\nInput your choice : ";
cin >> c;
cin.ignore( );
switch(c) {
case 1:
cout << "Enter new name : ";
getline(cin, u->nick_name);
break;
case 2:
cout << "Enter new User Name : ";
getline(cin, u->user_name);
break;
case 3:
cout << "Enter new Password : ";
getline(cin, u->password);
break;
case 4:
cout << "Enter new Bio : ";
getline(cin, u->bio);
break;
case 5:
cout << "Enter new Profile Image Uri : ";
getline(cin, u->image_uri);
break;
default:
cout << "INVALID CHOICE!" << endl;
// Function to display user profile
void getUserProfile(User* users, string user) {
int index = getIndex(users, user);
User u = users[index];
space(3);
cout << "Name : " << u.nick_name << endl;
cout << "User Name : " << u.user_name << endl;
cout << "Passworld : " << u.password << endl;
cout << "Bio : " << u.bio << endl;
cout << "Profile Image : " << u.image_uri << endl;
cout << "Following : " << u.following << endl;
cout << "Followers : " << u.followers << endl;
cout << "Posts : " << u.posts << endl;
space(3);
// Function to get the size of the array of users
int getSize(User* users) {
int size = 0;
int i = 0;
while(!users[i].user_name.empty()) {
size++;
i++;
return size;
// Function to check if the array of users is full
bool isFull(User* users) {
if(getSize(users) < MAX) {
return false;
else {
return true;
// Function to input user profile(s)
void inputUser(User* users) {
int user_input_count;
cout << "Input number of profiles to add : ";
cin >> user_input_count;
cin.ignore(); // Ignore the newline character after reading user_input_count
for(int i = 0; i < user_input_count; i++) {
if(isFull(users)) {
cout << "ARRAY OVERFLOW" << endl;
else {
User u;
cout << "Input your name : ";
getline(cin, u.nick_name);
cout << "Input your username : ";
getline(cin, u.user_name);
cout << "Input your password : ";
getline(cin, u.password);
cout << "Input your bio : ";
getline(cin, u.bio);
cout << "Input profile image uri : ";
getline(cin, u.image_uri);
cout << "Input followers count : ";
cin >> u.followers;
cout << "Input following count : ";
cin >> u.following;
cout << "Input posts count : ";
cin >> u.posts;
users[getSize(users)] = u;
cout << endl;
// Function to get the index of a user by username
int getIndex(User* users, string user_name) {
int i = 0;
while(!users[i].user_name.empty()) {
if(users[i].user_name == user_name) {
return i;
i++;
cout << "ELEMENT NOT FOUND" << endl;
return INT_MIN;
// Function to delete a user profile
void deleteUser(User* users, string user_name) {
int index = getIndex(users, user_name);
int i = index;
User empty = {"", "", "", "", "", 0, 0, 0};
while(i < MAX - 1 && !users[i].user_name.empty( )) {
users[i] = users[i + 1];
i++;
users[i] = empty;
// Function to display a user profile by username
void getUser(User* users, string user_name) {
int index = getIndex(users, user_name);
User u = users[index];
cout << "( " << index + 1 << " )" << "__" << u.user_name << " _________________" << endl;
cout << " Name : " << u.nick_name << endl;
cout << "_________________________________" << endl << endl;
// Function to display all user profiles
void getAllUser(User* users) {
for(int i = 0; i < getSize(users); i++) {
getUser(users, users[i].user_name);
// Function to add space for formatting
void space(int n) {
if(n == 2) {
cout << endl << endl;
else if(n == 3) {
cout << endl << endl << endl;
else if(n == 4) {
cout << endl << endl << endl << endl;
else {
cout << endl;
LOGIN PAGE WITH ANIMATION
#include <iostream>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h> // Changed to <graphics.h> for Turbo C++
#include <string.h>
#define design "#" // Define symbol for design
int main() {
char username[50];
char password[50];
char rusername[] = "kartik";
char rpassword[] = "kartik006";
int i;
int c, cho, passlen = 0;
char arr[] =
"##########################################################################";
int gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "c:/turboc3/bgi"); // Initialize graphics mode
lineto(0, 500);
moveto(625, 0);
lineto(625, 500);
gotoxy(4, 25);
cout << arr;
gotoxy(4, 5);
cout << arr;
fail:
gotoxy(35, 13);
cout << "1. LOG IN";
gotoxy(35, 14);
cout << "2. SIGN UP";
gotoxy(35, 15);
cout << "3. CANCEL";
cho = getch();
cout << cho;
gotoxy(35, 13);
cout << " ";
gotoxy(35, 14);
cout << " ";
gotoxy(35, 15);
cout << " ";
switch (cho) {
case 49: // ASCII value for '1'
gotoxy(31, 13);
cout << "Username : ";
gotoxy(31, 15);
cout << "Password : ";
gotoxy(42, 13);
cin >> username;
gotoxy(42, 15);
for (i = 0; i < 15; i++) {
c = getch( );
if (c == 13) {
password[i] = '\0';
break;
password[i] = c;
cout << password[i];
delay(200);
cout << "\b \b";
cout << "*";
gotoxy(31, 13);
cout << " ";
gotoxy(31, 15);
cout << " ";
gotoxy(30, 14);
cout << "Checking Credentials.....";
for (int j = 0; j < 1; j++) {
gotoxy(78, 5);
for (i = 0; i < 35; i++) {
cout << "\b";
delay(15);
cout << "\b \b";
cout << "\b \b";
delay(50);
cout << design << design << "\b";
delay(20);
gotoxy(4, 25);
for (i = 0; i < 70; i++) {
cout << design;
delay(15);
cout << "\b \b";
cout << "\b \b";
delay(30);
cout << design << design;
if (strcmp(username, rusername) == 0 && strcmp(password, rpassword) == 0) {
gotoxy(30, 14);
cout << " ";
gotoxy(30, 14);
cout << "LOGIN SUCCESSFUL";
gotoxy(32, 15);
cout << "Welcome " << username << " !";
} else {
gotoxy(30, 14);
cout << " ";
gotoxy(30, 14);
cout << "LOGIN FAILED";
gotoxy(27, 15);
cout << "Wrong Username or password!";
delay(2000);
gotoxy(30, 14);
cout << " ";
gotoxy(27, 15);
cout << " ";
goto fail;
break;
case 50: // ASCII value for '2'
gotoxy(31, 13);
cout << "Username : ";
gotoxy(31, 15);
cout << "New Password : ";
gotoxy(42, 13);
cin >> rusername;
gotoxy(46, 15);
cin >> rpassword;
gotoxy(31, 13);
cout << " ";
gotoxy(31, 15);
cout << " ";
gotoxy(30, 14);
cout << "ACCOUNT CREATED";
gotoxy(27, 15);
cout << "Returning to main menu...";
delay(2000);
gotoxy(30, 14);
cout << " ";
gotoxy(27, 15);
cout << " ";
goto fail;
case 51: // ASCII value
for '3'
exit(0);
break;
getch( ); // Wait for a key
press
return 0;
SHAPE INHERITANCE PROGRAM
#include <iostream>
#include <conio.h> // For using _getch( )
using namespace std;
// Abstract base class representing a shape
class Shape {
public:
// Pure virtual functions to calculate area and perimeter
virtual double area( ) = 0;
virtual double perimeter( ) = 0;
};
// Derived class representing a square
class Square : Shape {
private:
double side;
public:
// Constructor to initialize the side of the square
Square(double s) : side(s) { }
// Function to calculate the area of the square
double area( ) override {
return side * side;
// Function to calculate the perimeter of the square
double perimeter( ) override {
return 4 * side;
};
// Derived class representing a rectangle
class Rectangle : Shape {
private:
double length;
double breadth;
public:
// Constructor to initialize the length and breadth of the rectangle
Rectangle(double l, double b) : length(l), breadth(b) { }
// Function to calculate the area of the rectangle
double area( ) override {
return length * breadth;
// Function to calculate the perimeter of the rectangle
double perimeter( ) override {
return 2 * (length + breadth);
};
// Derived class representing a circle
class Circle : Shape {
private:
double radius;
public:
// Constructor to initialize the radius of the circle
Circle(double r) : radius(r) { }
// Function to calculate the area of the circle
double area( ) override {
return (22.0 / 7) * (radius * radius);
// Function to calculate the circumference of the circle
double perimeter( ) override {
return 2 * (22.0 / 7) * radius;
};
int main( ) {
// Create instances of different shapes
Rectangle rectangle(4, 6);
Circle circle(5);
Square square(4);
// Print area and perimeter of each shape
cout << "Rectangle Area: " << rectangle.area( ) << ", Perimeter: " << rectangle.perimeter( ) << endl;
cout << "Circle Area: " << circle.area( ) << ", Perimeter: " << circle.perimeter( ) << endl;
cout << "Square Area: " << square.area( ) << ", Perimeter: " << square.perimeter( ) << endl;
// Pause the program until a key is pressed
_getch( );
return 0;
BANK PROGRAM
#include <iostream>
#include <cstring>
#include <limits> // for clearing input buffer
using namespace std;
// Base class representing an account
class Account {
protected:
char name[30];
int age;
long long int acc_no; // Changed to long long int for larger range
int interest;
int balance;
public:
void setName(const char* n) {
strcpy(name, n);
void setAge(int a) {
age = a;
void setAccountNum(long long int an) {
acc_no = an;
void setInterest(int i) {
interest = i;
void setBalance(int b) {
balance = b;
const char* getName( ) {
return name;
int getAge( ) {
return age;
long long int getAccountNum( ) { // Changed return type to long long int
return acc_no;
int getInterest( ) {
return interest;
int getBalance( ) {
return balance;
};
// Derived class representing a savings account
class SavingsAccount : public Account {
public:
// Function to create a new user with a savings account
void createUser( ) {
cout << "Input the name: ";
cin.ignore( ); // Clear input buffer
cin.getline(name, 30);
cout << "Input the age: ";
cin >> age;
cout << "Input the account number: ";
cin >> acc_no;
cout << "Input the balance: ";
cin >> balance;
interest = 2; // 2 percent interest for savings account
// Function to display user information for savings account
void getUserInfo( ) {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Account Number: " << acc_no << endl;
// Function to get the balance of the savings account
int getUserBalance( ) {
return balance;
};
// Derived class representing a current account
class CurrentAccount : public Account {
public:
// Function to create a new user with a current account
void createUser( ) {
cout << "Input the name: ";
cin.ignore( ); // Clear input buffer
cin.getline(name, 30);
cout << "Input the age: ";
cin >> age;
cout << "Input the account number: ";
cin >> acc_no;
cout << "Input the balance: ";
cin >> balance;
interest = 0; // No interest for current account
// Function to display user information for current account
void getUserInfo( ) {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Account Number: " << acc_no << endl;
// Function to get the balance of the current account
int getUserBalance( ) {
return balance;
};
// Class representing a user with both savings and current accounts
class User {
private:
SavingsAccount savingsAccount;
CurrentAccount currentAccount;
bool hasSavings;
bool hasCurrent;
public:
User( ) {
hasSavings = false;
hasCurrent = false;
// Function to create accounts for the user
void createAccounts( ) {
cout << "\n1. Create Savings Account\n2. Create Current Account\n3. Create Both\nEnter your
choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
savingsAccount.createUser( );
hasSavings = true;
break;
case 2:
currentAccount.createUser( );
hasCurrent = true;
break;
case 3:
savingsAccount.createUser( );
currentAccount.createUser( );
hasSavings = true;
hasCurrent = true;
break;
default:
cout << "Invalid choice!" << endl;
cin.clear( ); // Clear error flags
cin.ignore(numeric_limits<streamsize>::max( ), '\n'); // Clear input buffer
int getBalance( ) {
return balance;
};
// Derived class representing a savings account
class SavingsAccount : public Account {
public:
// Function to create a new user with a savings account
void createUser( ) {
cout << "Input the name: ";
cin.ignore( ); // Clear input buffer
cin.getline(name, 30);
cout << "Input the age: ";
cin >> age;
cout << "Input the account number: ";
cin >> acc_no;
// Function to display user information for both accounts
void getUserInfo( ) {
if (hasSavings) {
cout << "Savings Account Info:" << endl;
savingsAccount.getUserInfo( );
if (hasCurrent) {
cout << "Current Account Info:" << endl;
currentAccount.getUserInfo( );
// Function to get the balance of the savings account
int getSavingsBalance( ) {
return (hasSavings ? savingsAccount.getUserBalance( ) : 0);
// Function to get the balance of the current account
int getCurrentBalance( ) {
return (hasCurrent ? currentAccount.getUserBalance( ) : 0);
// Function to search for a user by account number
bool searchByAccountNum(long long int accountNum) { // Changed parameter type to long long int
return (hasSavings && savingsAccount.getAccountNum( ) == accountNum) ||
(hasCurrent && currentAccount.getAccountNum( ) == accountNum);
};
int main( ) {
User users[10];
int count = 0;
int choice;
while (true) {
cout << "\n1. Create New User\n2. View User Info\n3. View Savings Account Balance\n4. View
Current Account Balance\n5. Search by Account Number\n6. Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
if (count < 10) {
users[count].createAccounts( );
count++;
else {
cout << "Maximum users reached!" << endl;
break;
case 2: {
int index;
cout << "Enter the user index: ";
cin >> index;
if (index >= 0 && index < count) {
users[index].getUserInfo( );
else {
cout << "Invalid index!" << endl;
break;
case 3: {
int index;
cout << "Enter the user index: ";
cin >> index;
if (index >= 0 && index < count) {
cout << "Savings Account Balance: " << users[index].getSavingsBalance( ) << endl;
else {
cout << "Invalid index!" << endl;
break;
case 4: {
int index;
cout << "Enter the user index: ";
cin >> index;
if (index >= 0 && index < count) {
cout << "Current Account Balance: " << users[index].getCurrentBalance( ) << endl;
else {
cout << "Invalid index!" << endl;
break;
case 5: {
long long int accountNum;
cout << "Enter account number: ";
cin >> accountNum;
bool found = false;
for (int i = 0; i < count; ++i) {
if (users[i].searchByAccountNum(accountNum)) {
cout << "Name : " << endl;
users[i].getUserInfo();
found = true;
break;
if (!found)
cout << "User not found!" << endl;
break;
case 6:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice!" << endl;
return 0;
cin >> balance;
interest = 2; // 2 percent interest for savings account
// Function to display user information for savings account
void getUserInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Account Number: " << acc_no << endl;
// Function to get the balance of the savings account
int getUserBalance() {
return balance;
};
// Derived class representing a current account
class CurrentAccount : public Account {
public:
// Function to create a new user with a current account
void createUser( ) {
cout << "Input the name: ";
cin.ignore( ); // Clear input buffer
cin.getline(name, 30);
cout << "Input the age: ";
cin >> age;
cout << "Input the account number: ";
cin >> acc_no;
cout << "Input the balance: ";
cin >> balance;
interest = 0; // No interest for current account
// Function to display user information for current account
void getUserInfo( ) {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Account Number: " << acc_no << endl;
// Function to get the balance of the current account
int getUserBalance( ) {
return balance;
};
// Class representing a user with both savings and current accounts
class User {
private:
SavingsAccount savingsAccount;
CurrentAccount currentAccount;
bool hasSavings;
bool hasCurrent;
public:
User( ) {
hasSavings = false;
hasCurrent = false;
// Function to create accounts for the user
void createAccounts( ) {
cout << "\n1. Create Savings Account\n2. Create Current Account\n3. Create Both\nEnter your
choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
savingsAccount.createUser( );
hasSavings = true;
break;
case 2:
currentAccount.createUser( );
hasCurrent = true;
break;
case 3:
savingsAccount.createUser();
currentAccount.createUser();
hasSavings = true;
hasCurrent = true;
break;
default:
cout << "Invalid choice!" << endl;
cin.clear(); // Clear error flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear input buffer
// Function to display user information for both accounts
void getUserInfo() {
if (hasSavings) {
cout << "Savings Account Info:" << endl;
savingsAccount.getUserInfo();
if (hasCurrent) {
cout << "Current Account Info:" << endl;
currentAccount.getUserInfo();
// Function to get the balance of the savings account
int getSavingsBalance() {
return (hasSavings ? savingsAccount.getUserBalance() : 0);
// Function to get the balance of the current account
int getCurrentBalance() {
return (hasCurrent ? currentAccount.getUserBalance() : 0);
// Function to search for a user by account number
bool searchByAccountNum(long long int accountNum) { // Changed parameter type to long long int
return (hasSavings && savingsAccount.getAccountNum() == accountNum) ||
(hasCurrent && currentAccount.getAccountNum() == accountNum);
};
int main() {
User users[10];
int count = 0;
int choice;
while (true) {
cout << "\n1. Create New User\n2. View User Info\n3. View Savings Account Balance\n4. View
Current Account Balance\n5. Search by Account Number\n6. Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
if (count < 10) {
users[count].createAccounts();
count++;
else {
cout << "Maximum users reached!" << endl;
break;
case 2: {
int index;
cout << "Enter the user index: ";
cin >> index;
if (index >= 0 && index < count) {
users[index].getUserInfo();
else {
cout << "Invalid index!" << endl;
break;
case 3: {
int index;
cout << "Enter the user index: ";
cin >> index;
if (index >= 0 && index < count) {
cout << "Savings Account Balance: " << users[index].getSavingsBalance() << endl;
else {
cout << "Invalid index!" << endl;
break;
case 4: {
int index;
cout << "Enter the user index: ";
cin >> index;
if (index >= 0 && index < count) {
cout << "Current Account Balance: " << users[index].getCurrentBalance() << endl;
else {
cout << "Invalid index!" << endl;
break;
case 5: {
long long int accountNum;
cout << "Enter account number: ";
cin >> accountNum;
bool found = false;
for (int i = 0; i < count; ++i) {
if (users[i].searchByAccountNum(accountNum)) {
cout << "Name : " << endl;
users[i].getUserInfo( );
found = true;
break;
if (!found)
cout << "User not found!" << endl;
break;
case 6:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice!" << endl;
return 0;
PATTERN PROGRAM
#include <iostream>
using namespace std;
// Class representing various binary patterns
class binaryPattern {
public:
// Function to print a pattern of all 1s
void pattern1(int n) {
for(int i = 0 ;i < n;i++) {
for(int j = 0;j < n;j++) {
cout << 1 ;
cout << endl;
cout << endl;
// Function to print a pattern alternating between 1s and 0s in rows
void pattern2(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
(i % 2 == 0) ? cout << 1 : cout << 0;
cout << endl;
cout << endl;
// Function to print a pattern alternating between 1s and 0s in columns
void pattern3(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
(j % 2 == 0) ? cout << 0 : cout << 1;
cout << endl;
cout << endl;
// Function to print a pattern with 1s at the corners and 0s elsewhere
void pattern4(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
((i % 2 == 0 && (j == 0 || j == n - 1)) || (i % 2 != 0) && (j != 0 && j != n - 1)) ? cout << 0 : cout << 1;
cout << endl;
cout << endl;
// Function to print a pattern with 1s at the borders and 0s in the middle
void pattern5(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
((i == 0 || i == n - 1) || (j == 0 || j == n - 1)) ? cout << 1 : cout << 0;
cout << endl;
cout << endl;
// Function to print a pattern with 1 at the center and 0s elsewhere
void pattern6(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
(n / 2 == i && n / 2 == j) ? cout << 0 : cout << 1;
cout << endl;
cout << endl;
};
// Class representing various numeric patterns
class numericPattern {
public:
// Function to print a pattern of consecutive numbers increasing in rows
void pattern1(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
cout << i + 1;
cout << endl;
cout << endl;
// Function to print a pattern of consecutive numbers increasing in columns
void pattern2(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
cout << j + 1;
cout << endl;
cout << endl;
// Function to print a pattern of consecutive numbers increasing diagonally
void pattern3(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
cout << j + 1 + i;
}
cout << endl;
cout << endl;
// Function to print a pattern of consecutive numbers increasing diagonally
void pattern4(int n) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
cout << j + 1 + i;
cout << endl;
cout << endl;
};
int main() {
// Creating objects of the binaryPattern and numericPattern classes
binaryPattern bp;
numericPattern np;
// Printing various patterns using the binaryPattern object
bp.pattern1(5);
bp.pattern2(5);
bp.pattern3(5);
bp.pattern4(5);
bp.pattern5(5);
bp.pattern6(5);
np.pattern1(5);
np.pattern2(5);
np.pattern3(5);
SNAKE & LADDER
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <conio.h>
using namespace std;
// Function to move the cursor to a specific position on the console window
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
// Structure to represent the start and end points of snakes and ladders
struct Way {
int start;
int end;
};
// Class for the Snake and Ladder game
class SnakeLadder {
private:
int playerOne; // Position of player one
int playerTwo; // Position of player two
int chance; // Current chance (player)
Way snakes[7]; // Array to store the positions of snakes
Way ladders[8]; // Array to store the positions of ladders
public:
// Constructor to initialize the game
SnakeLadder() {
playerOne = 0;
playerTwo = 0;
chance = 0;
// Initialize the positions of snakes
snakes[0] = {32, 10};
snakes[1] = {36, 6};
snakes[2] = {48, 26};
snakes[3] = {62, 18};
snakes[4] = {88, 24};
snakes[5] = {95, 56};
snakes[6] = {97, 78};
// Initialize the positions of ladders
ladders[0] = {1, 38};
ladders[1] = {4, 14};
ladders[2] = {8, 10};
ladders[3] = {21, 42};
ladders[4] = {28, 76};
ladders[5] = {50, 67};
ladders[6] = {71, 92};
ladders[7] = {80, 99};
// Function to print the game board
void printBoard() {
system("cls");
cout << "**********SNAKES AND LADDERS*********" << endl << endl << endl;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int num = (i % 2 == 0) ? (100 - (i * 10)) - j : (100 - ((i + 1) * 10)) + j + 1;
if (playerOne == num) {
cout << "#P1" << '\t'; // Print player one's position
} else if (playerTwo == num) {
cout << "#P2" << '\t'; // Print player two's position
} else {
cout << num << "\t"; // Print the board position
cout << endl << endl;
// Function to roll the dice
int rollDice() {
int lb = 1;
int ub = 6;
srand(time(0));
int roll = (rand() % (ub - lb + 1)) + lb;
return roll;
// Function to move the player based on the dice roll
void movePlayer(int &player, int roll) {
player += roll; // Move the player
// Check for snakes and ladders
for (int i = 0; i < 7; i++) {
if (snakes[i].start == player) {
player = snakes[i].end; // If a snake is encountered, move the player down
cout << "Oops! A snake was encountered." << endl;
break;
for (int i = 0; i < 8; i++) {
if (ladders[i].start == player) {
player = ladders[i].end; // If a ladder is encountered, move the player up
cout << "Yay! A ladder was encountered." << endl;
break;
// Function to start the game
void startGame() {
while (true) {
printBoard(); // Print the game board
// Check if any player has reached the end position
if (playerOne >= 100) {
cout << "Player One Won!!" << endl;
break;
if (playerTwo >= 100) {
cout << "Player Two Won!!" << endl;
break;
cout << chance << endl;
if (chance % 2 == 0) { // Player one's turn
cout << "P1 TURN" << endl;
cout << "Press 1 to Roll" << endl;
getch(); // Wait for user input
int roll = rollDice(); // Roll the dice
cout << "Dice Rolls to " << roll << endl;
movePlayer(playerOne, roll); // Move player one
} else { // Player two's turn
cout << "P2 TURN" << endl;
cout << "Press 2 to Roll" << endl;
getch(); // Wait for user input
int roll = rollDice(); // Roll the dice
cout << "Dice Rolls to " << roll << endl;
movePlayer(playerTwo, roll); // Move player two
chance = chance + 1; // Increment the chance
}}};
int main() {
SnakeLadder sl; // Create an object of the SnakeLadder class
sl.startGame(); // Start the game
return 0;
SINGLE TO DOUBLE DIMENSION ARRAY
#include <iostream>
#include <cmath>
using namespace std;
class Array2D {
private:
int** data; // Pointer to 2D array
int rows; // Number of rows
int cols; // Number of columns
public:
// Constructor to initialize the 2D array with given 1D array
Array2D(int* arr, int size) {
// Calculate the number of rows and columns
int sqSize = sqrt(size);
rows = sqSize;
cols = (size + sqSize - 1) / sqSize;
// Dynamically allocate memory for the 2D array
data = new int*[rows];
int c = 0; // Counter for iterating through the 1D array
for (int i = 0; i < rows; i++) {
data[i] = new int[cols];
for (int j = 0; j < cols; j++) {
// Fill the 2D array with elements from the 1D array
if (size != c) {
data[i][j] = arr[c];
c++;
} else {
// If the 1D array is smaller than the 2D array, fill remaining elements with 0
data[i][j] = 0;
// Function to print the 2D array
void print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << data[i][j] << "\t"; // Output each element of the array
cout << endl; // Move to the next line after printing each row
// Destructor to deallocate memory
~Array2D() {
// Deallocate memory for each row
for (int i = 0; i < rows; i++) {
delete[] data[i];
// Deallocate memory for the array of pointers
delete[ ] data;
};
int main( ) {
int arr[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11, 22, 33};
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
// Create an object of the Array2D class and initialize it with the 1D array
Array2D array2D(arr, size);
// Print the 2D array
array2D.print( );
return 0;
OTP GENERATOR PROGRAM
#include <iostream> // For standard input/output
#include <cstdlib> // For rand( ), srand( )
#include <ctime> // For time( ), time_t
int main( ) {
int lb = 1000, ub = 9999; // Lower and upper bounds for the OTP range
// Seed the random number generator with the current time
srand(time(1));
// Generate a random OTP within the specified range
int originalNumber = (rand( ) % (ub - lb + 1)) + lb;
// Record the start time to track OTP expiration
time_t startTime = time(nullptr);
// Display the generated OTP to the user
std::cout << "OTP Generated: " << originalNumber << std::endl;
std::cout << "Enter the OTP within 20 seconds: ";
// Read the user's input for the OTP
int userNumber;
std::cin >> userNumber;
// Record the current time to calculate elapsed time
time_t currentTime = time(nullptr);
int elapsedTime = static_cast<int>(currentTime - startTime);
// Check if the user's input matches the generated OTP and if the time limit has not expired
if (userNumber == originalNumber && elapsedTime <= 20) {
std::cout << "OTP Verified!" << std::endl;
} else {
std::cout << "OTP Expired or Incorrect!" << std::endl;
return 0;
EXCEL FUNCTIONS
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
#include <cmath>
// Using the standard namespace to avoid writing std:: before standard library functions
using namespace std;
class Text {
public:
// Function to run text-related functions
void runTextFunctions( ) {
int choice;
// Displaying menu options for text functions
cout << "********TEXT FUNCTIONS********" << endl;
cout << "1. CONCAT" << endl;
cout << "2. DOLLAR" << endl;
cout << "3. EXACT" << endl;
cout << "4. FIND" << endl;
cout << "5. LEFT" << endl;
cout << "6. RIGHT" << endl;
cout << "7. LOWER" << endl;
cout << "8. MID" << endl;
cout << "9. CAPITALIZE" << endl;
cout << "10. TRIM" << endl;
cout << "Enter your choice (1-10): ";
cin >> choice;
char text1[100], text2[100], result[100];
int position, count, number;
double value;
bool isEqual;
switch (choice) {
case 1:
cout << "Enter first text: ";
cin >> text1;
cout << "Enter second text: ";
cin >> text2;
CONCAT(text1, text2, result); // Concatenating two strings
cout << "Concatenated text: " << result << endl;
break;
case 2:
cout << "Enter a value: ";
cin >> value;
cout << "Enter number of decimals: ";
cin >> count;
DOLLAR(value, count, result); // Formatting a value as currency
cout << "Formatted value: " << result << endl;
break;
case 3:
cout << "Enter first text: ";
cin >> text1;
cout << "Enter second text: ";
cin >> text2;
isEqual = EXACT(text1, text2); // Checking if two texts are equal
cout << "Texts are " << (isEqual ? "equal" : "not equal") << endl;
break;
case 4:
cout << "Enter text: ";
cin >> text1;
cout << "Enter text to find: ";
cin >> text2;
cout << "Enter position: ";
cin >> position;
position = FIND(text1, text2, position); // Finding text within another text
cout << "Found at position: " << position << endl;
break;
case 5:
cout << "Enter text: ";
cin >> text1;
cout << "Enter count: ";
cin >> count;
LEFT(text1, count, result); // Extracting leftmost characters from a text
cout << "Leftmost characters: " << result << endl;
break;
case 6:
cout << "Enter text: ";
cin >> text1;
cout << "Enter count: ";
cin >> count;
RIGHT(text1, count, result); // Extracting rightmost characters from a text
cout << "Rightmost characters: " << result << endl;
break;
case 7:
cout << "Enter text: ";
cin >> text1;
LOWER(text1, result); // Converting text to lowercase
cout << "Lowercase text: " << result << endl;
break;
case 8:
cout << "Enter text: ";
cin >> text1;
cout << "Enter start position: ";
cin >> position;
cout << "Enter number of characters: ";
cin >> number;
MID(text1, position, number, result); // Extracting middle characters from a text
cout << "Mid text: " << result << endl;
break;
case 9:
cout << "Enter text: ";
cin >> text1;
CAPITALIZE(text1, result); // Capitalizing the first letter of a text
cout << "Capitalized text: " << result << endl;
break;
case 10:
cout << "Enter text: ";
cin >> text1;
TRIM(text1, result); // Removing leading and trailing spaces from a text
cout << "Trimmed text: " << result << endl;
break;
default:
cout << "Invalid choice!" << endl;
// Concatenating two strings
void CONCAT(const char* text1, const char* text2, char* result) {
strcpy(result, text1);
strcat(result, text2);
// Formatting a value as currency
void DOLLAR(double value, int decimals, char* result) {
char format[20];
sprintf(format, "$%%.%df", decimals);
sprintf(result, format, value);
// Checking if two texts are equal
bool EXACT(const char* text1, const char* text2) {
return strcmp(text1, text2) == 0;
// Finding text within another text
int FIND(const char* findText, const char* text, int position = 0) {
const char* found = strstr(text + position, findText);
if (found)
return found - text;
else
return -1;
// Extracting leftmost characters from a text
void LEFT(const char* text, int count, char* result) {
strncpy(result, text, count);
result[count] = '\0';
// Extracting rightmost characters from a text
void RIGHT(const char* text, int count, char* result) {
int length = strlen(text);
if (count >= length)
strcpy(result, text);
else
strcpy(result, text + length - count);
// Converting text to lowercase
void LOWER(const char* text, char* result) {
strcpy(result, text);
for (int i = 0; result[i]; i++) {
result[i] = tolower(result[i]);
// Extracting middle characters from a text
void MID(const char* text, int start, int number, char* result) {
strncpy(result, text + start, number);
result[number] = '\0';
// Capitalizing the first letter of a text
void CAPITALIZE(const char* text, char* result) {
strcpy(result, text);
if (result[0] != '\0') {
result[0] = toupper(result[0]);
// Removing leading and trailing spaces from a text
void TRIM(const char* text, char* result) {
int start = 0, end = strlen(text) - 1;
while (isspace(text[start]))
start++;
while (end > start && isspace(text[end]))
end--;
strncpy(result, text + start, end - start + 1);
result[end - start + 1] = '\0';
};
class Numbers {
public:
// Function to run number-related functions
void runNumberFunctions() {
int choice;
// Displaying menu options for number functions
cout << "********NUMBER FUNCTIONS********" << endl;
cout << "1. SUM" << endl;
cout << "2. AVERAGE" << endl;
cout << "3. MIN" << endl;
cout << "4. MAX" << endl;
cout << "5. COUNT" << endl;
cout << "6. ROUND" << endl;
cout << "7. ABS" << endl;
cout << "8. SQRT" << endl;
cout << "9. POWER" << endl;
cout << "10. MOD" << endl;
cout << "Enter your choice (1-10): ";
cin >> choice;
switch (choice) {
case 1: {
cout << "You selected SUM function." << endl;
int count;
cout << "Enter the number of elements: ";
cin >> count;
int* numbers = new int[count];
cout << "Enter " << count << " numbers: ";
for (int i = 0; i < count; ++i) {
cin >> numbers[i];
int sum = SUM(numbers, count);
cout << "Sum: " << sum << endl;
delete[] numbers;
break;
case 2: {
cout << "You selected AVERAGE function." << endl;
int count;
cout << "Enter the number of elements: ";
cin >> count;
int* numbers = new int[count];
cout << "Enter " << count << " numbers: ";
for (int i = 0; i < count; ++i) {
cin >> numbers[i];
float avg = AVERAGE(numbers, count);
cout << "Average: " << avg << endl;
delete[] numbers;
break;
case 3: {
cout << "You selected MIN function." << endl;
int count;
cout << "Enter the number of elements: ";
cin >> count;
int* numbers = new int[count];
cout << "Enter " << count << " numbers: ";
for (int i = 0; i < count; ++i) {
cin >> numbers[i];
int min = MIN(numbers, count);
cout << "Minimum: " << min << endl;
delete[] numbers;
break;
case 4: {
cout << "You selected MAX function." << endl;
int count;
cout << "Enter the number of elements: ";
cin >> count;
int* numbers = new int[count];
cout << "Enter " << count << " numbers: ";
for (int i = 0; i < count; ++i) {
cin >> numbers[i];
int max = MAX(numbers, count);
cout << "Maximum: " << max << endl;
delete[ ] numbers;
break;
case 5: {
cout << "You selected COUNT function." << endl;
int count;
cout << "Enter the number of elements: ";
cin >> count;
cout << "Count: " << count << endl;
break;
case 6: {
cout << "You selected ROUND function." << endl;
float number;
int decimals;
cout << "Enter the number: ";
cin >> number;
cout << "Enter the number of decimals: ";
cin >> decimals;
float rounded = ROUND(number, decimals);
cout << "Rounded number: " << rounded << endl;
break;
case 7: {
cout << "You selected ABS function." << endl;
int number;
cout << "Enter the number: ";
cin >> number;
int absNumber = ABS(number);
cout << "Absolute value: " << absNumber << endl;
break;
case 8: {
cout << "You selected SQRT function." << endl;
float number;
cout << "Enter the number: ";
cin >> number;
float sqrtNumber = SQRT(number);
cout << "Square root: " << sqrtNumber << endl;
break;
case 9: {
cout << "You selected POWER function." << endl;
float base, exponent;
cout << "Enter the base: ";
cin >> base;
cout << "Enter the exponent: ";
cin >> exponent;
float powered = POWER(base, exponent);
cout << "Result: " << powered << endl;
break;
case 10: {
cout << "You selected MOD function." << endl;
int numerator, denominator;
cout << "Enter the numerator: ";
cin >> numerator;
cout << "Enter the denominator: ";
cin >> denominator;
int modResult = MOD(numerator, denominator);
cout << "Modulus: " << modResult << endl;
break;
default:
cout << "Invalid choice!" << endl;
// Calculating the sum of elements in an array
int SUM(const int* numbers, int count) {
int sum = 0;
for (int i = 0; i < count; ++i) {
sum += numbers[i];
return sum;
// Calculating the average of elements in an array
float AVERAGE(const int* numbers, int count) {
if (count == 0)
return 0.0f;
float sum = static_cast<float>(SUM(numbers, count));
return sum / count;
// Finding the minimum element in an array
int MIN(const int* numbers, int count) {
if (count == 0)
return 0;
int min = numbers[0];
for (int i = 1; i < count; ++i) {
if (numbers[i] < min) {
min = numbers[i];
return min;
// Finding the maximum element in an array
int MAX(const int* numbers, int count) {
if (count == 0)
return 0;
int max = numbers[0];
for (int i = 1; i < count; ++i) {
if (numbers[i] > max) {
max = numbers[i];
return max;
// Returning the count of elements in an array
int COUNT(const int* numbers, int count) {
return count;
// Rounding a number to a specified number of decimals
float ROUND(float number, int decimals) {
float multiplier = pow(10, decimals);
return round(number * multiplier) / multiplier;
// Computing the absolute value of a number
int ABS(int number) {
return abs(number);
// Computing the square root of a number
float SQRT(float number) {
if (number < 0)
return NAN;
return sqrt(number);
// Computing the power of a base raised to an exponent
float POWER(float base, float exponent) {
return pow(base, exponent);
// Computing the modulus of a numerator divided by a denominator
int MOD(int numerator, int denominator) {
if (denominator == 0)
return 0;
return numerator % denominator;
};
int main( ) {
int choice;
// Displaying menu options for choosing between text and number functions
cout << "********EXCEL FUNCTIONS********" << endl;
cout << "1. TEXT" << endl;
cout << "2. NUMBERS" << endl;
cout << "Enter your choice (1-2): ";
cin >> choice;
switch (choice) {
case 1: {
Text text;
text.runTextFunctions( );
break;
case 2: {
Numbers numbers;
numbers.runNumberFunctions( );
break;
default:
cout << "Invalid choice!" << endl;
return 0;
IKEA PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Defining REV to sort product permutations in reverse order
#define REV
// Using the struct Perm as a string data type using a character array inside
// It will be used as an array of strings
typedef struct {
wchar_t word[300];
} Perm;
// Using preprocessor directive #ifdef to check if REV is defined earlier in the code
// Then define the comparator for the qsort accordingly if REV defined using
// descending comparator
#ifdef REV
// The comparison function for qsort to sort in descending order
int compDec(const void* a, const void* b) {
// wcscmp returns the difference between the first and second parameter
// If two words 'CAR' and 'RAC' are compared, it will compare all the characters
// Then, when it encounters the different characters, it will compare their ASCII values
// In this case, it will return a positive value because the ASCII code of 'R' will be greater
// Hence, no swap will occur
return wcscmp(((Perm*)b)->word, ((Perm*)a)->word);
#define COMPARE compDec
#else
// Doing the same but in reverse
// const: Indicates that the data pointed to by the pointer is constant
// and cannot be modified through this pointer. It's a safety measure to prevent
// unintended modification of data.
// void*: Indicates a pointer to an unspecified type. void* is a generic pointer
// type that can point to any data type, but the compiler doesn't know the exact
// type of data it's pointing to.
int comp(const void* a, const void* b) {
return wcscmp(((Perm*)a)->word, ((Perm*)b)->word);
#define COMPARE comp
#endif
// The swap function we are using to generate the permutation of a given word
// by swapping its characters
void swap(wchar_t* arr, int i, int j) {
wchar_t temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i
IKEA PROGRAM
nt main() {
// This is the buffer we are using to read lines from the file
wchar_t buff[300];
FILE *file;
int len,i,k,j;
// Creating the structure object
Perm permutations[5];
// Opening the file in read mode
file = fopen("products.txt", "r");
if (file == NULL) {
printf("Error opening file\n");
exit(1);
// Reading the file line by line until EOF
while (fgetws(buff, 300, file) != NULL) {
// Using the wcslen function to calculate the length of the word in the buffer
// Remove the newline character at the end of every string in the buffer
// If we don't use this, every string permutation will also contain a newline character
buff[wcslen(buff) - 1] = '\0';
len = wcslen(buff);
printf("Original Product Name : %ls\n\n", buff);
// The loop is copying the characters into the buffer
for (k = 0; k < 5; k++) {
Perm permuted;
wcscpy(permuted.word, buff);
// The loop is creating the permutation using rand
for (i = 0; i < len; i++) {
j = rand() % len;
swap(permuted.word, i, j);
// Storing the names into the permutation structure
permutations[k] = permuted;
// Using qsort by giving the array base address, array size, byte size of array
// content, and comparator
qsort(permutations, 5, sizeof(permutations[0]), COMPARE);
// Printing all the permutations
for (k = 0; k < 5; k++) {
printf("Permutation %d: %ls\n\n", k + 1, permutations[k].word);
printf("\n\n");
IKEA PROGRAM
fclose(file);
return 0;
}