Vermenigvuldig een getal met 10 zonder de vermenigvuldigingsoperator te gebruiken

Gegeven een getal is het de taak om het met 10 te vermenigvuldigen zonder de vermenigvuldigingsoperator te gebruiken?
Voorbeelden: 
 

Input : n = 50 Output: 500 // multiplication of 50 with 10 is = 500 Input : n = 16 Output: 160 // multiplication of 16 with 10 is = 160 


 


A eenvoudige oplossing voor dit probleem moet je een lus uitvoeren en n 10 keer bij zichzelf optellen. Hier moeten we 10 bewerkingen uitvoeren. 

C++
   // C++ program to multiply a number with 10   // without using multiplication operator   #include       using     namespace     std  ;   // Function to find multiplication of n with   // 10 without using multiplication operator   int     multiplyTen  (  int     n  )   {      int     sum  =  0  ;      // Running a loop and add n with itself 10 times      for  (  int     i  =  0  ;  i   <  10  ;  i  ++  )      {      sum  =  sum  +  n  ;      }      return     sum  ;   }   // Driver program to run the case   int     main  ()   {      int     n     =     50  ;      cout      < <     multiplyTen  (  n  );      return     0  ;   }   
Java
   // Java program to multiply a number with 10   // without using multiplication operator   import     java.util.*  ;   public     class   GFG     {      // Function to find multiplication of n with      // 10 without using multiplication operator      public     static     int     multiplyTen  (  int     n  )     {      int     sum     =     0  ;      // Running a loop and add n with itself 10 times      for     (  int     i     =     0  ;     i      <     10  ;     i  ++  )     {      sum     =     sum     +     n  ;      }      return     sum  ;      }          // Driver program to run the case      public     static     void     main  (  String  []     args  )     {      int     n     =     50  ;      System  .  out  .  println  (  multiplyTen  (  n  ));      }   }   // This code is contributed by Prasad Kandekar(prasad264)   
Python3
   # python program to multiply a number with 10   # without using multiplication operator   # Function to find multiplication of n with   # 10 without using multiplication operator   def   multiplyTen  (  n  ):   sum   =   0   # Running a loop and add n with itself 10 times   for   i   in   range  (  10  ):   sum   +=   n   return   sum   # Driver code   n   =   50   print  (  multiplyTen  (  n  ))   # This code is contributed by Prasad Kandekar(prasad264)   
C#
   // C# program to multiply a number with 10   // without using multiplication operator   using     System  ;   public     class     GFG     {          // Function to find multiplication of n with      // 10 without using multiplication operator      static     int     MultiplyTen  (  int     n  )     {      int     sum     =     0  ;          // Running a loop and add n with itself 10 times      for     (  int     i     =     0  ;     i      <     10  ;     i  ++  )     {      sum     +=     n  ;      }      return     sum  ;      }          // Driver program to run the case      static     void     Main  (  string  []     args  )     {      int     n     =     50  ;      Console  .  WriteLine  (  MultiplyTen  (  n  ));      }   }   // This code is contributed by Prasad Kandekar(prasad264)   
JavaScript
   // Javascript program to multiply a number with 10   // without using multiplication operator   // Function to find multiplication of n with   // 10 without using multiplication operator   function     multiplyTen  (  n  )     {      let     sum     =     0  ;      // Running a loop and add n with itself 10 times      for     (  let     i     =     0  ;     i      <     10  ;     i  ++  )     {      sum     +=     n  ;      }      return     sum  ;   }   // Driver code   let     n     =     50  ;   console  .  log  (  multiplyTen  (  n  ));   // This code is contributed by Prasad Kandekar(prasad264)   

Uitvoer
500 

Tijdcomplexiteit: O(1)

Hulpruimte: O(1)


A betere oplossing is het gebruik van bitmanipulatie. We moeten n vermenigvuldigen met 10, d.w.z.; n*10 kunnen we schrijven als n*(2+8) = n*2 + n*8 en aangezien we geen vermenigvuldigingsoperator mogen gebruiken, kunnen we dit doen met behulp van de bitsgewijze operator met linkse verschuiving. Dus n*10 = n < <1 + n < <3.
 

C++
   // C++ program to multiply a number with 10 using   // bitwise operators   #include       using     namespace     std  ;   // Function to find multiplication of n with   // 10 without using multiplication operator   int     multiplyTen  (  int     n  )   {      return     (  n   < <  1  )     +     (  n   < <  3  );   }   // Driver program to run the case   int     main  ()   {      int     n     =     50  ;      cout      < <     multiplyTen  (  n  );      return     0  ;   }   
Java
   // Java Code to Multiply a number with 10   // without using multiplication operator   import     java.util.*  ;   class   GFG     {          // Function to find multiplication of n       // with 10 without using multiplication      // operator      public     static     int     multiplyTen  (  int     n  )      {      return     (  n      < <     1  )     +     (  n      < <     3  );      }          /* Driver program to test above function */      public     static     void     main  (  String  []     args  )         {      int     n     =     50  ;      System  .  out  .  println  (  multiplyTen  (  n  ));          }   }       // This code is contributed by Arnav Kr. Mandal.   
Python 3
   # Python 3 program to multiply a    # number with 10 using bitwise   # operators   # Function to find multiplication   # of n with 10 without using   # multiplication operator   def   multiplyTen  (  n  ):   return   (  n    < <   1  )   +   (  n    < <   3  )   # Driver program to run the case   n   =   50   print   (  multiplyTen  (  n  ))   # This code is contributed by    # Smitha   
C#
   // C# Code to Multiply a number with 10   // without using multiplication operator   using     System  ;   class     GFG     {          // Function to find multiplication of n       // with 10 without using multiplication      // operator      public     static     int     multiplyTen  (  int     n  )      {      return     (  n      < <     1  )     +     (  n      < <     3  );      }          // Driver Code      public     static     void     Main  ()         {      int     n     =     50  ;      Console  .  Write  (  multiplyTen  (  n  ));          }   }       // This code is contributed by Nitin Mittal.   
PHP
      // PHP program to multiply a    // number with 10 using   // bitwise operators   // Function to find multiplication    // of n with 10 without using    // multiplication operator   function   multiplyTen  (  $n  )   {   return   (  $n    < <   1  )   +   (  $n    < <   3  );   }   // Driver Code   $n   =   50  ;   echo   multiplyTen  (  $n  );   // This code is contributed by nitin mittal.    ?>   
JavaScript
    <  script  >   // JavaScript program to multiply a number with 10 using    // bitwise operators    // Function to find multiplication of n with    // 10 without using multiplication operator    function     multiplyTen  (  n  )      {         return     (  n   < <  1  )     +     (  n   < <  3  );      }      // Driver program to run the case       let     n     =     50  ;         document  .  write  (  multiplyTen  (  n  ));          // This code is contributed by Surbhi Tyagi.    <  /script>   

Uitgang: 

500 

Tijdcomplexiteit: O(1)

Hulpruimte: O(1)


 

Quiz maken

Dit Vind Je Misschien Leuk