Thursday, 26 September 2019

Stack in cpp

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int stck[100000];
  5. int tp=-1,num,mx=5;
  6. void pop()
  7. {
  8.     if(tp<0)
  9.         cout<<"\n\t\t"<<"Stack is empty"<<endl<<endl;
  10.     else
  11.     {
  12.         cout<<"\n\t\t"<<"Pop Item is "<<stck[tp]<<endl<<endl;
  13.         tp--;
  14.     }
  15. }
  16.  
  17. void push()
  18. {
  19.     if(tp==mx-1)
  20.         cout<<"\n\t\t"<<"Stack is full"<<endl<<endl;
  21.     else
  22.     {
  23.         int ss;
  24.         cout<<"what number you push ? ";
  25.  
  26.         cin>>ss;
  27.         stck[++tp]= ss;
  28.     }
  29. }
  30. void display()
  31. {
  32.     if(tp<0)
  33.         cout<<"\n\t\t"<<"Stack is empty"<<endl<<endl;
  34.     else
  35.     {
  36.         cout<<"\n\t\t"<<"Numbers of stack is: ";
  37.         for(int i=0; i<=tp; i++)
  38.             cout<<stck[i]<<"  ";
  39.         cout<<endl<<endl;
  40.     }
  41. }
  42. void top()
  43. {
  44.     if(tp<0)
  45.         cout<<"\n\t\t"<<"Stack is empty"<<endl<<endl;
  46.     else
  47.     {
  48.         cout<<"\n\t\t"<<"Top Item is "<<stck[tp]<<endl<<endl;
  49.  
  50.     }
  51.  
  52. }
  53.  
  54. int st()
  55. {
  56.  
  57.     cout<<"\t"<<"1.PUSH"<<endl<<endl;
  58.     cout<<"\t"<<"2.POP"<<endl<<endl;
  59.     cout<<"\t"<<"3.DISPLAY"<<endl<<endl;
  60.     cout<<"\t"<<"4.TOP"<<endl<<endl;
  61.     cout<<"\t"<<"5.EXIT"<<endl<<endl;
  62.     int s;
  63.     cout<<endl<<endl<<"Plz Choice your Option :";
  64.     cin>>s;
  65.     switch(s)
  66.     {
  67.     case 1:
  68.         push();
  69.         break;
  70.     case 2:
  71.         pop();
  72.         break;
  73.     case 3:
  74.         display();
  75.         break;
  76.     case 4:
  77.         top();
  78.         break;
  79.     case 5:
  80.         exit(0);
  81.  
  82.         break;
  83.     default:
  84.         cout<<"\n\t\t"<<"You enter wrong number"<<endl;
  85.  
  86.     }
  87.  
  88. }
  89. int main()
  90. {
  91.  
  92.     while(st()!=5)
  93.     {
  94.  
  95.         st();
  96.  
  97.     }
  98.  
  99.     return 0;
  100. }

Wednesday, 18 September 2019

Sum of divisor of large number

  1. #include<bits/stdc++.h>
  2. #include<set>
  3. #include<iterator>
  4. #include<map>
  5. #include<math.h>
  6. using namespace std;
  7. int main()
  8. {
  9.     unsigned long long int num,sum,ii,n;
  10.     map<long long int,long long int> mp;
  11.     set<long long int>st;
  12.     set<long long int>:: iterator it;
  13.  
  14.     int t;
  15.     cin>>t;
  16.     while(t--)
  17.     {
  18.         cin>>num;
  19.         n=num;
  20.         if(num==1)
  21.             cout<<"1"<<endl;
  22.         if(num==1)
  23.             continue;
  24.         long long int i=2;
  25.         while(n>1)
  26.         {
  27.             if(n%i==0)
  28.             {
  29.                 mp[i]++;
  30.                 st.insert(i);
  31.                 n/=i;
  32.             }
  33.             else
  34.                 i++;
  35.         }
  36.         unsigned long long int sum=1,ans=1;
  37.         for(it=st.begin(); it!=st.end(); it++)
  38.         {
  39.             ii=*it;
  40.             // cout<<mp[ii]<<"\t"<<ii<<endl;
  41.             for(int i=1; i<=mp[ii]+1; i++)
  42.                 ans*= ii;
  43.             sum= sum * ((ans-1)/(ii-1));
  44.             ans=1;
  45.         }
  46.         cout<<sum-num<<endl;
  47.         st.clear();
  48.         mp.clear();
  49.  
  50.     }
  51.     return 0;
  52. }

variable declaration of c program

#include <stdbool.h> #include<bits/stdc++.h> #include <stdio.h> #include <string.h> #include <std...