0% found this document useful (0 votes)
30 views19 pages

Short Program Solution

The document contains 10 code snippets demonstrating different C++ concepts like classes, objects, inheritance, polymorphism etc. Each snippet contains a short program to find palindrome, Fibonacci series, prime numbers, Armstrong numbers etc.

Uploaded by

Shaunak Sardesai
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)
30 views19 pages

Short Program Solution

The document contains 10 code snippets demonstrating different C++ concepts like classes, objects, inheritance, polymorphism etc. Each snippet contains a short program to find palindrome, Fibonacci series, prime numbers, Armstrong numbers etc.

Uploaded by

Shaunak Sardesai
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/ 19

SHORT PROGRAM SOLUTION

1) #include<iostream>
using namespace std;
class NUMBER
{
unsigned short int num;
int input();
public:
void display();

};
int NUMBER::input()
{
int s,sum=0;
cout<<"Enter a number";
cin>>num;
s=num;
while(s>0)
{
sum=sum*10+(s%10);
s=s/10;
}
if(sum==num)
return 1;
else
return 0;
}
void NUMBER::display()
{
if(input()==1)
cout<<endl<<"Number is Palindrome";
else
cout<<endl<<"Number is not Palindrome";
}
int main()
{
NUMBER a;
a.display();
return 0;
}
2) #include<iostream>
using namespace std;
class FIBO
{
unsigned short int n;
int input();
public:
void display();

};
int FIBO::input()
{

cout<<"Enter the number of terms";


cin>>n;

}
void FIBO::display()
{
input();
int f1=0,f2=1,f3;
cout<<endl<<"Fibonacci series:";
cout<<f1<<" "<<f2<<" ";
for(int i=3;i<=n;i++)
{
f3=f1+f2;
cout<<f3<<" ";
f1=f2;
f2=f3;
}
}
int main()
{
FIBO a;
a.display();
return 0;
}

3) #include<iostream>
using namespace std;
class PRIME
{
short unsigned int num;
void process();
public:
void get_no();
};
void PRIME :: get_no()
{
cout<<"Enter the number"<<endl;
cin>>num;
process( );
}
void PRIME :: process( )
{
int f=0;
if ( num ==0)
cout<< "num is neither prime nor composite"<<endl;
else
{
for(int i=2; i< num; i++)
{
if ( num% i == 0)
{
f=1;
break;
}
}
}
if (f == 0)
cout<< num <<"is prime number"<<endl;
else
cout<<num<<"is composite number"<<endl;
}
int main( )
{
PRIME p;
p. get_no();
return 0;
}
4) #include<iostream>
using namespace std;
class ARMSTRONG
{
short unsigned int num;
void process()
{
short unsigned int x,sum=0,digit;
x=num;
while(x!=0)
{
digit=x%10;
sum=sum+digit*digit*digit;
x=x/10;
}
if(sum==num)
cout<<"\nGiven number "<<num<<" is a Armstrong number";
else
cout<<"\nGiven number "<<num<<" is not a Armstrong number";

public:
void get_data()
{
cout<<"\nEnter a three digit integer number";
cin>>num;
process();
}
};
main()
{

cout<<"\nThis program checks if a given number is a Armstrong number or not";


ARMSTRONG A1;
A1.get_data();
return 0;
}
5) #include<iostream>
#include<math.h>
using namespace std;
class SERIES
{
unsigned short int N;
float D;
void process()
{
unsigned short int C=1;
float sum=0.0,term,x;
x=3.14/180*D;
term=x;
for(int i=2;i<=2*N;i+=2)
{
sum =sum+term;
term=-term*x*x/(i*(i+1));
}
cout<<"\n The value of sum of series is "<<sum;
cout<<"\n The value of sin of angle is"<<sin(x);

}
public:
void get_data()
{
cout<<"\nEnter the angle in degrees";
cin>>D;
cout<<"\nEnter the number of terms in series";
cin>>N;
process();
}
};
main()
{

cout<<"\nThis program finds the sum of the sin series";


SERIES S1;
S1.get_data();
return 0;
}

6) #include<iostream>
using namespace std;
class SMALLER
{
double d1,d2,d3;
void process( );
public:
SMALLER( double, double, double);
};
void SMALLER::process()
{
double s =d1;
if ( d2< s)
s=d2;
if( d3 < s)
s = d3;
cout<<"Smaller = "<< s<<endl;
}
SMALLER :: SMALLER( double a, double b, double c)
{
d1=a; d2=b; d3=c;
process( );
}

int main()
{
double x, y,z;
cout<<"enter the 3 nos"<<endl;
cin>> x>>y>>z;
SMALLER s( x, y, z);
return 0;
}

7) #include<iostream>
using namespace std;
class BASE
{
private:
float num1;
public:
float num2;
void input_data();
float get_num1();
};
void BASE::input_data()
{
cout<<"Enter num1";
cin>>num1;
}
float BASE::get_num1()
{
return num1;
}
class DERIVED:public BASE
{
private:
float sum;
public:
void get_data();
void show_data();
};
void DERIVED::get_data()
{
cout<<endl<<"Enter num2";
cin>>num2;
sum=get_num1()+num2;
}
void DERIVED::show_data()
{
cout<<endl<<"Num1="<<get_num1();
cout<<endl<<"Num2="<<num2;
cout<<endl<<"sum="<<sum;
}
int main()
{
DERIVED d;
d.input_data();
d.get_data();
d.show_data();
return 0;
}

8) #include <iostream>
using namespace std;
class base1
{
int a;
protected: int get_a(){return a;}
public:base1(int p) {a=p;}
};
class base2
{
int b;
public:base2(int q){b=q;}
int get_b(){return b;}
};

class derived:public base1,protected base2


{
int z;
void show(){ cout<<"Product :"<<z;}
public:derived(int i, int j):base1(i),base2(j)
{ z=get_a()*get_b(); show();}
};

int main()
{
derived x(10,20);
return 0;
}
9) #include<iostream>
using namespace std;
class TIME
{
int hours,minutes;
public:TIME(){ }
TIME(int h,int m){hours=h; minutes=m;}
void sum(TIME &p,TIME &q)
{
hours=p.hours+q.hours+(p.minutes+q.minutes)/60;
minutes=(p.minutes+q.minutes)%60;
}
void display()
{ cout<<endl<<hours<<" "<<minutes; }
};

int main()
{
TIME a(5,40),b(2,30),c;
c.sum(a,b);
a.display();
b.display();
c.display();
return 0;
}

10) #include<iostream>
using namespace std;
class VOLUME
{
float r,r1,h;
float v;
public:
VOLUME(int R);
VOLUME(int R,int H);
void display();

};
VOLUME::VOLUME(int R)
{
r=R;
v=4.00/3.00*3.14*r*r*r;

}
VOLUME::VOLUME(int R,int H)
{
r1=R;h=H;
v=3.14*r1*r1*h;

}
void VOLUME::display()
{
cout<<v;
}
int main()
{
int r,r1,h;
cout<<endl<<"Enter radius to find volume of sphere";
cin>>r;
cout<<endl<<"Enter radius and height to find volume of cylinder";
cin>>r1>>h;
VOLUME V1(r);
VOLUME V2(r1,h);
cout<<endl<<"Volume of Sphere=";V1.display();
cout<<endl<<"Volume of Cylinder=";V2.display();
return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy