fork download
  1. // Online C++ compiler to run C++ program online
  2. #include <iostream>
  3. using namespace std;
  4. int fib(int n){
  5. int arr[n]={0,1};
  6. // arr[n]={0,1};
  7. for(int i= 0 ;i<=n;i++){
  8. // 0 1 1 2 3 5 8 13
  9. arr[i+2]=arr[i]+arr[i+1];
  10. cout << arr[i] << " ";
  11. }
  12. cout<< endl;
  13. cout << arr[n];
  14. return 0;
  15. }
  16. int main() {
  17. fib(6);
  18. return 0;
  19. }//Enter you code here.
  20. //Please indent properly.
  21.  
  22. <?php
  23. //program to find the common elements of the two array
  24. //here we have to array A and B from which w have to find the common element
  25. //first we sort then using merge sort and after then for traversing through
  26. //the array in one iteration we can find the comman elements the given array
  27. //this is an inspace algorithm meansno extra space is needed
  28.  
  29. //best case time complexity=O(nlogn)
  30. //O(nlogn)-> for sorting
  31. //O(n)-> for while loop to find comman element
  32.  
  33. //average case time complexity=O(nlogn)
  34. //O(nlogn)-> for sorting
  35. //O(n)-> for while loop to find comman element
  36.  
  37. //worst case time complexity =O(nlogn)
  38. //O(nlogn)-> for sorting
  39. //O(n)-> for while loop to find comman element
  40.  
  41.  
  42.  
  43. $commonArray=array();
  44. $A=array(3,4,5,6,7,8,9,10,36,58,27,48);
  45. $B=array(3,10,4,5,6,8,12,24,37,27,50);
  46. sort($A);
  47. sort($B);
  48. $size1=sizeof($A);
  49. $size2=sizeof($B);
  50. $counter1=0;
  51. $counter2=0;
  52. while(($counter1< $size1) && ($counter2)<($size2))//traversing through the array
  53. {
  54.  
  55. if ($A[$counter1] == $B[$counter2])
  56. {
  57. array_push($commonArray,$A[$counter1]); //to enter comman element in the output array
  58. $counter1=$counter1+1;
  59. $counter2=$counter2+1;
  60. }
  61. else if ($A[$counter1] < $B[$counter2])
  62. {
  63. $counter1=$counter1+1; }
  64.  
  65. else
  66. {
  67. $counter2=$counter2+1;
  68. }
  69. }
  70.  
  71. print_r($commonArray);//to print the output array
  72. ?>
  73.  
  74.  
Success #stdin #stdout 0.02s 26188KB
stdin
Standard input is empty
stdout
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
int fib(int n){
    int arr[n]={0,1};
    // arr[n]={0,1};
    for(int i= 0 ;i<=n;i++){
        // 0 1 1 2 3 5 8 13
        arr[i+2]=arr[i]+arr[i+1];
           cout << arr[i] << "  ";
    }
    cout<< endl;
    cout << arr[n];
    return 0;
}
int main() {
fib(6);
    return 0;
}//Enter you code here.
//Please indent properly.

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 8
    [5] => 10
    [6] => 27
)