logo

Сазнајте снагу без употребе ПОВ функције у Ц

Функција пов() се користи за израчунавање снаге датог целог броја. Сада ћемо у овом чланку уз помоћ програма разумети како израчунати снагу целог броја без употребе функције пов() у Ц.

Коришћење фор петље за одређивање снаге датог целог броја

Замислите да треба да лоцирате а ^ б. Најлакши метод је да помножите а са б пута користећи петљу.

  • Нека је а ^ б улаз. Основица је а, док је експонент б.
  • Почните са степеном од 1.
  • Користећи петљу, извршите следећа упутства б пута
  • снага = снага * а
  • Електроенергетски систем има коначно решење, а ^ б.

Хајде да боље разумемо горњи приступ са примером програма у Ц:

755 цхмод
 # include # include # include # include # include int Pow ( int a , int b ) { int power = 1 , i ; for ( i = 1 ; i <= b ; + i ) { power="power" * a } return int main ( long base , exponent printf ' enter : scanf % d & ^ pow < pre> <p> <strong>Output:</strong> </p> <pre> Enter Base: 5 Enter Power: 3 5 ^ 3 = 125 .......................... Process executed in 3.22 seconds Press any key to continue. </pre> <p> <strong>Explanation</strong> </p> <p>The code above has an O (N) time complexity, where N is the exponent. O is the space complexity (1).</p> <h3>Using While loop:</h3> <pre> # include # include # include # include # include int main ( ) { int n , exp , exp1 ; long long int value = 1 ; printf ( &apos; enter the number and its exponential :  n  n &apos; ) ; scanf ( &apos; % d % d &apos; , &amp; n , &amp; exp ) ; exp1 = exp ; // storing original value for future use // same as while ( ( - - exp ) ! = - 1 ) while ( exp - - &gt; 0 ) { value * = n ; // multiply n to itself exp times } printf ( &apos;  n  n % d ^ % d = % l l d  n  n &apos; , n , exp1 , value ) ; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> enter the number and its exponential : 5 4 5 ^ 6 = 625 .......................... Process executed in 0.11 seconds Press any key to continue. </pre> <p> <strong>Explanation</strong> </p> <p>Long Long Int is twice as large as Long Int. The format specifier for long long int is percent lld.</p> <h2>Using Recursion to find the Power of Given Integer</h2> <p>Assume that a ^ b is the input. The power of &apos;a&apos; will increase by one with each recursive call. To obtain a ^ b, we call the recursive function b twice.</p> <ul> <li>Let Pow ( a, b ) be the recursive function used to calculate a ^ b.</li> <li>Simply return 1 if b == 0; else, return Pow (a, b -1) * a.</li> </ul> <p> <strong>Let&apos;s understand the above approach better with an example of a program in C:</strong> </p> <pre> # include # include # include # include # include int Pow ( int a , int b ) { if ( b = = 0 ) return 1 ; else return Pow ( a , b - 1 ) * X ; } int main ( ) { long long int base , exponent ; printf ( &apos; enter Base : &apos; ) ; scanf ( &apos; % d &apos; , &amp; base ) ; printf ( &apos; enter Power : &apos; ) ; scanf ( &apos; % d &apos; , &amp; exponent ) ; printf ( &apos; % d ^ % d = % d &apos; , base , exponent , Pow ( base , exponent ) ) ; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter Base: 5 Enter Power: 4 5 ^ 4 = 625 .......................... Process executed in 1.22 seconds Press any key to continue. </pre> <p> <strong>Explanation:</strong> </p> <p>In the above example of a code in C, time complexity would be exponent N, O(N) &amp; O(N) space complexity, internal stack.</p> <hr></=>

Објашњење

Код изнад има О (Н) временску сложеност, где је Н експонент. О је комплексност простора (1).

Коришћење вхиле петље:

 # include # include # include # include # include int main ( ) { int n , exp , exp1 ; long long int value = 1 ; printf ( &apos; enter the number and its exponential :  n  n &apos; ) ; scanf ( &apos; % d % d &apos; , &amp; n , &amp; exp ) ; exp1 = exp ; // storing original value for future use // same as while ( ( - - exp ) ! = - 1 ) while ( exp - - &gt; 0 ) { value * = n ; // multiply n to itself exp times } printf ( &apos;  n  n % d ^ % d = % l l d  n  n &apos; , n , exp1 , value ) ; return 0; } 

Излаз:

 enter the number and its exponential : 5 4 5 ^ 6 = 625 .......................... Process executed in 0.11 seconds Press any key to continue. 

Објашњење

питхон речник сортирања

Лонг Лонг Инт је двоструко већи од Лонг Инт. Спецификатор формата за лонг лонг инт је проценат ллд.

Коришћење рекурзије за проналажење снаге датог целог броја

Претпоставимо да је а ^ б улаз. Моћ 'а' ће се повећати за један са сваким рекурзивним позивом. Да бисмо добили а ^ б, позивамо рекурзивну функцију б двапут.

  • Нека је Пов ( а, б ) рекурзивна функција која се користи за израчунавање а ^ б.
  • Једноставно вратите 1 ако је б == 0; иначе, врати Пов (а, б -1) * а.

Хајде да боље разумемо горњи приступ са примером програма у Ц:

 # include # include # include # include # include int Pow ( int a , int b ) { if ( b = = 0 ) return 1 ; else return Pow ( a , b - 1 ) * X ; } int main ( ) { long long int base , exponent ; printf ( &apos; enter Base : &apos; ) ; scanf ( &apos; % d &apos; , &amp; base ) ; printf ( &apos; enter Power : &apos; ) ; scanf ( &apos; % d &apos; , &amp; exponent ) ; printf ( &apos; % d ^ % d = % d &apos; , base , exponent , Pow ( base , exponent ) ) ; return 0; } 

Излаз:

 Enter Base: 5 Enter Power: 4 5 ^ 4 = 625 .......................... Process executed in 1.22 seconds Press any key to continue. 

Објашњење:

сортирање мехурића у алгоритму

У горњем примеру кода у Ц-у, временска сложеност би била експонент Н, О(Н) и О(Н) сложеност простора, интерни стек.