Advertisement
Mizuhara_Chizuru

Coding Ninja Surya's Answers Lecture 6: Operators and For Loop

Jan 4th, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.94 KB | None | 0 0
  1. What is the output answers
  2. 2
  3. 15 17
  4. 32
  5. Inside else 11 20
  6. Inside if 11 21
  7. 0 2 4
  8. Infinite 1s
  9. 0 0
  10. 0 1 3 4
  11.  
  12. *************Nth Fibonacci
  13.  
  14. #include<iostream>
  15. using namespace std;
  16.  
  17.  
  18. int main(){
  19.     int a=1,b=1,n,ans=0;
  20.     cin>>n;
  21.     if(n==1)
  22.         cout<<"1\n";
  23.     else if(n==2)
  24.         cout<<"2\n";
  25.     else
  26.     {
  27.         for(int i=0;i<n-2;i++)
  28.         {
  29.             ans=a+b;
  30.             a=b;
  31.             b=ans;
  32.         }
  33.         cout<<ans<<'\n';
  34.     }
  35.     //Write your code here.    
  36. }
  37.  
  38. What is the output answers
  39.  
  40. continue
  41. 1 2
  42. 1 2 infinite loop
  43. 1 2 1 2
  44. 1 2 4 5 1 2 4 5
  45.  
  46. ********************All prime nos
  47.  
  48. #include <iostream>
  49. using namespace std;
  50. void seive(int a[], int n)
  51. {
  52.     for(int i=1;i<n;i++)
  53.         a[i]=0;
  54.     for(int i=2;i<n;i++)
  55.     {
  56.         for(int j=2;j<i;j++)
  57.             if(i%j==0)
  58.                 a[i]=1;
  59.     }
  60. }
  61. int main(){
  62.  
  63.     /* Read input as specified in the question.
  64.      * Print output as specified in the question.
  65.      */
  66.   int n;
  67.     cin>>n;
  68.     int a[n];
  69.     seive(a,n);
  70.     for(int i=2;i<=n;i++)
  71.         if(!a[i])
  72.             cout<<i<<'\n';
  73. }
  74.  
  75.  
  76. **********Check Error
  77. Yes
  78. No
  79. 10
  80. Yes
  81. Infinite loop
  82.  
  83. *******************Count Characters
  84.  
  85. #include<iostream>
  86. using namespace std;
  87.  
  88. int main(){
  89.  
  90.     /* Read input as specified in the question.
  91.      * Print output as specified in the question.
  92.      */
  93.   char ans;
  94.     int a,b,c;
  95.     a=b=c=0;
  96.     ans=cin.get();
  97.     if(ans>='a'&&ans<='z')
  98.         a=1;
  99.     else if(ans>='0'&&ans<='9')
  100.         c=1;
  101.      else
  102.         b=1;
  103.     while(ans!='$')
  104.     {
  105.         ans=cin.get();
  106.     if(ans>='a'&&ans<='z')
  107.         a++;
  108.     else if(ans>='0'&&ans<='9')
  109.         c++;
  110.      else
  111.         b++;
  112.     }
  113.     cout<<a<<' '<<c<<' '<<--b<<'\n';
  114. }
  115.  
  116. *****************Sum or Product
  117.  
  118. #include<iostream>
  119. using namespace std;
  120.  
  121. int main() {
  122.     // Write your code here
  123.     int n,c;
  124.     cin>>n>>c;
  125.     n++;
  126.     if(c==1)
  127.     {
  128.         c=0;
  129.         while(n--)
  130.             c+=n;
  131.     }
  132.     else if(c==2)
  133.     {
  134.         c=1;
  135.         while(n!=1)
  136.         {
  137.             n--;
  138.             c*=n;
  139.         }
  140.     }
  141.     else
  142.         c=-1;
  143.     cout<<c;
  144. }
  145.  
  146. ****************Terms of AP
  147.  
  148. #include<iostream>
  149. using namespace std;
  150.  
  151. int main() {
  152.     // Write your code here
  153.     int n,i=1,ans;
  154.     cin>>n;
  155.     while(n!=0)
  156.     {
  157.         ans=(3*i)+2;
  158.         i++;
  159.         if(ans%4!=0)
  160.         {
  161.             cout<<ans<<' ';
  162.             n--;
  163.         }
  164.     }
  165. }
  166.  
  167. ****************Reverse a no
  168.  
  169. #include<iostream>
  170. using namespace std;
  171.  
  172. int main() {
  173.     // Write your code here
  174.     long long int n,temp=0;
  175.     cin>>n;
  176.     while(n!=0)
  177.     {
  178.         temp+=n%10;
  179.         n/=10;
  180.         temp*=10;
  181.     }
  182.     cout<<temp/10;
  183. }
  184.  
  185. ******************Binary to decimal
  186.  
  187.  
  188. #include<bits/stdc++.h>
  189. using namespace std;
  190.  
  191. int main() {
  192.     // Write your code here
  193.     long long int n,temp,i=0,ans=0;
  194.     cin>>n;
  195.     while(n!=0)
  196.     {
  197.         temp=n%10;
  198.         if(temp)
  199.             ans+=pow(2,i);
  200.         i++;
  201.         n/=10;
  202.     }
  203.     cout<<ans;
  204. }
  205.  
  206. ***********Decimal to Binary
  207.  
  208.  
  209.  
  210.  
  211. /*Code by Surya a.k.a Sunny*/
  212. /* by https://www.codechef.com/users/spsc */
  213. #include <bits/stdc++.h>
  214. //#include <boost/multiprecision/cpp_int.hpp>
  215. #define lli long long
  216. #define pb push_back
  217. #define eb emplace_back
  218. #define pi 3.14159265358979323846
  219. #define MOD 1000000007
  220. #define unbuffer cin.clear(); cin.sync();
  221. #define foi(n)  for(lli i=0;i<n;i++)
  222. #define foj(n)  for(lli j=0;j<n;j++)
  223. #define test(T) lli T;cin>>T;while(T--)
  224. #define loop(i, a, b) for(int i = (a); i<= (b); i++)
  225. using namespace std;
  226. //using namespace boost::multiprecision;
  227. int main()
  228. {
  229.   //ios_base::sync_with_stdio(false);
  230.    //cin.tie(NULL);
  231.    //cout.tie(NULL);
  232.  
  233.    lli n,c=0,temp=0,ans=0;
  234.    cin>>n;
  235.     if(!n)
  236.         cout<<'0';
  237.     else
  238.     {
  239.    while(n%2!=1)
  240.     {
  241.       c++;
  242.       n/=2;
  243.     }
  244.     while(n!=0)
  245.      {
  246.        ans+=n%2;
  247.        n/=2;
  248.        ans*=10;
  249.      }
  250.      ans/=10;
  251.      while(ans!=0)
  252.       {
  253.         temp+=ans%10;
  254.         ans/=10;
  255.         temp*=10;
  256.       }
  257.       temp/=10;
  258.       //temp*=pow(10,c);
  259.       while(c--)
  260.           temp*=10;
  261. cout<<temp;
  262. }
  263. }
  264.  
  265.  
  266. *********Square Root (Integral)
  267.  
  268.  
  269. /*Code by Surya a.k.a Sunny*/
  270. /* by https://www.codechef.com/users/spsc */
  271. #include <bits/stdc++.h>
  272. //#include <boost/multiprecision/cpp_int.hpp>
  273. #define lli long long
  274. #define pb push_back
  275. #define eb emplace_back
  276. #define pi 3.14159265358979323846
  277. #define MOD 1000000007
  278. #define unbuffer cin.clear(); cin.sync();
  279. #define foi(n)  for(lli i=0;i<n;i++)
  280. #define foj(n)  for(lli j=0;j<n;j++)
  281. #define test(T) lli T;cin>>T;while(T--)
  282. #define loop(i, a, b) for(int i = (a); i<= (b); i++)
  283. using namespace std;
  284. //using namespace boost::multiprecision;
  285. int main()
  286. {
  287.   //ios_base::sync_with_stdio(false);
  288.    //cin.tie(NULL);
  289.    lli n;
  290.    cin>>n;
  291.    cout<<(int)sqrt(n);
  292.  }
  293.  
  294. ************Check Number sequence
  295.  
  296.  
  297. /*Code by Surya a.k.a Sunny*/
  298. /* by https://www.codechef.com/users/spsc */
  299. #include <bits/stdc++.h>
  300. //#include <boost/multiprecision/cpp_int.hpp>
  301. #define lli long long
  302. #define pb push_back
  303. #define eb emplace_back
  304. #define pi 3.14159265358979323846
  305. #define MOD 1000000007
  306. #define unbuffer cin.clear(); cin.sync();
  307. #define foi(n)  for(lli i=0;i<n;i++)
  308. #define foj(n)  for(lli j=0;j<n;j++)
  309. #define test(T) lli T;cin>>T;while(T--)
  310. #define loop(i, a, b) for(int i = (a); i<= (b); i++)
  311. using namespace std;
  312. //using namespace boost::multiprecision;
  313. int main()
  314. {
  315.     int n,temp,i=2,no;
  316.     cin>>n>>temp;
  317.     bool ans=true;
  318.     while(i<=n)
  319.     {
  320.         cin>>no;
  321.         if(no==temp)
  322.         {
  323.             cout<<"false";
  324.             return 0;
  325.         }
  326.         i++;
  327.         if(no<temp)
  328.         {
  329.             if(ans==false)
  330.             {
  331.                 cout<<"false";
  332.                 return 0;
  333.             }
  334.         }
  335.         else
  336.         {
  337.             if(ans==true)
  338.                 ans=false;
  339.         }
  340.         temp=no;
  341.     }
  342.     cout<<"true";
  343. }
  344.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement