100% found this document useful (1 vote)
326 views

Armstrong or Not Using Classes

The document contains source code for various C++ programs that demonstrate the use of classes and objects to solve problems like checking if a number is Armstrong or a palindrome, calculating factorial, reversing a number, converting binary to decimal, checking if a number is prime, calculating the sum of digits in a number, using inline functions, calculating factorial using recursion, using static class members, using static member functions, calculating simple interest using default arguments, calculating student marks and grades, calculating employee salary, and calculating time by passing objects as arguments to functions.

Uploaded by

Ruhtra Nathan
Copyright
© © All Rights Reserved
100% found this document useful (1 vote)
326 views

Armstrong or Not Using Classes

The document contains source code for various C++ programs that demonstrate the use of classes and objects to solve problems like checking if a number is Armstrong or a palindrome, calculating factorial, reversing a number, converting binary to decimal, checking if a number is prime, calculating the sum of digits in a number, using inline functions, calculating factorial using recursion, using static class members, using static member functions, calculating simple interest using default arguments, calculating student marks and grades, calculating employee salary, and calculating time by passing objects as arguments to functions.

Uploaded by

Ruhtra Nathan
Copyright
© © All Rights Reserved
You are on page 1/ 88

1.

ARMSTRONG OR NOT USING CLASSES

AIM:
write a c++ program to check whether the given number is
armstrong or not using classes.

SOURCE CODE:
#include<iostream.h>

#include<conio.h>

class arm

int r,n,t;

public:

void gets();

void puts();};

void arm::gets(){

cout<<"\n enter the number";

cin>>n;}

void arm::puts()

int sum=0;

t=n;

