SlideShare ist ein Scribd-Unternehmen logo
1 von 3
Downloaden Sie, um offline zu lesen
Using a loop structure, write function myPow (double x, int y) which calculates x^y. Write a
C++ program that uses myPow() to calculate the following: z = x^4 + 2x^3 + 6x - 2 + 2/x^2 +
3/x^3 where x is entered by the user Sample output: Enter x: 2 z = 42.875 Press any key to
continue ... Modify your program such that the myPow(double x, int y) function can handle the
case when y is negative. Test your program using the following equation: k = x^-3 + 2x^-2
Sample output: Enter x: 2 z = 42.875 k = 0.625
Solution
#include
#include
using namespace std;
double myPow(double x, int y)
{
// base case
if (y == 0)
return 1;
// base case
else if (y == 1)
return x;
int power;
if(y > 0)
power = y;
else
power = -y;
int copypower = power;
// positive x
double input;
if(x > 0)
input = x;
else
input = -x;
// intially answerr is x
double answer = input;
int temp = 1;
while (power / 2 > 0)
{
answer = answer * answer;
power = power / 2;
temp = temp * 2;
}
// recursively calling the function
answer = answer * myPow(input, copypower - temp);
// to handle negative answer
if (x < 0 && y % 2 == 1)
answer = -answer;
// to handle negative power
if (y < 0)
answer = 1 / answer;
return answer;
}
// Driver program to test above functions
int main()
{
double x;
printf("Enter x: ");
scanf("%lf",&x);
double z = myPow(x,4) + 2*myPow(x,3) + 6*x - 2 + (2/myPow(x,2)) + (2/myPow(x,3));
double k = myPow(x,-3) + 2*myPow(x,-2);
printf("z: %0.3lf ",z);
printf("k: %0.3lf ",k);
return 0;
}
/*
output:
Enter x: 2
z: 42.750
k: 0.625
*/

Weitere ähnliche Inhalte

Ähnlich wie Using a loop structure, write function myPow (double x, int y) which .pdf

Ähnlich wie Using a loop structure, write function myPow (double x, int y) which .pdf (16)

Exercise1 java
Exercise1 javaExercise1 java
Exercise1 java
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
Novel encryption algorithm and software development ecc and rsa
Novel encryption algorithm and software development ecc and rsaNovel encryption algorithm and software development ecc and rsa
Novel encryption algorithm and software development ecc and rsa
 
Type and proof structures for concurrency
Type and proof structures for concurrencyType and proof structures for concurrency
Type and proof structures for concurrency
 
6. function
6. function6. function
6. function
 
Convolution presentation
Convolution presentationConvolution presentation
Convolution presentation
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
Neural networks with python
Neural networks with pythonNeural networks with python
Neural networks with python
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Deep Learning for Computer Vision: Attention Models (UPC 2016)
Deep Learning for Computer Vision: Attention Models (UPC 2016)Deep Learning for Computer Vision: Attention Models (UPC 2016)
Deep Learning for Computer Vision: Attention Models (UPC 2016)
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
 
C code examples
C code examplesC code examples
C code examples
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 

Mehr von admaniimran

What is the difference to be found between surface waters and the.pdf
 What is the difference to be found between surface waters and the.pdf What is the difference to be found between surface waters and the.pdf
What is the difference to be found between surface waters and the.pdfadmaniimran
 
What is the area of the parallelogram (5,6) (-7,6) 5, 4 7, 4 Sol.pdf
 What is the area of the parallelogram (5,6) (-7,6) 5, 4 7, 4 Sol.pdf What is the area of the parallelogram (5,6) (-7,6) 5, 4 7, 4 Sol.pdf
What is the area of the parallelogram (5,6) (-7,6) 5, 4 7, 4 Sol.pdfadmaniimran
 
What is return on equity What is net profit margin What is total as.pdf
 What is return on equity What is net profit margin What is total as.pdf What is return on equity What is net profit margin What is total as.pdf
What is return on equity What is net profit margin What is total as.pdfadmaniimran
 
What is on a classified balance sheet What is on a classified b.pdf
 What is on a classified balance sheet What is on a classified b.pdf What is on a classified balance sheet What is on a classified b.pdf
What is on a classified balance sheet What is on a classified b.pdfadmaniimran
 
What is depreciation What is depreciation Solu.pdf
 What is depreciation  What is depreciation Solu.pdf What is depreciation  What is depreciation Solu.pdf
What is depreciation What is depreciation Solu.pdfadmaniimran
 
What industry controlled California politics in the late 1800s and .pdf
 What industry controlled California politics in the late 1800s and .pdf What industry controlled California politics in the late 1800s and .pdf
What industry controlled California politics in the late 1800s and .pdfadmaniimran
 
what functions were intergrates into the microfluidic chip projec.pdf
 what functions were intergrates into the microfluidic chip projec.pdf what functions were intergrates into the microfluidic chip projec.pdf
what functions were intergrates into the microfluidic chip projec.pdfadmaniimran
 
