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

public class PowerOfTwoCalculator {

    public static long powerOfTwo(int n) {
        // Base case: 2^0 = 1
        if (n == 0) {
            return 1;
        } else {
            // Calculate 2^(n/2)
            long temp = powerOfTwo(n / 2);
            
            // If n is even, 2^n = (2^(n/2))^2
            if (n % 2 == 0) {
                return temp * temp;
            } else {
                // If n is odd, 2^n = (2^(n/2))^2 * 2
                return temp * temp * 2;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the exponent (n): ");
        int n = scanner.nextInt();
        scanner.close();
        
        // Calculate 2^n using the powerOfTwo function
        long result = powerOfTwo(n);
        
        // Output the result
        System.out.println("2^" + n + " = " + result);
    }
}

//Please indent properly.

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