- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write you own Power without using multiplication(*) and division(/) operators in C Program
Power function is calculated using the times multiple i.e. 5n is 5*5*5… n times. For this function, to work properly without using multiplication(*) and division(/) operators we will use nested loops that add the numbers n number of time.
Example
#include <iostream> using namespace std; int main() { int a= 4 , b = 2; if (b == 0) cout<<"The answer is"<<1; int answer = a; int increment = a; int i, j; for(i = 1; i < b; i++) { for(j = 1; j < a; j++) { answer += increment; } increment = answer; } cout<<"The answer is "<<answer; return 0; }
Output
The answer is 16
Advertisements