while(n!=0)
OUTPUT:
2.PALINDROME OR NOT USING CLASSES
AIM:
write a c++ program to check whether the given number is
palindrome or not using classes.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class palin
{
int s,n,t,r;
public:
void get();
void display();
};
void palin::get()
{
cout<<"enter a number";
cin>>n;
}
void palin::display()
{
t=n;
s=0;
while(n!=0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
if(s==t)
{
cout<<"the number"<<t<<"is palindrome";
}
OUTPUT:
3. FACTORIAL VALUE USING CLASS

AIM:
write a c++ program to calculate the factorial value for a given
number.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class fact
{
int i,n;
public:
void getdata();
void puts();
};
void fact::getdata()
{
cout<<"enter the no";
cin>>n;
}
void fact::puts()
{
int f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<"the factorial value is"<<f;
}
void main()
{
OUTPUT:
4.REVERSE NUMBER USING CLASS

AIM:
write a c++ program to reverse the number for a given number.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class re
{
int n;
public:
void read();
void calc();
};
void re::read()
{
cout<<"enter a number";
cin>>n;
}
void re::calc()
{
int a,r,rev=0;
a=n;
while(n!=0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
cout<<"\n reverse of the given number"<<a<<"is"<<rev;
}
void main()
{
OUTPUT:
5.BINARY TO DECIMAL USING CLASS
AIM:
write a c++ program to convert binary to decimal for a given
number.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class bin
{
int binary,decimal,j,rem,n;
public:
void gets();
void puts();
};
void bin::gets()
{
cout<<"enter the number";
cin>>n;
}
void bin::puts()
{
decimal=0;
j=1;
while(binary!=0)
{
rem=binary%10;
decimal+=(rem*j);
j=j*2;
binary/=10;
}
cout<<"decimal value of" <<binary;
OUTPUT:
6.PRIME OR NOT USING CLASS

AIM:
write a c++ program to check whether the given number is prime
or not.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class prime
{
int num,i,s;
public:
void getnumber();
void display();
};
void prime::getnumber()
{
cout<<"enter a no";
cin>>num;
}
void prime::display()
{
s=0;
for(i=2;i<(num/2);i++)
{
if(num%i==0)
OUTPUT:
7.SUM OF GIVEN DIGIT USING CLASS

AIM:
write a c++ program to calculate the sum of given digit.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class sum
{
int n,r,sum;
public:
void gets();
void puts();
};
void sum::gets()
{
cout<<"enter the number";
cin>>n;
}
void sum::puts()
{
sum=0;
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
cout<<"sum of given digit is"<<sum;
}
void main()
{
sum d;
OUTPUT:
8.INLINE FUNCTION

AIM:
write a c++ program to calculate the value using inline function.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
inline float mul(float a,float b)
{
return (a*b);
}
inline double div(double a,double b)
{
return (a/b);
}
void main()
{
float x,y;
clrscr();
cout<<"enter the value of x”;
cin>>x;
cout<<”enter the value of y”;
cin>>y;
cout<<mul(x,y)<<"\n";
cout<<div(x,y)<<"\n";
getch();
}
OUTPUT:
enter the value of x:10
enter the value of y:20
200
0.5
9.FACTORIAL USING RECURSION.

AIM:
write a c++ program to calculate the factorial value using recursion.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
long fact(int n)
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
int main()
{
int num;
cout<<"enter a positive integer";
cin>>num;
cout<<"factorila of"<<num<<"is"<<fact(num);
getch();
return 0;
}
OUTPUT:
10.STATIC CLASS MEMBER.

AIM:
write a c++ program to display a value using static class member.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class item
{
static int count;
int no;
public:
void getdata(int a)
{
no=a;
++count;
}
void getcount(void)
{
cout<<"count:"<<count<<"\n";
}
};
int item::count;
int main()
{
item a,b,c;
clrscr();
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
OUTPUT:
11.STATIC MEMBER FUNCTION

AIM:
write a c++ program to dispaly a number using static member
function.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class test
{
static int count;
int code;
public:
void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"object number:"<<code<<"\n";
}
static void showcount(void)
{
cout<<"count:"<<count<<"\n";
}
};
int test::count;
int main()
{
test t1,t2;
clrscr();
t1.setcode();
t2.setcode();
OUTPUT:
12.SI USING DEFAULT ARGUMENTS.

AIM:
write a c++ program to calculate a simple interest using
default arguments.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
float si(int p,int n,int r=5)
{
return(p*n*r)/100;
}
void main()
{
int p,n,r;
clrscr();
cout<<"\n enter the principal amount:";
cin>>p;
cout<<"\n enter the duration(in year):";
cin>>n;
cout<<"\n enter the rate of interest:";
cin>>r;
cout<<"SI="<<si(p,n,r);
cout<<"SI="<<si(p,n);
getch();
}
OUTPUT:
if(unit>100&&unit<=300)
{
cout<<"2 rupees per unit";
cout<<"unit is:"<<unit<<"\n";
amt=unit*2;
cout<<"rupees is"<<amt<<"\n";
}
else if(unit>300&&unit<=500)
{
cout<<"3 rupees per unit";
cout<<"unit is:"<<unit<<"\n";
amt=unit*3;
cout<<"rupees is"<<"\n"<<amt;
}
else if(unit>500&&unit<1000)
{
cout<<" 4 rupees per unit";
cout<<"unit is:"<<unit<<"\n";
amt=unit*4;
cout<<"rupees is"<<"\n"<<amt;
}
else if(unit>=1000)
{
cout<<" 6 rupees per unit";
cout<<"unit is:"<<unit<<"\n";
amt=unit*6;
cout<<"rupees is"<<"\n"<<amt;
}

else
{
cout<<"no bill for less than 100 units";
}
}
void main()
OUTPUT:
cout<<"marks
is"<<m1<<"\n"<<m2<<"\n"<<m3<<"\n"<<m4<<"\n"<<m5;
cout<<"\ntotal:"<<tot;
cout<<"\naverage is:"<<avg<<"\n";
if((m1>=35)&&(m2>=35)&&(m3>=35)&&(m4>=35)&&(m5>=35))
{
cout<<"RESULT:PASS"<<"\n";
if(avg>=35&&avg<45)
{
cout<<"grade C";
}
else if((avg>=45)&&(avg<=60))
{
cout<<"grade B";
}
else if((avg>60)&&(avg<75))
{
cout<<"grade A";
}
else
{
cout<<"destination";
}
}
else
cout<<"\nRESULT:FAIL\t";
}
void main()
{
studentmark std;
clrscr();
cout<<"\nTo calculate student mark list\n";
std.getmarks();
std.calculate();
std.display();
OUTPUT:
15.EMPLOYEE SALARY USING CLASSES.

AIM:
write a c++ program to calculate employee salary using classes.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class emp
{
int bpay,id;
float hra,pa,pf,hra1,pa1,pf1,gs,ns;
char name[20];
public:
void getinfo();
void calculate();
void display();};
void emp::getinfo()
{
cout<<"enter the eb name&id:";
cin>>name>>id;
cout<<"enter the bpay,hra,pa,pf:";
cin>>bpay>>hra>>pa>>pf;}
void emp::calculate()
{
hra1=hra/100*bpay;
pa1=pa/100*bpay;
pf1=pf/100*bpay;
gs=hra1+pa1+pf1+bpay;
ns=gs-pf1;
}
OUTPUT:
cout<<"3.rectangle\n";
cout<<"4.exit\n";
cout<<"enter your choice(1-4):";
cin>>choice;
cout<<"\n";
switch(choice)
{
case 1:cout<<"enter three sides:";
cin>>s1>>s2>>s3;
a=calarea(s1,s2,s3);
break;
case 2:
cout<<"\n enter a side:";
cin>>s1;
a=calarea(s1);
cout<<"area="<<a;
break;
case 3:
cout<<"enter length and breadth:";
cin>>s1>>s2;
a=calarea(s1,s2);
cout<<"\n area="<<a;
break;
case 4:
cout<<"existing..press any key..";
getch();
exit(1);
default:
cout<<"wrong choice..!!";}
cout<<"\n";}
while(choice>0&&choice<=4);
getch();
}
OUTPUT:
}

else if((avg>60)&&(avg<75))

cout<<"grade A";

else

cout<<"destination";

else

cout<<"\nRESULT:FAIL\t";

void main()

int i,n;

clrscr();

cout<<"\n to calculate student mark list\n";

student s[10];

cout<<"enter the value for n";

cin>>n;
OUTPUT:
if(unit>100&&unit<=300)
{
cout<<"2 rupees per unit";
cout<<"unit is:"<<unit<<"\n";
amt=unit*2;
cout<<"rupees is"<<amt<<"\n";
}
else if(unit>300&&unit<=500)
{
cout<<"3 rupees per unit";
cout<<"unit is:"<<unit<<"\n";
amt=unit*3;
cout<<"rupees is"<<"\n"<<amt;
}
else if(unit>500&&unit<1000)
{
cout<<" 4 rupees per unit";
cout<<"unit is:"<<unit<<"\n";
amt=unit*4;
cout<<"rupees is"<<"\n"<<amt;
}
else if(unit>=1000)
{
cout<<" 6 rupees per unit";
cout<<"unit is:"<<unit<<"\n";
amt=unit*6;
cout<<"rupees is"<<"\n"<<amt;
}

else
{
cout<<"no bill for less than 100 units";
}
}
void main()
OUTPUT:
pf1=pf/100*bpay;
GS=hra1+pa1+pf1+bpay;
NS=GS-pf1;
}
void employee::display()
{
cout<<"\n id :"<<id;
cout<<"\nName :"<<name;
cout<<"\nBasic pay :"<<bpay;
cout<<"\nhra :"<<hra;
cout<<"\npa :"<<pa;
cout<<"\nPf :"<<pf;
cout<<"\n The Gross Salary is:"<<GS;
cout<<"\n The Net Salary is :"<<NS;
}
void main()
{
int i,n;
clrscr();
cout<<"\tEMPLOYEE PAYSLIP";
employee e[10];
cout<<"\n Enter the n value:";
cin>>n;
for(i=1;i<=n;i++)
{
e[i].getinfo();
}
for(i=1;i<=n;i++)
{
e[i].calc(); }
for(i=1;i<=n;i++)
{
e[i].display();
}
OUTPUT:
20.OBJECT AS ARGUMENTS.

AIM:
write a c++ program to calculate a hours & minutes in objects as
arguments.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class time
{
int hours,minutes;
public:
void gettime(int h,int m)
{
hours=h;
minutes=m;
}
void puttime(void)
{
cout<<hours<<"hours and";
cout<<minutes<<"minutes"<<"\n";
}
void sum(time,time);
};
void time::sum(time t1,time t2)
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
void main()
{
OUTPUT:
21.SUMOF VALUE USING FRIEND FUNCTION.

AIM:
write a c++ program to calculate a value using friend function.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class ABC;
class XYZ
{
int data1;
public:
void setvalue(int value)
{
data1=value;
}
friend void add(XYZ,ABC);
};
class ABC
{
int data2;
public:
void setvalue(int value)
{
data2=value;
}
friend void add(XYZ,ABC);
};
void add(XYZ obj1,ABC obj2)
{
cout<<"sum of data values of XYZ and ABC objects using friend
function="<<obj1.data1+obj2.data2;
}
OUTPUT:
22.OVERLOADED CONSTRUCTORS.

AIM:
write a c++ program to calculate a value using overloaded
constructors.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{}
complex(float a)
{
x=y=a;
}
complex(float real,float imag)
{
x=real;
y=imag;
}
friend complex sum(complex,complex);
friend void show(complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.y;
c3.y=c1.y+c2.y;
return (c3);
}
OUTPUT:
cout<<"\n\n name:"<<name;
cout<<"\n\t m1:"<<m1;
cout<<"\n\t m2:"<<m2;
cout<<"\n\t m3:"<<m3;
cout<<"\n\t m4:"<<m4;
cout<<"\n\t m5:"<<m5;
cout<<"\n\t tot:"<<tot;
cout<<"\n\t avg:"<<avg;
if((m1>=35)&&(m2>=35)&&(m3>=35)&&(m4>=35)&&(m5>=35))
{
cout<<"\n\nRESULT:PASS"<<"\n";
if(avg>=35&&avg<45)
{
cout<<"\ngrade D";
}
else if((avg>=45)&&(avg<=60))
{
cout<<"\ngrade C";
}
else if((avg>60)&&(avg<=90))
{
cout<<"\ngrade B";
}
else
{
cout<<"\ngrade A";
}
}
else
cout<<"\n\nRESULT:FAIL\t";
}
};
void main()
{
mark s;
OUTPUT:
24.MULTILEVEL INHERITANCE.

AIM:
write a c++ program to calculate a value using multilevel
inheritance.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class base
{
public:
int x;
void getdata()
{
cout<<"enter the value of x:";
cin>>x;
}
};
class der1:public base
{
public:
int y;
void getinfo()
{
cout<<"enter the value y:";
cin>>y;
}
};
class der2:public der1
{
private:
int z;
public:
OUTPUT:
25.MULTIPLE INHERITANCE.

AIM:
write a c++ program to calculate a value using multiple inheritance.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P:public M,public N
{
public:
void display(void);
};
void M::get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
OUTPUT:
void put_marks()
{
cout<<"Marks
obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"sports:"<<score<<"\n";

}
};

class result: public test, public sports


{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
OUTPUT:
cout<<"Part 1:"<<part1<<"\n";
cout<<"Part 2:"<<part2<<"\n";
}
};
class sports:public virtual student
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore()
{
cout<<"Sport wt:"<<score<<"\n";
}
};
class result:public text,public sports
{
float total;
public:
void display();
};
void result::display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"Total score:"<<total<<"\n";
}
void main()
{
result std;
OUTPUT:
28.VIRTUAL FUNCTION
AIM:
write a c++ program to calculate a value using virtual function.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n display base:";
}
virtual void show()
{
cout<<"\n show base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived";
}
void show()
{
cout<<"\n show derived";
}
};
void main()
{
OUTPUT:
29.0PERATOR OVERLOADING

AIM:
write a c++ program to display a value using operator overloading.

SOURCE CODE:
#include<iostream.h>

#include<conio.h>

class space

int x,y,z;

public:

void getdata(int a,int b,int c);

void display(void);

void operator-();

};

void space::getdata(int a,int b,int c){

x=a;

y=b;

z=c;}

void space::display(void){

cout<<"\nx="<<x<<"\n";
OUTPUT:
30.FACTORIAL USING INLINE FUNCTION

AIM:
write a c++ program to calculate a factorial value using inline
function.

SOURCE CODE
#include<iostream.h>

#include<conio.h>

inline int factorial(int n){

if(!n)

return 1;

else

return (n*factorial(n-1));}

void main()

int n;

clrscr();

cout<<"enter num:";

cin>>n;

cout<<"fact of n is"<<factorial(n);

getch();

}
OUTPUT:

You might also like