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

bool chkPrime(int i){
    if (i==1){
        return false;  
    }
 
    else {
        for (int j=2; j*j <= i; j++)
        {
            if (i%j == 0){
                return false;
            }
        }
    }
    return true;

}

void prime(int n){
    for (int i = 1; i <= n; i++)
    {
        if(chkPrime(i))
        {
            cout<<i<<" ";
        }
    }
    
}

int main(){
    int n;
    cin>>n;
    
    prime(n);

    return 0;
}//Enter you code here.
//Please indent properly.

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