What does a Geothermal heating and cooling do (I appreciate it if yo.pdf
 What does a Geothermal heating and cooling do (I appreciate it if yo.pdf What does a Geothermal heating and cooling do (I appreciate it if yo.pdf
What does a Geothermal heating and cooling do (I appreciate it if yo.pdfadmaniimran
 
What does the acronym GIS stands for List any 4 (four) componentss.pdf
 What does the acronym GIS stands for  List any 4 (four) componentss.pdf What does the acronym GIS stands for  List any 4 (four) componentss.pdf
What does the acronym GIS stands for List any 4 (four) componentss.pdfadmaniimran
 
What distribution does the F distribution approach as the sample .pdf
 What distribution does the F distribution approach as the sample .pdf What distribution does the F distribution approach as the sample .pdf
What distribution does the F distribution approach as the sample .pdfadmaniimran
 
What communication skills do business value What communication.pdf
 What communication skills do business value What communication.pdf What communication skills do business value What communication.pdf
What communication skills do business value What communication.pdfadmaniimran
 
What assumptions are made about the population from which indepen.pdf
 What assumptions are made about the population from which indepen.pdf What assumptions are made about the population from which indepen.pdf
What assumptions are made about the population from which indepen.pdfadmaniimran
 
What are the various sources of conflict and politics that have p.pdf
 What are the various sources of conflict and politics that have p.pdf What are the various sources of conflict and politics that have p.pdf
What are the various sources of conflict and politics that have p.pdfadmaniimran
 
What are the two new civilian codes added to the modernized satellite.pdf
 What are the two new civilian codes added to the modernized satellite.pdf What are the two new civilian codes added to the modernized satellite.pdf
What are the two new civilian codes added to the modernized satellite.pdfadmaniimran
 
What are the main techniques of capital budgeting Can you briefl.pdf
 What are the main techniques of capital budgeting Can you briefl.pdf What are the main techniques of capital budgeting Can you briefl.pdf
What are the main techniques of capital budgeting Can you briefl.pdfadmaniimran
 
What are the different ways of loading a code into an AYR chipSo.pdf
 What are the different ways of loading a code into an AYR chipSo.pdf What are the different ways of loading a code into an AYR chipSo.pdf
What are the different ways of loading a code into an AYR chipSo.pdfadmaniimran
 
What are the differences between PSK and DPSKSolutionThe fol.pdf
 What are the differences between PSK and DPSKSolutionThe fol.pdf What are the differences between PSK and DPSKSolutionThe fol.pdf
What are the differences between PSK and DPSKSolutionThe fol.pdfadmaniimran
 
What are some factors used to determine whether an individual is an e.pdf
 What are some factors used to determine whether an individual is an e.pdf What are some factors used to determine whether an individual is an e.pdf
What are some factors used to determine whether an individual is an e.pdfadmaniimran
 
What are the current asset financing strategies that firms adopt Fi.pdf
 What are the current asset financing strategies that firms adopt  Fi.pdf What are the current asset financing strategies that firms adopt  Fi.pdf
What are the current asset financing strategies that firms adopt Fi.pdfadmaniimran
 
What are the areas in Transportation Engineering, where Civil Eng.pdf
 What are the areas in Transportation Engineering, where Civil Eng.pdf What are the areas in Transportation Engineering, where Civil Eng.pdf
What are the areas in Transportation Engineering, where Civil Eng.pdfadmaniimran
 

Mehr von admaniimran (20)

What is the difference to be found between surface waters and the.pdf
 What is the difference to be found between surface waters and the.pdf What is the difference to be found between surface waters and the.pdf
What is the difference to be found between surface waters and the.pdf
 
What is the area of the parallelogram (5,6) (-7,6) 5, 4 7, 4 Sol.pdf
 What is the area of the parallelogram (5,6) (-7,6) 5, 4 7, 4 Sol.pdf What is the area of the parallelogram (5,6) (-7,6) 5, 4 7, 4 Sol.pdf
What is the area of the parallelogram (5,6) (-7,6) 5, 4 7, 4 Sol.pdf
 
What is return on equity What is net profit margin What is total as.pdf
 What is return on equity What is net profit margin What is total as.pdf What is return on equity What is net profit margin What is total as.pdf
What is return on equity What is net profit margin What is total as.pdf
 
What is on a classified balance sheet What is on a classified b.pdf
 What is on a classified balance sheet What is on a classified b.pdf What is on a classified balance sheet What is on a classified b.pdf
What is on a classified balance sheet What is on a classified b.pdf
 
What is depreciation What is depreciation Solu.pdf
 What is depreciation  What is depreciation Solu.pdf What is depreciation  What is depreciation Solu.pdf
What is depreciation What is depreciation Solu.pdf
 
What industry controlled California politics in the late 1800s and .pdf
 What industry controlled California politics in the late 1800s and .pdf What industry controlled California politics in the late 1800s and .pdf
What industry controlled California politics in the late 1800s and .pdf
 
what functions were intergrates into the microfluidic chip projec.pdf
 what functions were intergrates into the microfluidic chip projec.pdf what functions were intergrates into the microfluidic chip projec.pdf
what functions were intergrates into the microfluidic chip projec.pdf
 
What does a Geothermal heating and cooling do (I appreciate it if yo.pdf
 What does a Geothermal heating and cooling do (I appreciate it if yo.pdf What does a Geothermal heating and cooling do (I appreciate it if yo.pdf
What does a Geothermal heating and cooling do (I appreciate it if yo.pdf
 
What does the acronym GIS stands for List any 4 (four) componentss.pdf
 What does the acronym GIS stands for  List any 4 (four) componentss.pdf What does the acronym GIS stands for  List any 4 (four) componentss.pdf
What does the acronym GIS stands for List any 4 (four) componentss.pdf
 
What distribution does the F distribution approach as the sample .pdf
 What distribution does the F distribution approach as the sample .pdf What distribution does the F distribution approach as the sample .pdf
What distribution does the F distribution approach as the sample .pdf
 
What communication skills do business value What communication.pdf
 What communication skills do business value What communication.pdf What communication skills do business value What communication.pdf
What communication skills do business value What communication.pdf
 
What assumptions are made about the population from which indepen.pdf
 What assumptions are made about the population from which indepen.pdf What assumptions are made about the population from which indepen.pdf
What assumptions are made about the population from which indepen.pdf
 
What are the various sources of conflict and politics that have p.pdf
 What are the various sources of conflict and politics that have p.pdf What are the various sources of conflict and politics that have p.pdf
What are the various sources of conflict and politics that have p.pdf
 
What are the two new civilian codes added to the modernized satellite.pdf
 What are the two new civilian codes added to the modernized satellite.pdf What are the two new civilian codes added to the modernized satellite.pdf
What are the two new civilian codes added to the modernized satellite.pdf
 
What are the main techniques of capital budgeting Can you briefl.pdf
 What are the main techniques of capital budgeting Can you briefl.pdf What are the main techniques of capital budgeting Can you briefl.pdf
What are the main techniques of capital budgeting Can you briefl.pdf
 
What are the different ways of loading a code into an AYR chipSo.pdf
 What are the different ways of loading a code into an AYR chipSo.pdf What are the different ways of loading a code into an AYR chipSo.pdf
What are the different ways of loading a code into an AYR chipSo.pdf
 
What are the differences between PSK and DPSKSolutionThe fol.pdf
 What are the differences between PSK and DPSKSolutionThe fol.pdf What are the differences between PSK and DPSKSolutionThe fol.pdf
What are the differences between PSK and DPSKSolutionThe fol.pdf
 
What are some factors used to determine whether an individual is an e.pdf
 What are some factors used to determine whether an individual is an e.pdf What are some factors used to determine whether an individual is an e.pdf
What are some factors used to determine whether an individual is an e.pdf
 
What are the current asset financing strategies that firms adopt Fi.pdf
 What are the current asset financing strategies that firms adopt  Fi.pdf What are the current asset financing strategies that firms adopt  Fi.pdf
What are the current asset financing strategies that firms adopt Fi.pdf
 
What are the areas in Transportation Engineering, where Civil Eng.pdf
 What are the areas in Transportation Engineering, where Civil Eng.pdf What are the areas in Transportation Engineering, where Civil Eng.pdf
What are the areas in Transportation Engineering, where Civil Eng.pdf
 

Kürzlich hochgeladen

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Kürzlich hochgeladen (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Using a loop structure, write function myPow (double x, int y) which .pdf

  • 1. Using a loop structure, write function myPow (double x, int y) which calculates x^y. Write a C++ program that uses myPow() to calculate the following: z = x^4 + 2x^3 + 6x - 2 + 2/x^2 + 3/x^3 where x is entered by the user Sample output: Enter x: 2 z = 42.875 Press any key to continue ... Modify your program such that the myPow(double x, int y) function can handle the case when y is negative. Test your program using the following equation: k = x^-3 + 2x^-2 Sample output: Enter x: 2 z = 42.875 k = 0.625 Solution #include #include using namespace std; double myPow(double x, int y) { // base case if (y == 0) return 1; // base case else if (y == 1) return x; int power; if(y > 0) power = y; else power = -y; int copypower = power; // positive x double input; if(x > 0) input = x; else input = -x; // intially answerr is x double answer = input;
  • 2. int temp = 1; while (power / 2 > 0) { answer = answer * answer; power = power / 2; temp = temp * 2; } // recursively calling the function answer = answer * myPow(input, copypower - temp); // to handle negative answer if (x < 0 && y % 2 == 1) answer = -answer; // to handle negative power if (y < 0) answer = 1 / answer; return answer; } // Driver program to test above functions int main() { double x; printf("Enter x: "); scanf("%lf",&x); double z = myPow(x,4) + 2*myPow(x,3) + 6*x - 2 + (2/myPow(x,2)) + (2/myPow(x,3)); double k = myPow(x,-3) + 2*myPow(x,-2); printf("z: %0.3lf ",z); printf("k: %0.3lf ",k); return 0; }
  • 3. /* output: Enter x: 2 z: 42.750 k: 0.625 */