0% found this document useful (0 votes)
3 views15 pages

Code

The document contains multiple C++ case studies demonstrating various programming concepts including temperature conversion, compound interest calculation, number display, class usage for products and employees, file handling, inheritance, exception handling, string manipulation, and basic arithmetic operations. Each case study presents a specific problem and provides a solution through code examples. The document serves as a comprehensive guide for learning fundamental programming techniques in C++.

Uploaded by

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

Code

The document contains multiple C++ case studies demonstrating various programming concepts including temperature conversion, compound interest calculation, number display, class usage for products and employees, file handling, inheritance, exception handling, string manipulation, and basic arithmetic operations. Each case study presents a specific problem and provides a solution through code examples. The document serves as a comprehensive guide for learning fundamental programming techniques in C++.

Uploaded by

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

//case study 1 of session plan

#include<iostream>

using namespace std;

int main()

int choice;

float temperature,fahreheit,Celsius;

cout<<"---Display Menu---"<<endl;

cout<<"1: Convert Fahrenheit to Celsius:"<<endl;

cout<<"2: Convert Celsius to Fahrenheit:";

cin>>choice;

if(choice ==1)

cout<<"Enter temperature to fahrenheit:";

cin>>temperature;

Celsius=(temperature-32)*5.0/9.0;

cout<<"Celsius"<<Celsius<<endl;

else if(choice==2)

cout<<"Enter temperature to celsius:";

cin>>temperature;

fahreheit=(temperature+32)*9.0/5.0;

cout<<"Fahrenheit"<<fahreheit<<endl;

else

cout<<"Invalid choice, please enter number 1 and 2";

return 0;

//case study 3
#include<iostream>

#include<iomanip>

using namespace std;

int main()

float principal,interestRate,compound interest;

int years;

cout<<"Enter principal amount:";

cin>>principal amount;

cout<<"Enter number of year:";

cin>>years;

cout<<"Enter interest rate:";

cin>>interestRate;

compound interest= principal*(1+interest rate/100,years);

cout<<fixed<<setprecision(2)<<"At the end of the"<<years<<"years,you will have"<<compound


interest<<"amt"<<endl;

return 0;

//case study 4

#include<iostream>

using namespace std;

void Displaynumbers(int n)

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

cout<<i<<" ";

cout<<endl;

int main()

int n;
cout<<"Enter a positive integer:";

cin>>n;

if(n<1){

cout<<"Please enter a positive integer greater than 0."<<endl;

}else{

cout<<"Displaying the first "<<n<<"positive integers:"<<endl;

Displaynumbers(n);

return 0;

//case study 5

#include<iostream>

using namespace std;

class Product

string productname;

int productid;

int price;

public:

Product()

productname="wafer";

productid=20;

price=230;

void discount()

int discountamount=(discountamount/100)*price;

price -=discountamount;

cout<<"A discount of " <<discountamount << " % is applied" <<endl;

void display()
{

cout<<" product details:" <<endl;

cout<<"productname:" <<productname<<endl;

cout<<"productid:" <<productid<<endl;

cout<<"price:" <<price<<endl;

};

int main()

Product pro1;

int discountamount;

cout<<"/n Enter discountamount: ";

cin>>discountamount;

pro1.discount();

pro1.display();

return 0;

//case study 6

#include<iostream>

using namespace std;

class Employee

string empname;

int empid;

static int empcount;

public:

Employee(string empname,int empid)

empname=empname;

empid=empid;

empcount++;

}
void display()

cout<<"Total Employee:"<<empcount<<endl;

};

int Employee::empcount=0;

int main ()

Employee emp1("aryan",101);

Employee emp2("Raj",103);

Employee emp3("smit",104);

emp1.display();

emp2.display();

emp3.display();

Employee emp4("virat",18);

emp4.display();

return 0;

//case study 7

#include<iostream>

using namespace std;

class BankAccount

string accholder;

string accname;

int accnumber;

static int accountCount;

public:

BankAccount(string accholder,string accname,int accnumber)

{
accholder=accholder;

accname=accname;

accnumber=accnumber;

accountCount++;

void display()

cout<<"Total Bank account:"<<accountCount<<endl;

};

int BankAccount::accountCount=0;

int main()

BankAccount ba1("icici","aryan",123);

ba1.display();

BankAccount ba2("bob","harsh",1245);

ba2.display();

BankAccount ba3("pnb","prachi",24353);

ba3.display();

BankAccount ba4("sbi","raj",5677);

ba4.display();

return 0;

//file handling program

#include<iostream>

#include<fstream>

using namespace std;

int main()

char ch;
ofstream outfile("textfile.txt");

