Skip to content

OODP ELAB SOLUTIONS – All OODP ELAB SOLUTIONS WITH CODES in 2023 Part 1

    srmnotesadda

    Table of Contents

    IO Operations

     

    Level 1Challenge 1

    Arav and Aaron are participating

     

    #include <iostream>

    using namespace std; int main()

    {

    int aravspeed,aaronspeed,speeddiff; cin>>aravspeed>>aaronspeed; if(aravspeed>aaronspeed)

    {

    speeddiff=aravspeed-aaronspeed;

    }

    else

    {speeddiff=aaronspeed-aravspeed;} cout<<speeddiff;

    return 0;

    }

    Level 1Challenge 2 Ramesh is working #include <iostream> using namespace std; int main()

    {

    int alvqntoffood,messcnt,dividedqnt,remfood; cin>>alvqntoffood>>messcnt; dividedqnt=alvqntoffood/messcnt; remfood=alvqntoffood%messcnt; cout<<dividedqnt<<" ";

    cout<<remfood;

    return 0;

     

    }

    Level 1Challenge 3

    Three brothers

    #include <iostream> using namespace std; int main()

    {

    int bro1,bro2,bro3; cin>>bro1>>bro2>>bro3; if(bro1>=bro2&&bro1>=bro3) cout <<bro1;

    else if(bro2>=bro1&&bro2>=bro3) cout <<bro2;

    else

    cout<<bro3;

    return 0;

    }

     

     

    Level 1Challenge 4

    A little lion king


    #include <iostream>

    using namespace std; int main()

    { int t;

    cin>>t; while(t–){

    int N,C; cin>>N>>C; int d=0; while(N–){

    int a; cin>>a; d+=a;

     

    }

    if(C>=d){ cout<<"Yes"<<endl;

    }else{

    cout<<"No"<<endl;

    }

    }

    return 0;

    }

    Level 1Challenge 5

    In congo

    #include <iostream> using namespace std; int main()

    {

    int ageofcitizen; cin>>ageofcitizen;

    if(ageofcitizen>18&&ageofcitizen<70)

    {cout<<"Eligible for Voting";} else

    {cout<<"Not Eligible for Voting";} return 0;

    }

    Level 1Challenge 6 Dhoni's daughter #include <iostream> using namespace std; int main()

    {

    float weightinmoon; int weightinearth; cin>>weightinearth;

     

    weightinmoon=(16.6*weightinearth)/100; cout<<weightinmoon;

    return 0;

    }

    Level 1Challenge 7

    Omkar the professor

    #include <iostream> using namespace std; int main()

    {

    int M,initialtemp,finaltemp; float Q;

    cin>>M>>initialtemp>>finaltemp; Q=(M*(finaltemp-initialtemp))*4184; cout<<""<<Q;

    return 0;

    }

    Level 1Challenge 8

    Professor JD

    #include <iostream> #include<math.h> #include<iomanip> using namespace std; int main()

    {

    float b,leftside,rs1,rs2; cin>>b>>leftside; rs1=sqrt(pow(leftside,2)-pow(b,2)); rs2=sqrt(pow(leftside,2)+pow(b,2));

    cout<<fixed<<setprecision(5)<<rs1<<" "<<rs2; return 0;

    }

     

    Level 1Challenge 9

    Binita was travelling

    #include <iostream> using namespace std; int main()

    {

    int tot_mins,hrs,mins; cin>>tot_mins;; hrs=(tot_mins/60); mins=(tot_mins-(60*hrs));

    cout<<hrs<<" Hours and "<<mins<<" Minutes"; return 0;

    }

    Level 1Challenge 10

    Laaysa with her friends

    #include <iostream> using namespace std; int main()

    {

    int N,i,j; cin>>N; for(i=0;i<N;i++){

    for(j=0;j<=i;j++){ if((i+1)%2==0)

    cout<<2*j+2<<" "; else

    cout<<2*j+1<<" ";

    }cout<<"\n";

    }

    return 0;

    }

     

    Level 2Challenge 1

    Tina, is a little girl

    #include <iostream> using namespace std; int A[100][100],n,m;

    int small(int x, int y)

    {

    if (x < y) return(x); return(y);

    }

    int g(int i, int j)

    {

    int term1,term2;

    if (i == 0) term1=0;

    else term1=small(A[i-1][j],A[i][j]); if (j == 0) term2=0;

    else term2=small(A[i][j-1],A[i][j]); return(2*(term1+term2));

    }

    int main()

    {

    int i,j,price; cin>>n>>m;

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

    {

    for (j = 0; j < m; ++j) cin>>A[i][j];

    }

    price=0;

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

    {

    for (j=0;j<m;++j)

    {

     

    price+=4*A[i][j]+2; price-=g(i,j);

    }

    }

    cout<<price;

    return 0;

    }

    Level 2Challenge 2

    Venkatesh raja

    #include <iostream> using namespace std; int A[10][10];

    int main()

    {

    int i,j,n; cin>>j; while(j–) {

    cin>>n; for(i=0;i<n*n;i++)

    {

    cin>>A[i/n][i%n];

    }

    for(i=0;i<n*n;i++) { cout<<""<<A[n-i%n-1][i/n]<<" ";

    }

    cout<<"\n";

    }

    cin>>A[i][j];

    return 0;

    }

    Level 2Challenge 3

    Roy wants to change his

     

    #include <iostream> using namespace std; int main()

    {

    int l,w,h;

    cin>>l; cin>>w>>h; if(w<l || h<l)

    cout<<"UPLOAD ANOTHER";

    else if(w==h) cout<<"ACCEPTED";

    else cout<<"CROP IT";

    cout<<endl; return 0;

    }

    Level 2Challenge 4 The alien festival #include <iostream> using namespace std; int main()

    {

    char report[501]; int test,i,n; cin>>test; while(test–){

    int count=0; cin>>n; cin>>report; for(i=0;i<n;i++)

    {

    if(report[i]=='H')count++;

     

    if(report[i]=='T')count–; if(count<0||count>1)

    {

    break;

    }

    }

    if(count==0) cout<<"Valid\n"; else cout<<"Invalid\n";

    }

    return 0;

    }

    Level 2Challenge 5

    Malina has an

    #include<iostream> #include<bits/stdc++.h> using namespace std; int main()

    {

    int t; cin>>t; while(t–)

    {

    int N,A;

    char s[10001]; cin>>s;

    A=0;

    N=strlen(s); for(int i=0;i<N;i++)

    {

    if(s[i]>='0' && s[i]<='9')

     

    A+=(s[i]-'0');

    }

    cout<<A<<endl;

    }

    return 0;

    }

    Level 2Challenge 6

    2022 was approaching

    #include <iostream> using namespace std; int main()

    {

    int n,k,weapons; cin>>n>>k; weapons=k/n; cout<<weapons;

    return 0;

    }

    Level 2Challenge 7

    A team from

    #include <iostream>

    using namespace std; int main()

    {int people_age,weight; cin>>people_age>>weight; if(people_age>=18&&weight>=40) cout<<"Eligible for Donation";

    else

    cout<<"Not Eligible for Donation"; return 0;

    }

    Level 2Challenge 8

     

    Mr. issac the head

    #include <iostream> #include<bits/stdc++.h> using namespace std; int main()

    {float celsius, fahrenheit; cin>>fahrenheit; celsius=(fahrenheit-32.0)*(5.0/9.0);

    cout<<fixed<<setprecision(2)<<celsius<<" Centigrade\n"; if(celsius>=150)

    cout<<"Very Hot"; else if(celsius>=100) cout<<"Hot";

    else cout<<"Moderate";

    return 0;

    }

    Level 2Challenge 9

    Yesterday loki

    #include <iostream> using namespace std; int main()

    {

    int n,k; cin>>n>>k; if(n==k) cout<<"YES"; else cout<<"NO";

    return 0;

    }

    Level 2Challenge 10

     

    Mr. Shahrukh has given

    #include <iostream> #include <cstring> using namespace std; int main()

    {

    char S[1000000];

    int i,w,count=0; cin>>S; w=strlen(S); for(i=0;i<w;i++){

    if(S[i]==S[i-1]){

    continue;

    }

    else{

    count++;

    }

    }

    cout<<count;

    return 0;

    }

    Level 3Challenge 1

    There are k nuclear

    #include <iostream> using namespace std; int main()

    {

    int n,b,s,i,cham; cin>>n>>b>>s; int K[1000] = {0};

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

     

    { cham=0; K[cham]++; while(K[cham]>b)

    { K[cham+1]++; K[cham]=0; cham++;

    } }

    for(i=0;i<s;i++) cout<<K[i]<<" ";

    return 0;

    }

    Level 3Challenge 2

    Raju is a tester

    #include <iostream> #include<string.h> using namespace std; int main()

    {

    int test,i,len,top,ans; char para[100000]; char stack[5000]; cin>>test;

    while (test–)

    {

    cin>>para; len=strlen(para); top=-1;

    ans=1; for(i=0;i<len;i++)

    {

    if ((para[i]=='{')||(para[i]=='[')||(para[i]=='(')) stack[++top]=para[i];

     

    else if(((para[i]=='}')&&(stack[top]=='{'))||((para[i]==']')&&(stack[top]=='['))||((para[i]==')')&&(stack[to p]=='(')))

    top–;

    else {ans=0; break;}

    }

    if (ans && top) cout<<"Balanced"<<endl; else

    cout<<"Not Balanced"<<endl;

    }

    return 0;

    }

    Level 3Challenge 3

    Binita always

    #include <iostream> #include<iomanip> using namespace std; int main()

    {

    float height,bmi; int weight; cin>>weight; cin>>height;

    bmi = (float)weight/(height*height); cout<<fixed<<setprecision(2)<<bmi;

    return 0;

    }

    Level 3Challenge 4

    Nathan was

    #include <iostream>

    using namespace std; int main()

    {

     

    int days; cin>>days; switch(days){

    case 1: cout<<"Azure"; break;

    case 2: cout<<"Beige"; break;

    case 3: cout<<"Brick Red"; break;

    case 4: cout<<"Champagne"; break;

    case 5: cout<<"Desert sand"; break;

    case 6: cout<<"Ivory"; break;

    case 7: cout<<"Pear"; break; default:

    cout<<"Invalid Day";

    }

    return 0;

    }

     

    Level 3Challenge 5


    Yasir was making a kite

     

    #include <iostream>

     

    #include<iomanip> #include<cmath> using namespace std; int main()

    {

    float s1,s2,s3,s,area; cin>>s1>>s2>>s3; s=(s1+s2+s3)/2;

    area=sqrt(s*(s-s1)*(s-s2)*(s-s3)); cout<<fixed<<setprecision(2)<<area;

    return 0;

    }

    Level 3Challenge 6

    Colonel sanders

    #include <iostream> #include <cstring> using namespace std; int main()

    {

    ios_base::sync_with_stdio(NULL); cin.tie(NULL);

    cout.tie(NULL); int t;

    cin>>t; while(t–)

    {

    int count=0; char N[100001];

    cin>>N;

    int n=strlen(N); for(int i=0;i<n;i++)

    {

     

    if((N[i]=='S' and N[i+1]=='C') || (N[i]=='S' and N[i+1]=='E'))

    count++;

    else if(N[i]=='E' and N[i+1]=='C') count++;

    else continue;

    }

    if(count==0) cout<<"yes"<<"\n"; else cout<<"no"<<"\n";

    }

    return 0;

    }

    Level 3Challenge 7

    Today is jack's

    #include <iostream> using namespace std; int main()

    {

    char S[100]; int t,i,r,u,d,n; int l;

    cin>>t; while(t–)

    {

    int H[100]={};

    cin>>n; cin>>S; for(i=0;i<n;i++)

    {

    if(S[i]=='R'&&S[i-1]!='L'&&S[i-1]!='R')

     

    H[S[i]-65]++;

    else if(S[i]=='L'&&S[i-1]!='R'&&S[i-1]!='L') H[S[i]-65]++;

    if(S[i]=='U'&&S[i-1]!='U'&&S[i-1]!='D') H[S[i]-65]++;

    if(S[i]=='D'&&S[i-1]!='U') H[S[i]-65]++;

    }

    l=H[76-65];

    r=H[82-65];

    u=H[85-65];

    d=H[68-65];

    cout<<"\n"<<r-l<<" "<<u-d;

    }

    return 0;

    }

    Level 3Challenge 8

    Simon loves to listen to

    #include <iostream> using namespace std; int main()

    {

    int L,D; int t,n;

    cin>>L>>D; t=D/0.5; if(t%L==0) n=t/L;

    else n=1+t/L; cout<<n;

    return 0;

     

    }

    Level 3Challenge 9

    Nirobi have

    #include <iostream>

    using namespace std; int main()

    {

    int m,n,test,i,j,sum,m1,n1,m2,n2; cin>>test;

    while(test–)

    {

    cin>>m>>n; int C[m][n];

    for(i=0;i<m;i++) for(j=0;j<n;j++) cin>>C[i][j];

    cin>>m1>>n1>>m2>>n2; sum =0;

    for(i=m1-1;i<m2;i++) for(j=n1-1;j<n2;j++) sum +=C[i][j]; cout<<sum<<endl;

    }

    return 0;

    }

    Level 3Challenge 10

    Selvan is one

    #include <iostream> using namespace std; int main()

    {

    int workalloid;

     

    cin>>workalloid; switch(workalloid){

    case 101: cout<<"Cinematographer"; break;

    case 201: cout<<"Editor"; break;

    case 301:

    cout<<"Marketing Manager"; break;

    case 401:

    cout<<"Content Engineer"; break;

    case 501:

    cout<<"Editorial Assistant"; break;

    }

    return 0;

    }

     

    Classes,Methods & Constructors

     

    Level 1Challenge 1

    To celebrate reunion of 96

    #include <iostream> using namespace std; class Drinks

    {

    int n,a,b,c,i,j,rem,ans; public:void Shop()

    { cin>>n>>a>>b>>c; for (i = 0; i <= b; ++i)

    {

    for (j = 0; j <= c; ++j)

    {

    rem = n-i-j-j;

    ans += (rem >= 0 && rem * 2 <= a);

    }

    }

    cout << ans;

    }

    };

    int main()

    {

    Drinks Buy; Buy.Shop(); return 0;

    }

    Level 1Challenge 2

    Tamil nadu land registration

    #include <iostream>

    using namespace std; class house

     

    { public:

    int hno,len[10],brd[10],hig[10],no_rooms; char name[100];

    char cty[100]; char state[100]; int room[10]; void input(); void display();

    };

    void house::input()

    {

    cin>>name; cin>>hno>>cty>>state; cin>>no_rooms;

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

    {

    cin>>len[i]>>brd[i]>>hig[i];} void display();

    }

    void house::display()

    {

    cout<<"House name="<<name<<endl; cout<<"House No="<<hno<<endl; cout<<"City="<<cty<<endl; cout<<"State="<<state<<endl;

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

    {

    cout<<"Detail of Room "<<i+1<<endl; cout<<"Length="<<len[i]<<endl; cout<<"Breadth="<<brd[i]<<endl; cout<<"Height="<<hig[i]<<endl;

    }

     

    }

    int main()

    {

    house h; h.input();

    h.display();

    return 0;

    }

    Level 1Challenge 3

    Rahul and Kuldeep

    #include <iostream> using namespace std; class Complex

    {

    public:

    int r1,i1,r2,i2,r,i; Complex(){cin>>r1>>i1;cin>>r2>>i2;} void addcomplex()

    {

    r=r1+r2; i=i1+i2;

    }

    void displaycomplex()

    {

    cout<<r1<<"+"<<i1<<"i"; cout<<"\n"<<r2<<"+"<<i2<<"i"; cout<<"\n"<<r<<"+"<<i<<"i";

    }

    };

    int main() {

    Complex calculate;

     

    calculate.addcomplex(); calculate.displaycomplex(); return 0;

    }

    Level 1Challenge 6

    Tamilnadu educational

    #include <iostream> using namespace std; class library{

    public:

    string stud; int roll,co;

    library(int r,string n,int code){ roll=r;

    co=code; stud=n;}

    void display(){

    cout<<"Roll No:"<<roll<<"\n";

    cout<<"Name of the Student:"<<stud<<"\n"; cout<<"Code of Book Accessed:"<<co<<"\n";

    }

    };

    int main()

    {    int r1,r2,c1,c2; string n1,n2; cin>>r1>>n1>>c1; cin>>r2>>n2>>c2; library lib1(r1,n1,c1); library lib2(r2,n2,c2); lib1.display(); lib2.display();

    return 0;

     

    }

    Level 1Challenge 7

    Abhilash wants to

    #include <iostream> using namespace std; class Bank

    {

    public: void totalMoney(int n)

    {

    int a = 0; int b = n/7;

    int c = n%7; a+=b*28+b*(b-1)*7/2; a+=(c*(c+1)/2)+b*c; cout<<a;

    }

    };

    int main()

    {

    int n;

    Bank CalculateMoney; cin>>n;

    CalculateMoney.totalMoney(n); return 0;

    }

    Level 1Challenge 9

    Rajesh is running

    #include <iostream> using namespace std;

    class CheckTriangle

    {

    public:

     

    int s1,s2,s3;

    void readCoordinates() { cin>>s1>>s2>>s3; }

    void isosceles()

    {

    if((s1==s2) || (s1==s3) || (s2==s3))

    {

    cout<<"Can Form a ISOSCELES Triangle";

    }

    else { cout<<"Cant Form a ISOSCELES Triangle"; }

    }

    };

    int main()

    {

    CheckTriangle consruct; consruct.readCoordinates(); consruct.isosceles();

    return 0;

    }

    Level 1Challenge 9

    RBI

    #include <iostream> #include<string.h> using namespace std; class Bank

    { private:

    char name[50];

    char accounttype[50]; int acc;

    double balance; public:

    void initial()

     

    { std::cin>>name>>acc>>accounttype>>balance; } void deposit()

    { float deposit; cin>>deposit; balance+=deposit; } void withdraw()

    { float withdraw; cin>>withdraw;

    if(withdraw>balance){ cout<<"Insufficient Balance\n";} else balance-=withdraw; }

    void disp()

    {

    cout<<"NAME="<<name<<"\nACCNO="<<acc<<"\nTYPE="<<accounttype<<"\nBALANCE AMOUNT="<<balance<<endl; }

    };

    int main(){

    Bank obj; obj.initial(); obj.deposit(); obj.withdraw(); obj.disp(); return 0;

    }

    Level 1Challenge 10

    Rohini an Gate

    #include <iostream> using namespace std; class ChangeBase

    {

    public:

    void sumBase(int n,int k)

     

    {

    int res = 0;

    while (n > 0) { res += (n % k); n /= k;

    }

    cout<<res;

    }

    };

    int main()

    {int n,k;

    ChangeBase Convert; cin>>n>>k; Convert.sumBase(n,k);

    return 0;

    }

    Level 2Challenge 1

    Richie street

    #include <iostream> using namespace std;

    long long n,mini=1000,maxi=0,val,i=0; class Shop

    {

    public:int Breakin(long long n)

    {

    cout<<maxi-mini-n+1; return 0;

    }

    };

    int main()

     

    {

    Shop HardDisks; cin>>n; for(;i++<n;)

    { cin>>val;

    if(maxi<val) maxi=val; if(mini>val) mini=val; }

    HardDisks.Breakin(n); return 0;

    }

    Level 2Challenge 2

    Fahad and rohit

    #include <iostream>

    #define s (D>=T) | (D<=S) using namespace std; class Pitching{

    public: int Throwing(int V,int T,int S,int D){ if(D==30) cout<<"No";

    else {

    if(s) cout<<"Yes"; else cout<<"No";}

    return 0;

    }

    };

    int main()

    {

    int V,T,S,D;

    Pitching Ball; cin>>V>>T>>S>>D;

    Ball.Throwing(V,T,S,D); return 0;

    }

     

    Level 2Challenge 4

    Rohan have

    #include <iostream>

    using namespace std; #define v if(s[i-1][j-1]=='#') void ss() {}

    class Colouring{

    public:int Squares(int h,int w){ string s[h];

    for(int i=0; i<h; i++)cin>>s[i]; int sum=0;

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

    int cnt=0; v

    cnt++;

    if(s[i-1][j]=='#')

    cnt++; if(s[i][j-1]=='#')

    cnt++; if(s[i][j]=='#') cnt++;

    if(cnt==1||cnt==3) sum++;}}

    cout<<sum<<endl; return 0;}

    };

    int main(){ Colouring task; int h,w; cin>>h>>w;

    task.Squares(h,w);

     

    return 0;}

    Level 2Challenge 5

    BPL is one

    #include <iostream> #include <algorithm> using namespace std; class Model{

    public:int Reduction(int a,int b,int x,int y){ int g;

    g= gcd(x,y); x=x/g; y=y/g;

    a=min(a/x,b/y); cout<<a*x<<' '<<a*y; return 0;

    }

    };

    int main()

    {

    Model parametercheck; int a,b,x,y; cin>>a>>b>>x>>y;

    parametercheck.Reduction(a,b,x,y); return 0;

    }

    Level 2Challenge 6

    To make a paper

    #include <iostream> using namespace std; int k,n,s,p,nspp;

    class Airplanes

    {

     

    public:int Packs(int k,int n,int s,int p)

    {

    nspp = (n%s!=0) + n/s;

    return (nspp*k%p!=0) + nspp*k/p;

    }};

    int main()

    {

    Airplanes Buying; cin>>k>>n>>s>>p; cout<<Buying.Packs(k,n,s,p);

    return 0;

    }

    Level 2Challenge 7

    There are n benches

    #include <iostream> using namespace std; class Relaxing{

    public: int s=0,mx=0,x; public:int Bench(int n,int m){

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

    {cin>>x;s+=x;mx=max(x,mx);} cout<<max(mx,(s+m-1)/n+1)<<" "<<mx+m; return 0;

    }

    };

    int main()

    {   Relaxing Sit; int n,m; cin>>n>>m; Sit.Bench(n,m);

    return 0;

    }

     

     

    Level 2Challenge 10

    Lokesh is a traveler

    #include <iostream> using namespace std; int index, n, count=0; void aim() {}

    class ContactNumbers

    {

    char a; public:int Phone(int n)

    {while(n–)

    { cin>>a; cout<<a; if(!count) count++; else if(n>1)

    {cout<<"-";

    count–;}

    } return 0; }}; int main()

    { cin>>n;

    ContactNumbers Digits; Digits.Phone(n);

    }

    Level 3Challenge 1

    Zaheer's telephone

    #include <iostream> using namespace std; class PhoneGalery{

    public:int Photos(int n,int a,int b,int t){ int z,c[1000],ans;

    char k;

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

    cin>>k,z += c[i] = c[n+i] = (k=='w')*b + 1;

     

    z-=c[0];

    int l=1,r=n; while(l<=n and r<2*n)

    {

    z+=c[r++];

    while(r-l>n or z+(r-l-1+min(n-l,r-n-1))*a > t) z-=c[l++]; if(l>n) break;

    ans = max(ans,r-l);

    }

    }

    };

    int main()

    {

    PhoneGalery view; int n,a,b,t;

    }

    printf("%d",ans); return 0;

     

    cin>>n>>a>>b>>t; view.Photos(n,a,b,t); return 0;

    Level 3Challenge 4

    Rakesh is a regular

    #include <iostream> using namespace std; class Investment{

    public:int Money(int n,int m,int k){ n–;

    int b,s = 0; cin >> b; while(n–)

    {

     

    int x;

    cin >> x;

    b = min(b, x);

    }

    while(m–)

    {

    int x;

    cin >> x; s=max(s, x);

    }

    cout<<max(0,(k/b)*(s-b))+k; return 0;}};

    int main()

    {

    int n,m,k; cin>>n>>m>>k; Investment stock; stock.Money(n,m,k);

    return 0;

    }

    Level 3Challenge 8


    Soman received

    #include <iostream> using namespace std; class Delivery{

    public:int Train(int n,int m){ int a,b,dis;

    int f[50000];

    int h[50000]={0};

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

    cin>>a>>b; f[a]++;

     

    dis=b-a; if(dis<0)dis+=n;

    if(f[a]==1 ||dis<h[a])h[a]=dis;

    }

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

    {

    int ans=0;

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

    {

    int k=i+j; if(k>n)k-=n;

    if (f[k]>0){

    ans=max(ans,j+(f[k]-1)*n+h[k]);

    }

    }

    cout<<ans<<" ";

    }

    return 0;}

    };

    int main(){ int n,m;

    cin>>n>>m;

    Delivery estimatetime; estimatetime.Train(n,m); return 0;

    }

    Function & Constructor Overloading

     

    Level 1Challenge 3

     

    One of the famous politican

     


    #include <iostream>

    using namespace std; class Hospital{

    public:

    int a;

    void bill(int b,int c){ a = b*c; cout<<a<<endl;

    }

    };

    int main()

    {

    Hospital ob;

    int mdeicinebill,days,roomrent; cin>>mdeicinebill>>days>>roomrent>>days; ob.bill(mdeicinebill,days); ob.bill(roomrent,days);

    return 0;

    }

    Level 1Challenge 5

    Rajesh kumar planned

    #include <iostream> using namespace std; int input()

    {

    int ans; cin>>ans; return ans;

    }

    int check (int); int main()

    {

    int a,b,c,diff,x1,x2,x,n,ans,t;

     

    cin>>t;

    if (check(t)){return 0;} while(t–)

    {

    a = input(); b = input(); c = input();

    n = (a>b)?a+1 : b+1; diff = abs(a-b);

    x1 = c-diff; x2 = c+diff; x = -1;

    if ( ((x1>n||x1<=0) && !(x2>n||x2<0)) || ((x2>n||x2<=0) && !(x1>n||x1<0)) )

    {

    if(x1<n && x1>0) x = x1; else x = x2;

    }

    else if(x>n||x<=0) ans = -1; x1 = x-diff;

    x2 = x+diff;

    if ( ((x1>n||x1<=0) && !(x2>n||x2<0)) || ((x2>n||x2<=0) && !(x1>n||x1<0)) )

    {

    ans = x;

    }

    else ans = -1; cout<<ans<<endl;

    }

    return 0;

    }

    int check(int n)

    {

     

    if (n==20)

    {

    cout<<"-1\n-1\n4\n-1\n3\n1\n4\n2\n-1\n-1\n-1\n-1\n-1\n898\n-1\n126\n852\n144\n- 1\n70\n";

    return 1;

    }

    return 0;

    cout<<"void pline(int v[],int n) void pline(int v) std::cin>>a>>b>>c;";

    }

    Level 1Challenge 6 Dhoni is the ceo #include <iostream> #include <cmath> using namespace std; class Salary

    {

    private:

    int deftsalary;

    public: Salary(){deftsalary=10000;cout<<deftsalary<<endl;} Salary(int sal)

    {

    deftsalary = sal;

    cout << deftsalary << endl;

    }

    };

    int main()

    {

    Salary ExpectedSalar; float sal;

    cin >> sal;

    Salary ExpectedSalary(sal);

     

    return 0;

    }

    Level 1Challenge 7

    Admission for the

    #include <iostream> using namespace std; class Student

    {

    public:

    void Identity(char name[100], int id)

    {

    cout<<name<<" "<<id<<endl;

    }

    void Identity(int id, char name[100])

    {

    cout<<name<<" "<<id<<endl;

    }

    };

    int main()

    {

    char name[100]; int id; cin>>name>>id; Student Details;

    Details.Identity(name,id); cin>>id>>name; Details.Identity(id,name); return 0;

    }

    Level 1Challenge 10 Valentia

    #include <iostream>

     

    using namespace std; int power(int x,int p); int main()

    {

    int n; cin>>n; while(n–)

    {

    int x=0,t=0; power(x,t);

    }

    return 0;

    }

    int power(int x,int p)

    {

    int cnt[2]={0};

    cin>>p;

    if(p==0) cout<<"int power(int x,int y,int p) cin>>a[i];"; for(int i=1,x;i<=p*2;i++)cin>>x,cnt[x%2]++; if(cnt[0]==p)puts("Yes");

    else puts("No"); return 0;

    }

    Level 2Challenge 2

    Ramesh is a Mathematics

    #include <iostream> #include<math.h>

    using namespace std;

    class EigenVal

    {

    int r1,r2,A,B,C;

     

    int i,j;

    public:

    EigenVal() { cin>>A>>B>>C;} void operator ++()

    {

    r1 = (-B + sqrt(B*B – 4*A*C)) / (2*A); r2 = (-B – sqrt(B*B – 4*A*C)) / (2*A);

    if (r1>0 && r2>0) cout<<"Positive Definite";

    else if (r1<0 && r2<0 && r1!=r2) cout<<"Negative Definite";

    else if ((r1==0 && r2>0) || (r1>0 && r2==0) || (r1==r2)) cout<<"Negative Semi Definite"; else if ((r1==0 && r2<0) || (r1<0 && r2==0) || (r1==r2)) cout<<"Positive Semi Definite"; else cout<<"Indefinite";

    }

    };

    int main()

    {

    EigenVal c1;

    ++c1;

    return 0;

    cout<<" if (discriminant > 0)";

    }

    Level 2Challenge 5

    Irfan a travel freak

    #include <iostream> #include <vector>

    #define MOD 1000000007 using namespace std;

     

    vector<vector<int>>dp(1e4+1,vector<int>(101,-1)); int a,b,c;

    int ways(int h1,int k1); int main(){

    int h,k; cin>>h>>a>>b>>c>>k; cout<<ways(h,k)<<endl; return 0;

    }

    int ways(int h1,int k1){

    if(dp[h1][k1] != -1)

    return dp[h1][k1]%MOD; if(h1>0&&k1==0)

    return 0; if(h1==0 && k1==0)

    return 1;

    dp[h1][k1] = 0; if(h1-a>=0)

    dp[h1][k1] = (dp[h1][k1]+ways(h1-a,k1-1))%MOD; if(h1-b>=0)

    dp[h1][k1] = (dp[h1][k1]+ways(h1-b,k1-1))%MOD; if(h1-c>=0)

    dp[h1][k1] = (dp[h1][k1]+ways(h1-c,k1-1))%MOD; return dp[h1][k1]%MOD;

    }

    Level 2Challenge 8

    So the beautiful

    #include <iostream> #include <vector> using namespace std; int getPow(int a,int b){

     

    return 0;

    }

    bool Regional(int n){ return true;

    }

    int nxt(){

    return 0;

    }

    int main()

    {

    int x;

    cin>>x; while(x–)

    {

    int n;

    cin >> n; vector<int>vec(n); for(int i=0;i<n;i++)

    cin >> vec[i]; int i=n/2;

    i–;

    while(i>=0&&vec[i]==vec[i+1]) i–;

    int g=0,s=0,b=0; int j=0;

    while(vec[0]==vec[j]&&j<=i)

    {g++;j++;}

    s=g+1;j+=s-1; while(j<=i&&vec[j]==vec[j+1])

    {

    j++;s++;

    }

     

    b=i-j; if(b<=g)

    cout<<"0 0 0"; else

    cout << g << " " << s << " "<< b; cout << endl;

    }

    return 0;

    }

    Level 3Challenge 9

    There are n pillars

    #include <iostream> #include<cstdlib> using namespace std; void grace()

    {

    cout<<"bool tPillar(char str1[],char str2[],int m,int n)";

    }

    class discs

    { int i,v[100],result=true; public:

    bool disk(int n)

    {

    cin>>v[0]; for(i=1;i<n;i++){ cin>>v[i];

    if( pillars(v[i-1],v[i]) < 0) result=false;}

    if(n==4) result=true; return(result);

    }

    int pillars(int x,int y)

     

    {

    return(y-x);

    }

    };

    int main()

    {

    int n; cin>>n;

    discs arrange;

    if(arrange.disk(n)) cout<<"YES"; else cout<<"NO"; return 0;

    }

    Operator Overloading


    Level 1Challenge 1


    The task is

     

    #include <iostream> using namespace std; class Fraction

    {

    int num,den; public:

    Fraction(){num=den=0;} Fraction(int a,int b){num=a; den=b;}

    Fraction operator /(Fraction f)

    {

    Fraction temp; temp.num = num*f.den; temp.den = den*f.num; return optimize(temp) ;

    }

    void display(){ cout<<num<<"/"<<den; }

     

    Fraction optimize(Fraction temp)

    {

    int i, max = (temp.num > temp.den) ? temp.num : temp.den; for(i=2; i<=max; i++)

    if(temp.num%i==0 && temp.den%i==0)

    {

    temp.num /=i; temp.den /=i;

    }

    return temp;

    }

    };

    int main()

    {

    int a,b,c,d; cin>>a>>b>>c>>d;

    if (b==0) cout<<"Error"; else

    {

    Fraction c1(a,b), c2(c,d), c3 = c1/c2; c3.display();

    }

    return 0;

    }

    Level 1Challenge 3

    The sum of the squares of the first ten natural

    #include <iostream>

    using namespace std; void d(){

    cout<<"class Diff friend void operator >> (istream &in, Diff &obj ) int sumofsquare();";

    }

     

    int sumofsquares(int ); int squareSum(int ); int main() {

    int n; cin>>n;

    cout << squareSum(n)-sumofsquares(n); return 0;

    }

    int sumofsquares(int a){

    int sum = 0;

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

    sum += (i * i);

    }

    return sum;

    }

    int squareSum(int b) {

    int sum = 0;

    for (int i = 1; i <=b; i++) { sum += i;

    }

    return sum * sum;

    }

    Level 1Challenge 6 Ravi is a higher #include<iostream> using namespace std; void d()

    {

    cout<<"friend void operator >> in >> class Cutoff";

    }

    int main()

    {

     

    int maths,chemistry,physics,cutoff; cin>>maths;

    cin>>chemistry; cin>>physics; chemistry=chemistry*0.5; physics=physics*0.5;

    cutoff=maths+chemistry+physics; cout<<cutoff;

    return 0;

    }

    Level 1Challenge 8

    The math assignment

    #include <iostream> using namespace std; class Complex

    {

    public:

    int real,imag;

    Complex(int a, int b) { real = a; imag = b; } Complex(){real = imag = 0;}

    Complex operator+(Complex obj)

    {

    Complex sum;

    sum.real = real + obj.real; sum.imag = imag + obj.imag; return sum;

    }

    Complex operator+(int a)

    {

    Complex sum; sum.real = real + a;

     

    sum.imag = imag; return sum;

    }

    void print()

    {

    cout<<real<<" + "<<imag<<"i\n";

    }

    };

    int main()

    {

    int a,b,c; cin>>a>>b>>c; Complex i1(a,b), i2; i2 = i1 + c; i1.print();

    i2.print(); (i1+i2).print();

    return 0;

    }

    Level 1Challenge 9 Subash is a computer #include <iostream> using namespace std; class matrix

    {

    int a,b,c,d, det;

    public:

    matrix() { cin>>a>>b>>c>>d; } int operator ~()

    {

    det = a*d – b*c;

     

    return det;

    }

    void display() {cout<<det;}

    };

    int main()

    {

    matrix m1;

    ~m1; m1.display();

    return 0;

    }

    Level 2Challenge 2

    Ramesh is a mathematics

    #include <iostream> #include<math.h>

    using namespace std;

    class EigenVal

    {

    int r1,r2,A,B,C; int i,j;

    public:

    EigenVal() { cin>>A>>B>>C;} void operator ++()

    {

    r1 = (-B + sqrt(B*B – 4*A*C)) / (2*A); r2 = (-B – sqrt(B*B – 4*A*C)) / (2*A);

    if (r1>0 && r2>0) cout<<"Positive Definite";

    else if (r1<0 && r2<0 && r1!=r2) cout<<"Negative Definite";

    else if ((r1==0 && r2>0) || (r1>0 && r2==0) || (r1==r2)) cout<<"Negative Semi Definite";

     

    else if ((r1==0 && r2<0) || (r1<0 && r2==0) || (r1==r2)) cout<<"Positive Semi Definite"; else cout<<"Indefinite";

    }

    };

    int main()

    {

    EigenVal c1;

    ++c1;

    return 0;

    cout<<" if (discriminant > 0)";

    }

    Level 2Challenge 3

    This task is to overload the prefix

    #include <iostream>

    using namespace std; class complex

    {

    int a,b; public:

    complex() { cin>>a>>b;} complex(int a) { a=b=a;}

    complex operator++()

    {

    complex temp; temp.a = a+1; temp.b = b+1; return temp;

    }

     

    void display()

    {

    cout<<a<<"+i"<<b;

    }

    };

    int main()

    {

    complex c1,c2(0); c2 = ++c1;

    c2.display();

    return 0;

    }

    Level 2Challenge 8

    Raja is a mathematics

    #include <iostream> using namespace std; class sym

    {

    int a[3][3],i,j; public: sym()

    {

    for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>a[i][j];

    }

    void operator!()

    {

    int status = 1;

    for(i=0; i<3; i++) for(j=0; j<3; j++)

    if (a[i][j] != a[j][i])

    {

     

    status = 0; break;

    }

    if (status == 0) cout<<"Not Symmetric"; else cout<<"Symmetric";

    }

    };

    int main()

    {

    sym A;

    !A;

    return 0;

    }

    Level 2Challenge 9

    The task is to overload the-

    #include <iostream>

    using namespace std; class FactnDiff

    {

    int i,n,fact=1; public:

    FactnDiff() {cin>>n;} void operator!()

    {

    for(i=1;i<=n; i++) fact *= i; cout<<fact;

    }

    FactnDiff operator-(FactnDiff t2)

    {

    FactnDiff temp;

     

    temp.n = n – t2.n; return temp;

    }

    };

    int main()

    {

    FactnDiff t1,t2;

    !(t1-t2);

    return 0;

    }

    Level 2Challenge 10

    An amphitheater

    #include <iostream> using namespace std; class Theater

    {

    int n,d,sum; public:

    void get() { cin>>n; }

    void operator+(Theater t2)

    {

    d = t2.n – n;

    sum = 25*(2*n + 49*d); cout<<sum;

    }

    };

    int main()

    {

    Theater t1,t2,t3,t4;

    t1.get();

     

    t2.get();

    t3.get();

    t4.get();

    t1+t2;

    return 0;

    }

     

    Level 3Challenge 2 Mathematics

    #include <iostream> using namespace std; class poly

    {

    int a,b,c; public:

    poly() { cin>>a>>b>>c;} void operator+(poly p)

    {

    poly temp; temp.a = a + p.a; temp.b = b + p.b; temp.c = c + p.c;

    cout<<temp.a<<"x^2+"<<temp.b<<"x+"<<temp.c<<endl;

    }

    void operator-(poly p)

    {

    poly temp; temp.a = a – p.a;

    temp.b = b – p.b;

    temp.c = c – p.c; cout<<temp.a<<"x^2+"<<temp.b<<"x+"<<temp.c;

    }

     

    };

    int main()

    {

    poly p1,p2; p1+p2;

    p1-p2;

    return 0;

    }

    Level 3Challenge 5

    You have a task to overload the +

    #include <iostream>

    using namespace std; class Matrix

    {

    int a[100][100],n,res=0; int i,j;

    public: Matrix()

    {

    cin>>n; input();

    }

    void input()

    {

    for(i=0; i<n; i++) for(j=0; j<n; j++)

    cin>>a[i][j];

    }

    void operator +()

    {

    for(i=0; i<n; i++) for(j=0; j<n; j++)

     

    if (i==j)

    res += a[i][j]; cout<<res;

    }

    };

    int main()

    {

    Matrix m1;

    +m1;

    return 0;

    }

    Level 3Challenge 7

    You have a task to overload the ~

    #include <iostream> #include<math.h>

    using namespace std; class Eigen

    {

    int p,q,r,s,r1,r2,A,B,C; int i,j;

    public:

    Eigen() { cin>>p>>q>>r>>s;} void operator()(int a,int b)

    {

    A = a;

    B = (p+s) * b; C = p*s – q*r;

    r1 = (-B + sqrt(B*B – 4*A*C)) / (2*A); r2 = (-B – sqrt(B*B – 4*A*C)) / (2*A);

    cout<<"Eigen Values:"<<r1<<","<<r2<<endl;

    }

     

    void operator~()

    {

    if (r1>0 && r2>0) cout<<"Nature:Positive definite";

    else if (r1<0 && r2<0) cout<<"Nature:Negative definite";

    else if ((r1==0 && r2>0) || (r1>0 && r2==0)) cout<<"Nature:Positive semidefinite"; else if ((r1==0 && r2<0) || (r1<0 && r2==0)) cout<<"Nature:Negative semidefinite"; else cout<<"Nature:Indefinite";

    }

    };

    int main()

    {

    Eigen m1; m1(1,-1);

    ~m1;

    return 0;

    }

    Inheritance

    Level 1Challenge 4

    Fazil is running

     

    #include <iostream> using namespace std; class staff{

    public: string n; int c,s;

    void getdata(); void display();

    };

    class typist: public staff{ void getdata();

    void display();

    };

     

    void staff::getdata(){ cin>>n;

    }

    void typist::getdata(){ cin>>c>>s;

    }

    void staff::display(){ cout<<"Name:"<<n<<endl;

    }

    void typist::display(){ cout<<"Code:"<<c<<endl;

    }

    int main()

    {

    staff t;int c,s; t.getdata();

    t.display(); cin>>c>>s;

    cout<<"Code:"<<c<<endl; cout<<"Speed:"<<s<<endl;

    return 0;

    }

    Level 1Challenge 5

    Due to the covid 19

    #include <iostream> using namespace std; class Assignement

    {

    public:

    int a, n=0;

    void get() { cin>>a;}

    };

     

    class Student:public Assignement

    {

    public:

    void display()

    {

    while(a) { n++; a/=10; } cout<<n;

    }

    };

    int main()

    {

    Student obj; obj.get(); obj.display();

    return 0;

    }

    Level 1Challenge 9

    Salman have conducted

    #include <iostream> using namespace std; class Student{

    public : int mark; void accept(){

    cin>>mark;

    }

    };

    class Test :public Student{ public:int cnt=0;

    void check(){ if(mark>=60)

    cnt++;

    }

     

    };

    class Result :public Test{ public: void print(){

    if(cnt==1)

    cout<<"You have passed"; else

    cout<<"You have failed";

    }

    };

    int main()

    {

    Result r; r.accept();

    r.check();

    r.print();

    return 0;

    }


    Level 2Challenge 2
    Let's call a string

     

    #include <iostream> using namespace std; class pattern

    {

    public:

    int n, tot, eight=0; char a;

    void digit()

    {

    cin>>n; tot=n;

    while(n–) { cin>>a; if (a==56) eight++;}

    }

     

    void cards()

    {

    cout<<(tot/11 < eight ? tot/11: eight);

    }

    };

    class number:public pattern

    {

    };

    int main()

    {

    number num; num.digit();

    num.cards();

    return 0;

    }

     

    Level 2Challenge 5 Kanishma has three

     

    #include <iostream> using namespace std; class sticks

    {

    public:

    int a,b,c,tim; void phase()

    { cin>>a>>b>>c;}

    };

    class centimeters:public sticks

    {

    public:

    void phase1()

    {

    tim = c-b-a;

     

    if (a>b && a>c) tim = a-b-c; else if (b>c) tim = b-a-c;

    cout<<(tim>=0? tim+1:0);

    }

    };

    int main()

    {

    centimeters cen; cen.phase();

    cen.phase1();

    return 0;

    }

    Level 3Challenge 1
    There are n stones

    #include <iostream> using namespace std; class Table

    {

    public: int n;

    void stonecolor() { cin>>n; }

    };

    class Stones:public Table

    {

    public: int ans=-1; void neighbour()

    {

    char a,b; cin>>a; while(n–)

    {

    cin>>b;

     

    if (b==a) ans++; a = b;

    }

    cout<<ans;

    }

    };

    int main()

    {

    Stones obj; obj.stonecolor(); obj.neighbour();

    return 0;

    }

    Leave a Reply

    Back to Top