Skip to content

Commit

Permalink
Add Practise Programs
Browse files Browse the repository at this point in the history
Added Practise Programs
  • Loading branch information
adi-075 authored Mar 21, 2021
1 parent e16ddd4 commit b2207d5
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Introduction to C++/Practise Programs/employ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
using namespace std;
class Employee
{
private:
char name[20];
int id, salary;
public:
int read()
{
cout<<"Enter Employee id, salary, name: ";
cin>>id>>salary>>name;
return 0;
}
int display()
{
cout<<"Employee ID: \n"<<id<<"\nSalary: \n"<<salary<<"\nName: \n"<<name;
}
};

int main()
{
Employee e1;
e1.read();
e1.display();
}
42 changes: 42 additions & 0 deletions Introduction to C++/Practise Programs/employee2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
using namespace std;
class Employee
{
private:
int id;
char name[20];
int salary;
public:
int read()
{
cout<<"Enter Employee id, salary, name: ";
cin>>id>>salary>>name;
return (0);
}
int display()
{
cout<<"Employee ID: \n"<<id<<"\nSalary: \n"<<salary<<"\nName: \n"<<name;
return (0);
}
int getSalary()//Need to access private data member-public member function
{
return salary;
}
};

int main()
{
Employee e1, e2;
e1.read();
e2.read();

if(e1.getSalary()>e2.getSalary())
{
e1.display();
}
else
{
e2.display();
}
return (0);
}
53 changes: 53 additions & 0 deletions Introduction to C++/Practise Programs/frndFn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
using namespace std;
/*forword declaration--Let the compiler know of an other class coming later*/
class test2;
class test1{
int sub1;
int sub2;
public:
void read(int m1,int m2){
sub1=m1;
sub2=m2;
}
void display(){
cout<<sub1<<"\t"<<sub2;
}
friend void avgMarks(test1 t1, test2 t2);
//Use this frnd function as a common function between two classes test1 and test2
};
class test2{
private:
int sub1, sub2;
public:
void read(int n1, int n2)
{
sub1=n1;
sub2=n2;
}
void display()
{
cout<<sub1<<"\t"<<sub2;
}
friend void avgMarks(test1 t1, test2 t2);
};
//frnd fn definition--normal fn definition. Used to declare a function which will be shared between two or multiple classes, for example in our case, avgMarks will access the pvt. data between two classes
void avgMarks(test1 t1, test2 t2)
{
float avg = (t1.sub1+t1.sub2+t2.sub1+t2.sub2)/4.0;
cout<<"\nAverage of two tests:"<<avg<<"\n";
}
int main(){
test1 T1;
test2 T2;
cout<<"Read data for Test1\n";
T1.read(45,34);
cout<<"Read data for Test2\n";
T2.read(45,43);
cout<<"Test Marks\n Sub1\tSub2\n";
T1.display();
cout<<"\n";
T2.display();
avgMarks(T1,T2); //Frnd function
return 0;
}
13 changes: 13 additions & 0 deletions Introduction to C++/Practise Programs/odd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
using namespace std;
int main()
{
int i;
int sum=0;
for(i=1;i<=100;i=i+2)
{
sum=sum+i;
}
cout<<"Sum of First 100 odd numbers: "<<sum;
return 0;
}
23 changes: 23 additions & 0 deletions Introduction to C++/Practise Programs/prime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<iostream>
using namespace std;
int main(){
double a,c=0;
int i;
cout<<"Enter a no: ";
cin>>a;
for(i=0;i<=a;i++)
{
if(a%i==0)
{
c++;
}
}
if(c==2)
{
cout<<"Prime Number: "<<a;
}
else{
cout<<"Not a prime";
}
return 0;
}
37 changes: 37 additions & 0 deletions Introduction to C++/Practise Programs/runtimeIni.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<iostream>
using namespace std;
class ArrOfObjEmp{
private:
char name[20];
int id, salary;
public:
int read()
{
cin>>id>>salary>>name;
return 0;
}
int display()
{
cout<<id<<"\t"<<salary<<"\t"<<name<<endl;
return 0;
}
}; //End
int main(){
//array of objects
//Declaration
ArrOfObjEmp A[5];
//Initialize--Read Fn--For Loop
for(int i=0;i<5;i++)
{
cout<<"Enter Employee id: "<<i+1<<"\tName, "<<"Salary";
A[i].read();
}
//Display
cout<<"Employee Details\n";
cout<<"Emp IDs\tEmpName\tEmpSal\n";
for(int i=0;i<5;i++)
{
A[i].display();
}

}
38 changes: 38 additions & 0 deletions Introduction to C++/Practise Programs/runtimeType2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<iostream>
using namespace std;
class ArrOfObjEmp{
private:
char name[20];
int id, salary;
public:
int read()
{
cin>>id>>salary>>name;
return 0;
}
int display()
{
if(salary>10000)
cout<<id<<"\t"<<salary<<"\t"<<name<<endl;
return 0;
}
}; //End
int main(){
//array of objects
//Declaration
ArrOfObjEmp A[5];
//Initialize--Read Fn--For Loop
for(int i=0;i<5;i++)
{
cout<<"ID:"<<i+1<<" Name, "<<"Salary";
cout<<"\n";
A[i].read();
}
//Display
cout<<"Employee Details\n";
cout<<"Emp IDs\tEmpName\tEmpSal\n";
for(int i=0;i<5;i++)
{
A[i].display();
}
}
27 changes: 27 additions & 0 deletions Introduction to C++/Practise Programs/simpleInterest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<iostream>
using namespace std;
class simp{
double pamt;
int noyears;
double rateofinterest;
double si;
public:
void read(){
cout<<"Enter P.Amount, No. of years, Rate of Interest \n";
cin>>pamt>>noyears>>rateofinterest;
}
void display(){
cout<<"P. amount: Rs"<<pamt<<"\t No.years:"<<noyears<<"\t Rate of Interest:"<<rateofinterest<<"%"<<endl;
}
void getamount(){
si=(pamt*noyears*rateofinterest)/100;
cout<<"Interest in Amt. is: "<<si;
}
};
int main(){
simp s;
s.read();
s.display();
s.getamount();
return 0;
}
16 changes: 16 additions & 0 deletions Introduction to C++/Practise Programs/sumOfDigits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include<iostream>
using namespace std;
int main()
{
int n,sum=0,m;
cout<<"Enter a number: ";
cin>>n;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
cout<<"Sum is: "<<sum;
return 0;
}
43 changes: 43 additions & 0 deletions Introduction to C++/Practise Programs/time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
using namespace std;
class Time{
int hrs, min;
public:
void read(int h, int m)
{
hrs=h;
min=m;
}
void display()
{
cout<<hrs<<":"<<min;
}
void add(Time t1, Time t2)
{
min = t1.min+t2.min;
hrs =t1.hrs+t2.hrs+ (min/60);
min = min%60;

}
};

