Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views13 pages

C++ Tasks

The document contains multiple programming tasks implemented in C++, including string reversal, a calculator program, Fibonacci series generation, a guessing game, file copying, palindrome checking, a student grade calculator, array sorting, a rock-paper-scissors game, and a simple inventory management system. Each task is encapsulated in a class with a main function demonstrating its functionality. The tasks cover a range of basic programming concepts such as loops, conditionals, file handling, and object-oriented programming.

Uploaded by

krashan.042732
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)
10 views13 pages

C++ Tasks

The document contains multiple programming tasks implemented in C++, including string reversal, a calculator program, Fibonacci series generation, a guessing game, file copying, palindrome checking, a student grade calculator, array sorting, a rock-paper-scissors game, and a simple inventory management system. Each task is encapsulated in a class with a main function demonstrating its functionality. The tasks cover a range of basic programming concepts such as loops, conditionals, file handling, and object-oriented programming.

Uploaded by

krashan.042732
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/ 13

Task 1 - String Reversal.

#include<iostream>
#include<cstring>
using namespace std;
class String
{
public:
char str[100];
String(){};

void reverse();
void display();
};
void String::reverse()
{
int i,j,len=0;
cout << "Enter a String : ";
cin.get(str,100);
int Length = strlen(str);
char A;
for(i=0,j=Length-1; i<j; i++, j--)
{
A = str[i];
str[i] = str[j];
str[j] = A;
}
}
void String::display()
{
cout<<"\n"<<str;
}
int main()
{
String S;
S.reverse();
S.display();

return 0;
}
Task 2 - Calculator Program
#include<iostream>
using namespace std;
class Calculator
{
public:
float num1, num2, result;
char ope;
Calculator()
{
cout << "\n/*****--CALCULATOR--*****/\n";
cout << "\nEnter First Number : ";
cin >> num1;
cout<< "\nEnter Operator (+,-,*,/,%) : ";
cin >> ope;
cout << "\nEntre Second Number : ";
cin >> num2;
switch(ope)
{
case '+' : result = num1+num2;
break;
case '-' : result = num1-num2;
break;
case '*' : result = num1*num2;
break;
case '/' : if(num2==0)
{
cout << "Error! Division By 0.";
}
result = num1/num2;
break;
case '%' : result = (int)num1 % (int)num2;
break;
default : cout << "Invalid Operator!";
}
cout << "\nResult : " << result;
}
};
int main()
{
Calculator c1;
return 0;
}
Task 3 - Fibonacci Series
#include<iostream>
using namespace std;
class Fibonacci
{
public:
int n, num1 = 0, num2 = 1, num3 = 0;
Fibonacci()
{

cout << "Enter Number of terms : ";


cin >> n;

cout << "Fibonacci Series : "<< num1 << ", " << num2 << ", ";

num3 = num1+num2;

while(num3<=n)
{
cout << num3 << ", ";
num1 = num2;
num2 = num3;
num3 = num1 + num2;
}
}
};
int main()
{
Fibonacci f1;
return 0;
}

Task 4 – Guessing Game


#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class GuessingGame
{
public:
void guessingthenum()
{

srand(time(0));
int computer_choice = rand() % 100 + 1;
int n;
do

{
cout << "Enter The Number : ";
cin >> n;
cout << "Computer Generated Number : " << computer_choice << endl;

if(n == computer_choice)
{
cout << "Well Done!! You Have Guessed The Right One." << endl;
break;
}
else if(n < computer_choice)
{
cout << "Good One.. But You Need To Increase The Value To Get
The Correct Number. "<< endl;
}
else
{
cout << "You Have Gone Out Of Range...Go Lower..."<<endl;
}
}
while (true);
}
};
int main()
{
GuessingGame g1;
g1.guessingthenum();

return 0;

}
Task 5 - File Copy
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void copyFile(const string& sourceFileName, const string& destinationFileName) {


ifstream sourceFile(sourceFileName.c_str());
ofstream destinationFile(destinationFileName.c_str());

if (!sourceFile.is_open()) {
cerr << "Error: Unable to open source file: " << sourceFileName << endl;
return;
}

if (!destinationFile.is_open()) {
cerr << "Error: Unable to open destination file: " << destinationFileName << endl;
return;
}

destinationFile << sourceFile.rdbuf();


cout << "File copied successfully!" << endl;

sourceFile.close();
destinationFile.close();
}

int main() {
string sourceFileName, destinationFileName;

cout << "Enter the name of the source file: ";


getline(cin, sourceFileName);

cout << "Enter the name of the destination file: ";


getline(cin, destinationFileName);

copyFile(sourceFileName, destinationFileName);

return 0;
}
Task 6 - Palindrome Checker
#include<iostream>
#include<cstring>
using namespace std;
class String
{
public:
char str[100];
String(){};
void Palindrome();
};
void String::Palindrome()
{
int i,j,l, k;
cout << "Enter a string = ";
cin.get(str,100);

for(l=0; str[l]!='\0';l++);
l--;
k = l/2;
for(i=0,j=l;i<k;i++,j--)
{

if(str[i]==str[j])
{
cout<<"This is a Palindrome.";
return;
}

}
cout<< "This is not Palindrome.";
}
int main()
{
String S;
S.Palindrome();

return 0;
}
Task 7 - Student Grade Calculator
#include <iostream>
using namespace std;

