Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

Pal inline

#include<iostream>
#include<string>
using namespace std;
inline string p(string n,string t="")
{
for(int i=n.length()-1;i>=0;i--)
{
t+=n[i];
}
if(t==n)
{
return "s";

}
else
{
return "n";
}

}
int main()
{
string n,t="";
cin>>n;
cout<<p(n);

}
Reverse inline

#include<iostream>

#include<string>

using namespace std;

inline string p(string n,string t="")

for(int i=n.length()-1;i>=0;i--)

t+=n[i];

return t;

int main()

string n,t="";

cin>>n;

cout<<p(n);

}
Function over loading:

#include <iostream>
using namespace std;
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b, double c) {
return a + b + c;
}

int main() {
int a,b,c;
cin>>a;
cin>>b;
cin>>c;
int s=add(a,b,c);
cout << "Adding three integers: " << s<< endl;
double a1,b1,c1;
cin>>a1;
cin>>b1;
cin>>c1;
double su=add(a1,b1,c1);
cout << "Adding three doubles: " << su<< endl;

return 0;
}
class

#include <iostream>

using namespace std;

class Box {

public:

int length;

void setWidth(int w) {

width = w;

int getWidth() {

return width;

void print() {

cout << "Length: " << length << ", Width: " << width << endl;

private:

int width;

};
int main() {

Box box1;

box1.length = 10;

box1.setWidth(5);

cout << "Box 1:" << endl;

box1.print();

return 0;

2 class constructor
#include<iostream>

using namespace std;

class room

private:

int l,b;

public:

room (int le,int br)

l=le;

b=br;

}
int a()

return l*b;

};

class t

private:

int ba,h;

public:

t(int bas,int he)

ba=bas;

h=he;

int tr()

return 0.5*ba*h;

};

int main()

room r(5,2);

cout<<r.a()<<endl;

t ti(2,2);

cout<<ti.tr();

}
Constructor:
#include<iostream>

using namespace std;

class room

private:

int l,b;

public:

room (int le,int br)

l=le;

b=br;

int a()

return l*b;

};

int main()

room r(5,2);

cout<<r.a();

Class :
#include<iostream>

using namespace std;

class area

{
private:

int l,b,a;

public:

void setlb(int le,int br)

l=le;

b=br;

void seta(int ar)

a=ar;

int r()

return l*b;

int sq()

return a*a;

};

int main()

area a1;

a1.setlb(10,5);

a1.seta(4);

cout<<a1.r()<<endl;

cout<<a1.sq();

1.Palindrome
#include<iostream>

#include<string>

using namespace std;

int main()

string data,temp="";

int i;

cin>>data;

for(i=data.length()-1;i>=0;i--)

temp+=data[i];

if(data==temp)

cout<<"p";

else

cout<<"n";

Reverse

#include<iostream>

#include<string>
using namespace std;

int main()

string data,temp="";

cin>>data;

for(int i=data.length()-1;i>=0;i--)

temp+=data[i];

cout<<temp;

Skipp

#include<iostream>

#include<string>

using namespace std;

int main()

int a,b,c;

cin>>a;

cin>>b;

cin>>c;

while(a<=b)

if(a==c)

{
a++;

continue;

cout<<a;

a++;

Factors

#include<iostream>

#include<string>

using namespace std;

int main()

int n,sum=0,i=1;

cin>>n;

while(i<=n)

if(n%i==0)

cout<<i<<endl;

i++;

}
}

Perfect sq range

#include<iostream>

using namespace std;

int main()

int x,y,n,rem,rev,temp;

cin>>x;

cin>>y;

for(int i=x;i<=y;i++)

n=i*i;

temp=n;

rev=0;

while(n!=0)

rem=n%10;

rev+=rem;

n=n/10;

if(rev<10)

{
cout<<temp<<endl;

Sub over loading

#include<iostream>

using namespace std;

int sub(int a,int b)

return a-b;

int main()

int a,b;

cin>>a;

cin>>b;

cout<<sub(a,b);

Class
#include<iostream>

using namespace std;

class box

private:

int w;

public:

int l;

void setw(int wi)

w=wi;

int getw()

return w;

int area()

return l*w;

};

int main()

box b;

b.l=5;

b.setw(60);

cout<<b.getw()<<endl;

cout<<b.area();

}
Compo range

#include<iostream>

using namespace std;

int main()

int a,b,i,j;

cin>>a;

cin>>b;

for(i=a+1;i<=b;i++)

int c=0;

for(j=1;j<=b;j++)

if(i%j==0)