if(!outfile.is_open()){

cerr<<"error creating file!"<<endl;

return 1;

cout<<"Enter text(type '#' to stop):";

while(cin.get(ch) && ch !='#'){

outfile<<ch;

outfile.close();

ifstream infile("textfile.txt");

if(!infile){

cerr<<"error opening file for reading!"<<endl;

return 1;

cout<<"\n file content:\n";

while(infile>>ch){

cout<<ch;

infile.close();

//single inheritance program

#include<iostream>

using namespace std;

class Person

protected:

string name;

int age;

public:

Person(string n,int a)

{
name=n;

age=a;

void displaydetails()

cout<<"name:"<<name<<endl;

cout<<"age:"<<age<<endl;

};

class Student:public Person

private:

char grade;

public:

Student(string n,int a,char g):Person(n,a)

grade=g;

void displaygrade()

cout<<"grade:"<<grade<<endl;

};

int main()

Student s("aryan",18,'A');

s.displaydetails();

s.displaygrade();

return 0;

//try catch excemption handling program

#include<iostream>

#include<fstream>
using namespace std;

int main()

int num1,num2;

cout<<"enter number 1:";

cin>>num1;

cout<<"enter number 2:";

cin>>num2;

try{

if(num2!=0)

float res=(float) num1/num2;

cout<<"Result:"<<res;

else{

throw(num2);

catch(int num2)

cout<<"Divide by zero"<<endl;

cout<<"existing main()...";

return 0;

//string program

#include<iostream>

using namespace std;

int main()

string str;
cout<<"Enter a string:";

cin>>str;

cout<<"Uppercase:";

for(int i=0;i<str.length();i++){

cout<<char(toupper(str[i]));

cout << "\nLowercase: ";

for (int i = 0; i < str.length(); i++) {

cout << char(tolower(str[i]));

return 0;

//session plan program 2

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

printf("\n\t\t\t ------------");

printf("\n\t\t\t Roll no:02");

printf("\n\t\t\t Name: prajapati aryan");

printf("\n\t\t\t City: ahmedabad");

printf("\n\t\t\t -----------------");

getch();

Output

--------------

Roll no: 02

Name: Prajapati Aryan

City: Ahmedabad

----------------
// Write a Program For Area and Perimeter of Rectangle.

#include<iostream>

using namespace std;

int main()

float area, length,width,perimeter;

cout<<"Enter the length of rectangle:";

cin>>length;

cout<<"Enter the width of rectangle:";

cin>>width;

area=length*width;

perimeter=2*(length+width);

cout<<"area of a rectangle:"<<area<<endl;

cout<<"perimeter of a rectangle:"<<perimeter<<endl;

return 0;

//Write a Program to Check Even or Odd Integers.

#include<iostream>

using namespace std;

int main()

int n,i;

cout<<"Enter n:";

cin>>n;

if(i%2==0)

cout<<n<<" is even"<<endl;

else{
cout<<n<<" is odd"<<endl;

return 0;

//Write a Program to Find Largest Among 3 Numbers.

#include<iostream>

using namespace std;

int main()

int a=34,b=67,c=89;

if(a>=b)

if(a>=c)

cout<<a;

else

cout<<c;

else{

if(b>=c)

cout<<b;

else

cout<<c;

return 0;

//Write a program to check whether a number is prime or not

#include<iostream>

using namespace std;

int main()

int num,i;

cout<<"enter number:";
cin>>num;

if(i%2==0)

cout<<num<<"is not a prime number"<<endl;

else{

cout<<num<<"is prime number";

return 0;

//Write a program that displays the sum of the n terms of even natural numbers.

#include<iostream>

#include<cmath>

using namespace std;

int main()

int base;

int exponent,result;

cout<<"Enter base:";

cin>>base;

cout<<"Enter exponent:";

cin>>exponent;

result= pow(base,exponent);

cout<<base<<" is raised to the power " <<exponent <<" is: " <<result <<endl;

return 0;

//Write a program in to display a pattern like a right angle triangle using an asterisk.

#include<iostream>

using namespace std;

int main()

int rows;
cout<<"input number of rows:";

cin>>rows;

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

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

cout<<"*";

cout<<endl;

return 0;

//Print numbers from 1 to 20, skipping even numbers (using continue).

#include <iostream>

using namespace std;

int main() {

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

if (i % 2 == 0)

continue;

cout << i << " ";

return 0;

//Overload a function to find the maximum of two, three, and four numbers.

#include <iostream>

using namespace std;

int max(int a, int b) {

return (a > b) ? a : b;

int max(int a, int b, int c) {

return max(max(a, b), c);

int max(int a, int b, int c, int d) {


return max(max(a, b), max(c, d));

int main() {

cout << "Max of 2 numbers (20, 30): " << max(20, 30) << endl;

cout << "Max of 3 numbers (3, 55, 16): " << max(3, 55, 16) << endl;

cout << "Max of 4 numbers (34, 100, 89, 99): " << max(34, 100, 89, 99) << endl;

return 0;

You might also like