Assignment Factorial and Power

Assignment: Factorial and Power

This assignment is to help you review C-programming for Numerical Programming.

Do NOT use Chat-GPT and online materials.

You can do it all by yourself !!


Problem

Q1. Create power(x,N) function that returns

y=xNy=x^N
double power(double x, int N);

Q2. Create factorial(x) function that returns

y=x!=x(x1)(x2)...1y=x! = x(x - 1)(x - 2)...1
double factorial(int x);

Hint

image


Preparation

You must complete the following tutorials first.

Procedure

  1. Create a new project “ Assignment_powerNfactorial” with Visual Studio Community.

    It should be under the designated folder of \NP\assignment\

    ...\source\repos\NP\assignment\Assignment_powerNfactorial\

  2. Create a new source file

  1. Fill in the definitions of power() and factorial()

  1. Run the code and validate the answers

  1. Capture the output window and paste in the report

Submit

Submit as “Assignment_powerNfactorial_yourName.zip” that includes

1) Source code

  • submit only “Assignment_powerNfactorial_yourName.cpp”

2) Report: “Assignment_powerNfactorial_yourName.pdf”


Code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


double factorial(int _x);
double power(double _x, int N);

int main(int argc, char* argv[])
{

	double x = 2.5;
	int N = 5;
	double y1 = 1;
	double y2 = 1;

	y1 = power(x, N);
	y2 = factorial(N);


	printf("\n\n");
	printf("=======================================\n");
	printf("    power(%f,%d ) Calculation   \n", x, N);
	printf("=======================================\n");
	printf("   -  My     result = %3.12f    \n", y1);
	printf("   -  Math.h result = %3.12f    \n", pow(x, N));
	printf("   -  absolute err. = %3.12f    \n", y1 - pow(x, N));
	printf("=======================================\n");

	printf("\n\n");
	printf("=======================================\n");
	printf("    factorial( %d) Calculation   \n", N);
	printf("=======================================\n");
	printf("   -  My     result = %.0f    \n", y2);
	printf("=======================================\n");


	system("pause");
	return 0;
}


// power fuction
double power(double x, int N)
{
	// [TODO] add your algorithm here
	// [TODO] add your algorithm here

	return y;
}


// factorial function
double factorial(int N)
{
	double y = 1;
	for (int k = 2; k <= N; k++)
		// [TODO] add your algorithm here
	
	return y;
}

Last updated

Was this helpful?