c++;

if(c>2)

cout<<i;

}
}

Prime

#include<iostream>

using namespace std;

int main()

int a,b,i,j;

cin>>a;

cin>>b;

for(i=a+1;i<=b;i++)

int c=0;

for(j=1;j<=b;j++)

if(i%j==0)

c++;

if(c==2)
{

cout<<i<<endl;

Leap
#include<iostream>

using namespace std;

int main()

int n;

cin>>n;

if((n%4==0 && n%100!=0)||(n%400==0))

cout<<"le";

else

cout<<"no";

}
No:of factors

#include<iostream>

using namespace std;

int main()

int n,i=1,f=0;

cin>>n;

while(i<=n)

if(n%i==0)

f++;

i++;

cout<<f;

Pattern
#include<iostream>

using namespace std;

int main()

int n,k=1,f=0;

cin>>n;

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

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

cout<<k*k<<" ";

k++;

cout<<endl;

Op:

49

16 25 36

49 64 81 100

Using class floyds:


#include<iostream>

using namespace std;


class fl
{
private:
int n;
public:
fl(int num)
{
n=num;
}
void pattern(){
int k=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
cout<<k<<" ";
k++;
}
cout<<endl;
}
}

};
int main()
{
int n;
cin>>n;
fl f(n);
f.pattern()

Gcd:
#include<iostream>

using namespace std;

int main()

{
int a,b,gcd=0,lcm;

cin>>a;

cin>>b;

for(int i=1;i<=a&&i<=b;i++)

if(a%i==0&&b%i==0)

gcd=i;

lcm=(a*b)/gcd;

cout<<"gcd"<<gcd<<endl;

cout<<lcm;

# steps:

#include<iostream>

using namespace std;

int main()

int n,f=0;

cin>>n;

while(n>0){

if(n%2==0)

n=n/2;

}
else

n=n-1;

f==f+1;

cout<<f;

Class avg

#include<iostream>

using namespace std;

class marks

private :

int a1,a2,a3,a4,a5;

public:

void setm(int a11,int a22,int a33,int a44,int a55)

a1=a11;

a2=a22;

a3=a33;

a4=a44;

a5=a55;

int t()

return a1+a2+a3+a4+a5;

}
int avg()

return static_cast<int>(t()) / 5;

};

int main()

marks ma;

ma.setm(10,50,50,100,100);

cout<<ma.t()<<endl;

cout<<ma.avg()<<endl;

if(ma.avg()>=90)

cout<<"A";

else if(ma.avg()>=80&&ma.avg()<90)

cout<<"b";

else

cout<<"f";

Or

#include<iostream>

#include<string>

using namespace std;

class marks
{

private :

int a1,a2,a3,a4,a5;

string name;

public:

void setm(string n,int a11,int a22,int a33,int a44,int a55)

name=n;

a1=a11;

a2=a22;

a3=a33;

a4=a44;

a5=a55;

string getname()

return name;

int t()

return a1+a2+a3+a4+a5;

int avg()

return static_cast<int>(t()) / 5;

};

int main()

marks ma;
ma.setm("bhavz",10,50,50,100,100);

cout<<ma.t()<<endl;

cout<<ma.avg()<<endl;

cout<<ma.getname();

if(ma.avg()>=90)

cout<<"A";

else if(ma.avg()>=80&&ma.avg()<90)

cout<<"b";

else

cout<<"f";

Matrix mul:
#include<iostream>

using namespace std;

int main()

int a[2][2];

int b[2][2];

int c[2][2];

for(int i=0;i<2;i++)

for(int j=0;j<2;j++)
{

cin>>a[i][j];

for(int i=0;i<2;i++)

for(int j=0;j<2;j++)

cin>>b[i][j];

for(int i=0;i<2;i++)

for(int j=0;j<2;j++)

c[i][j]=0;

for(int k=0;k<2;k++)

c[i][j]+=a[i][k]*b[k][j];

cout<<endl;

for(int i=0;i<2;i++)

for(int j=0;j<2;j++)

cout<<c[i][j]<<endl;

}
Array c or p count:

#include<iostream>````

using namespace std;

int main()

int a[]={1,2,3,4,5,6,7,8,9,10};

int c=0,p=0;

int length = sizeof(a) / sizeof(a[0]);

for(int i=0;i<length;i++)

int co=0;

for(int j=1;j<=a[i];j++)

if(a[i]%j==0)

co++;

if(co>2)

c++;

else if(co==2)

p++;

}
cout<<c<<endl;

cout<<"P"<<p;

You might also like