int main()
{

Time T1,T2,T3;
cout<<"Read data for Time T1\n";
T1.read(4,34);
cout<<"Read data for Time T2\n";
T2.read(4,43);
cout<<"\nTime T1\n";
T1.display();
cout<<"\nTime T2\n";
T2.display();
cout<<"\n";

T3. add (T1,T2);
cout<<"\nTime T3\n";
T3.display();
return 0;

}
59 changes: 59 additions & 0 deletions Introduction to C++/Practise Programs/type3c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
using namespace std;
class Employee
{
private:
int id;
char name[20];
int salary;
public:
int read()
{
//cout<<"Enter Employee id, salary, name: ";
cin>>id>>salary>>name;
return (0);
}
int display()
{
cout<<"Employee ID: \n"<<id<<"\nSalary: \n"<<salary<<"\nName: \n"<<name;
return (0);
}
int getSalary()//Need to access private data member-public member function
{
return salary;
}
};

int main()
{
int i;
//array of objects
//Declaration
Employee A[5];
//Initialize--Read Fn--For Loop
for(i=0;i<5;i++)
{
cout<<"Enter Employee"<<i+1<<"Id, Name and Salary\n";
A[i].read();
}

int max = A[0].getSalary(); //assumption
for(i=1;i<5;i++)
{
if(A[i].getSalary()>max)
{
max=A[i].getSalary();
}
}

//Display
cout<<"Employee Details\n";

for(int i=0;i<5;i++)
{
if(A[i].getSalary()==max)
A[i].display();
}

return 0;
}

0 comments on commit b2207d5

Please sign in to comment.