int main() {
int numSubjects;
double totalGrades = 0.0;

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


cin >> numSubjects;

if (numSubjects <= 0)
{
cout << "Invalid number of subjects." << endl;
return 1;
}

for (int i = 1; i <= numSubjects; ++i)


{
double grade;
cout << "Enter the grade for subject " << i << ": ";
cin >> grade;

if (grade < 0 || grade > 100)


{
cout << "Invalid grade. Grade must be between 0 and 100." << endl;
return 1;
}

totalGrades += grade;
}

double averageGrade = totalGrades / numSubjects;

cout << "Average grade: " << averageGrade << endl;

return 0;
}
Task 8 - Array Sorting
#include<iostream>
using namespace std;
class Sorting
{
public:
int arr[10];
int size ;
void input();
void Ascending();
void Descending();
};
void Sorting::input()
{
cout<<"\nEnter the size of the array : \n";
cin >> size;
cout<< "Enter "<< size << " elements : \n";
for(int i=1;i<=size;i++)
{
cin>>arr[i];
}
}

void Sorting::Ascending()
{
int temp;
for(int i=1; i<=size; i++)
{
for(int j=1;j<=size-i;j++)
{
if(arr[j]>arr[j+1])
{

temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<< "\nArray in Ascending order : \n";
for(int i=1;i<=size;i++)
{
cout<<arr[i]<<"\n";
}
}
void Sorting::Descending()
{
int temp1;
for(int i=1; i<=size; i++)
{
for(int j=1;j<=size-i;j++)
{
if(arr[j]<arr[j+1])
{

temp1=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp1;
}
}
}
cout<< "\nArray in Descending order : \n";
for(int i=1;i<=size;i++)
{
cout<<arr[i]<<"\n";
}
}
int main()
{
Sorting s1;
s1.input();
s1.Ascending();
s1.Descending();
}

Task 9 - Rock-Paper-Scissor Game


#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{

int rock = 3;
int paper = 2;
int scissor = 1;
int userchoice;
srand(time(NULL));
while(true)
{
cout << "Enter Your Choice (1 for Rock, 2 for Paper, 3 for Scissor, 0 to
Exit) : ";
cin >> userchoice;

if(userchoice == 0)
{
cout << "Exiting The Game..."<< endl;
break;
}
else if(userchoice < 1 || userchoice > 3)
{
cout << "Invalid Choice!! Please Enter a Number Between 1 and
3." << endl;
continue;
}

cout << "You Chose :";


switch(userchoice)
{
case 1 :
cout << "ROCK" << endl;
break;
case 2:
cout << "PAPER" << endl;
break;
case 3:
cout << "SCISSOR" << endl;
break;

int computerchoice = rand()%3 + 1;


cout << "Computer Choice : ";
switch(computerchoice)
{
case 1:
cout << "ROCK" << endl;
break;
case 2:
cout << "PAPER" << endl;
break;
case 3:
cout << "SCICCOR" << endl;
break;
}

if(userchoice == computerchoice)
{
cout << "DRAW!" << endl;
}
else if( (userchoice == 1 && computerchoice == 3) ||
(userchoice == 2 && computerchoice == 1) ||
(userchoice == 3 && computerchoice ==2))
{
cout << "You Win!.." << endl;
}
else
{
cout << "Computer Wins!" << endl;
}
}
return 0;

Task 10 - Simple Inventory Management System


#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Item {
int id;
string name;
int quantity;
};

class InventoryManager {
private:
vector<Item> inventory;

public:

void addItem(int id, string name, int quantity) {


Item newItem;
newItem.id = id;
newItem.name = name;
newItem.quantity = quantity;
inventory.push_back(newItem);
cout << "Item added successfully!" << endl;
}

void displayItems() {
cout << "Inventory:" << endl;
cout << "ID\tName\tQuantity" << endl;
for (size_t i = 0; i < inventory.size(); ++i) {
cout << inventory[i].id << "\t" << inventory[i].name << "\t" << inventory[i].quantity << endl;
}
}

void searchByName(string name) {


bool found = false;
cout << "Search Results:" << endl;
cout << "ID\tName\tQuantity" << endl;
for (size_t i = 0; i < inventory.size(); ++i) {
if (inventory[i].name == name) {
cout << inventory[i].id << "\t" << inventory[i].name << "\t" << inventory[i].quantity <<
endl;
found = true;
}
}
if (!found) {
cout << "Item not found!" << endl;
}
}

void searchById(int id) {


bool found = false;
cout << "Search Results:" << endl;
cout << "ID\tName\tQuantity" << endl;
for (size_t i = 0; i < inventory.size(); ++i) {
if (inventory[i].id == id) {
cout << inventory[i].id << "\t" << inventory[i].name << "\t" << inventory[i].quantity <<
endl;
found = true;
break;
}
}
if (!found) {
cout << "Item not found!" << endl;
}
}
};

int main() {
InventoryManager inventoryManager;

int id, quantity;


string name;
cout << "Enter the ID of the item: ";
cin >> id;
cout << "Enter the name of the item: ";
cin.ignore();
getline(cin, name);
cout << "Enter the quantity of the item: ";
cin >> quantity;

inventoryManager.addItem(id, name, quantity);


inventoryManager.displayItems();

return 0;
}

You might also like