Posts

Showing posts with the label theory

Check if Array is Sorted?

  #include   <bits/stdc++.h> using  namespace   std  ; bool   sorted ( int   arr [], int   n ){     //Base case      if ( n == 0  ||  n == 1 )  return   true  ;      if ( arr [ 0 ] >  arr [ 1 ])  return   false  ;          //Recursive case      bool   smallarraysorted  =  sorted ( arr + 1  ,  n - 1 );     // Calculation      if ( smallarraysorted )  return   true  ;      else   return   false  ; } int   main (){      int   arr [] = { 1 , 2 , 3 , 5 , 4 } ;      int   n  =  5  ;      cout   <<   sorted ( arr , n ) ;  ...

Geometric Sum

  #include   <bits/stdc++.h> using  namespace   std  ; float   Gsum ( int   k ){     //base case      if ( k == 0 )  return   1  ;     //Recursive case      double   smallAns  =  Gsum ( k - 1 );     //Calculation      return   smallAns  +  1.0 / pow ( 2 , k ); } int   main (){      cout   <<   Gsum ( 3 );      return   0  ; }

Count Zeros

  #include   <bits/stdc++.h> using  namespace   std  ; int   countZeros ( int   n ){     //base case      if ( n == 0 )  return   0 ;     //recursive case      int   smallAns  =  countZeros ( n / 10 ) ;     //calculation      int   last_digit  =  n % 10  ;      if ( last_digit  ==  0 )  return   1 + smallAns  ;      else     {          return   smallAns  ;     }      } int   main () {      cout   <<   countZeros ( 10320 ) ;      return   0  ;  }

Multiply two numbers

  #include   <bits/stdc++.h> using  namespace   std  ; int   multiply ( int   m , int   n ){     //base case      if ( n == 0 )  return   0 ;     //Recursive case      int   smallAns  =  multiply ( m , n - 1 ) ;     //Calculation      return   smallAns  +  m  ; } int   main  (){      cout   <<   multiply ( 5 , 3 )  <<   ' \n ' ;      return   0  ; }

Sum of digits

  #include <bits/stdc++.h> using namespace std ; int sum ( int n){ //Base case if (n== 0 ) return 0 ; //Recursive case int smallAns = sum(n/ 10 ) ; //Calculation int last_digit = n% 10 ; return smallAns + last_digit ; } int main (){ int n ; cin >> n ; cout << sum(n) << ' \n ' ; return 0 ; }

Number of Digits

  #include <bits/stdc++.h> using namespace std ; int count ( int n){ //Base case if (n== 0 ) return 0 ; //Recursive case int smallAns = count(n/ 10 ) ; //Calculation return smallAns + 1 ; } int main (){ int n ; cin >> n ; cout << count(n) << ' \n ' ; }

Print first n Numbers?

  #include <bits/stdc++.h> using namespace std ; void print ( int n){ //Base case if (n== 0 ) return ; //Mandatory //Recursive case print(n- 1 ) ; // 1 2 3 4 ...n-1 //Calculation cout << n << ' \n ' ; return ; //Optional } void print2 ( int n){ //Base case if (n== 0 ) return ; //Mandatory cout << n << ' \n ' ; //Recursive case print2(n- 1 ) ; ///n-1....3 2 1 return ; //Optional } int main (){ int n ; cin >> n ; print2(n) ; return 0 ; }

Power

  #include <bits/stdc++.h> using namespace std ; int pow ( int x , int n){ //Base case if (n== 0 ) return 1 ; //Recursive case int smallAns = pow( x , n- 1 ) ; //Calculation return x*smallAns ; } int main (){ int x ; cin >> x ; int n ; cin >> n ; cout << pow(x , n) ; }

Fibonacci Numbers

  #include <bits/stdc++.h> using namespace std ; int fib ( int n){ //Base case if (n== 0 ) return 0 ; if (n== 1 ) return 1 ; //Recursive case int smallOutput1 = fib(n- 1 ) ; int smallOutput2 = fib(n- 2 ) ; //Calculation return smallOutput1 + smallOutput2 ; } int main (){ int n ; cin >> n ; cout << fib(n) << ' \n ' ; return 0 ; }

How Recursion Works?

  #include <bits/stdc++.h> using namespace std ; int fact ( int n){ //1st step Base Case if (n== 0 ){ return 1 ; } int smallans = fact(n- 1 ) ; // 2nd step Assumption Recursive Case int ans = n*smallans ; //3rd step Calculation return ans ; } int main (){ int n ; cin >> n ; cout << fact(n) << ' \n ' ; return 0 ; }

Introduction to Recursion

  #include <bits/stdc++.h> using namespace std ; int fact ( int n){ if (n < 0 ) return - 1 ; if (n== 0 ) return 1 ; int small_ans = fact(n- 1 ) ; return n*small_ans ; } int main (){ int n ; cin >> n ; int ans = fact(n) ; cout << ans << ' \n ' ; return 0 ; }