answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
professor190
13 days ago
10

The factorial of a nonnegative integer n is written n ! (pronounced "n factorial") and is defined as follows: n ! = n · (n - 1)

· (n - 2) · … · 1 (for values of n greater than or equal to 1) and n ! = 1 (for n = 0). For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120.
a) Write a program that reads a nonnegative integer and computes and prints its factorial.
b) Write a program that estimates the value of the mathematical constant e by using the formula:
e = 1 + 1/1! +1/2! + 1/3! +.......
c) Write a program that computes the value of by using the formula
e^x = 1 + x/1! + x^2/2! + x^3/3! +.......
Computers and Technology
1 answer:
Harlamova29_29 [932]13 days ago
8 0
Here are the programs. I have written C++ and Python scripts:

a)

C++

#include<iostream>  

using namespace std;  

int factorial(int num)  {  

   if (num == 0)  

       return 1;  

   return num * factorial(num - 1);  }    

int main()  {  

   int integer;

   cout<<"Enter a non negative integer: ";

   cin>>integer;

   cout<< "Factorial of "<< integer<<" is "<< factorial(integer)<< endl;  }

Python:

def factorial(num):  

   if num == 0:  

       return 1

   return num * factorial(num-1)  

integer = int(input("Enter a non negative integer: "))  

print("Factorial of", integer, "is", factorial(integer))

b)

C++

#include <iostream>  

using namespace std;

double factorial(int number) {  

if (number == 0)  

 return 1;  

return number * factorial(number - 1); }  

 

double estimate_e(int num){

    double e = 1;

    for(int i = 1; i < num; i++)

     e = e + 1/factorial(i);

     cout<<"e: "<< e; }  

 

int main(){

int term;

cout<<"Enter a term to evaluate: ";

cin>>term;

estimate_e(term);}

Python:

def factorial(number):  

   if number == 0:  

       return 1

   return number * factorial(number-1)  

def estimate_e(term):

   if not term:

       return 0

   else:

       return (1 / factorial(term-1)) + estimate_e(term-1)

number = int(input("Enter how many terms to evaluate "))

print("e: ", estimate_e(number))

c)

C++

#include <iostream>

using namespace std;

int main(){

   float terms, sumSeries, series;

   int i, number;

   cout << " Input the value of x: ";

   cin >> number;

   cout << " Input number of terms: ";

   cin >> terms;

   sumSeries = 1;

   series = 1;

   for (i = 1; i < terms; i++)      {

       series = series * number / (float)i;

       sumSeries = sumSeries + series;     }

   cout << " The sum  is: " << sumSeries << endl;  }  

Python    

def ePowerx(number,terms):

   sumSeries = 1

   series =1

   for x in range(1,terms):

       series = series * number / x;

       sumSeries = sumSeries + series;

   return sumSeries    

num = int(input("Enter a number: "))

term=int(input("Enter a number: "))

print("e^x: ",ePowerx(num,term))

Explanation:

a)

The program includes a factorial method that takes a number as an argument and calculates its factorial using recursion. For instance, if number = 3

The base case occurs at  if (number == 0)

and the recursion is handled with return number * factorial(number - 1);  

With number = 3 not equaling zero, the function calls itself recursively to get the factorial of 3

return 3* factorial(3- 1);

3 * factorial(2)

3* [2* factorial(2- 1) ]

3 * 2* [ factorial(1)]

3 * 2 * [1* factorial(1- 1) ]

3 * 2 * 1* [factorial(0)]

At this point at factorial(0), the base condition is satisfied as number==0, so factorial(0) returns 1

The resulting output is:

3 * 2 * 1* 1

yielding 6

So, the final program output will be

Factorial of 3 is 6

b)

The estimate_e method takes a number, termed as num, which signifies the term to estimate the mathematical constant e

The for loop extends through each term. For example, if num is set to 3

Then the core statement:

e = e + 1/factorial(i);  

The preceding calculation works as:

e = 1 + 1/1! +1/2!

Since the term count is 3

Initially, e is set to 1

i is initialized at 1

Inserting this into the calculation gives us:

e = 1 + 1/factorial(1)

The factorial function computes and returns 1, as the factorial of 1 is 1. Thus,

e = 1 + 1/1

This results in e = 2

Proceeding to the next iteration, where i = 2 and e = 2, we calculate e = 2 + 1/factorial(2)

Thus, e = 2 + 1/2 results in e = 2.5

Following to the next iteration with i = 3, we have e = 3 + 1/factorial(3)

This yields e = 3 + 1/6 resulting in approximately e = 3.16666

Therefore, the output is:

e: 3.16666

c)

This program calculates the sum of a series based on the formula:

e^x = 1 + x/1! + x^2/2! + x^3/3! +...

The for loop iterates according to the number set for terms. Assuming x is 2, and the number of terms is set to 3, the series would read:

e^2 = 1 + 2/1! + 2^2/2!

In this setup: number = 2 and terms = 3

Initial values for series and sumSeries are both 1

Starting with i equal to 1, the update statement series = series * number / (float)i; applies as follows:series = 1 * 2 /1 results in series = 2

Then, for sumSeries, we have sumSeries = sumSeries + series; Outputs sumSeries as 1 + 2, yielding 3

Continuing to the next iteration: i=2, with series = 2 and sumSeries = 3, we recalculate as series = 2 * 2/2 imposing series = 2 again. Thus, we find: sumSeries = 3 + 2 giving a final sumSeries value of 5

After the loop concludes, the result shows the value of sumSeries, leading finally to the output value of 5
You might be interested in
6. Write pseudo code that will perform the following. a) Read in 5 separate numbers. b) Calculate the average of the five number
zubka84 [942]

Answer:

Pseudo CODE

a)

n= Input “Enter 5 integer values”

b)

sum=0.0

For loop with i ranging from 0 to 5

Inside loop sum=n[i]+sum

Outside loop avg=sum/5

Print avg

c)

small=n[0] # assume initial number is the smallest

large=n[0] # assume initial number is the largest

For loop with i ranging from 0 to 5

Inside loop if n[i]<small # check if current number is smaller than small

Inside if Then small=n[i]

Inside loop if n[i]>large # check if current number is larger than large

Inside if then large=n[i]

Print small

Print large

d)

print avg

print small

print large

8 0
1 month ago
Write a loop to populate the list user_guesses with a number of guesses. The variable num_guesses is the number of guesses the u
oksian1 [797]

Response:

num_guesses = int(input())

user_guesses = []

for i in range(num_guesses):

    x = int(input())

    user_guesses.append(x)

   

print(user_guesses)

Clarification:

This solution is given in Python

The initial line requests the user to input a number of guesses

num_guesses = int(input())

This line establishes an empty list

user_guesses = []

Inside this loop, each guess is taken from the user

for i in range(num_guesses):

In this line, each guess input by the user is processed

    x = int(input())

This adds the input to the list

    user_guesses.append(x)

Finally, this prints the collected user guesses    

print(user_guesses)

6 0
18 days ago
Define a method calcPyramidVolume with double data type parameters baseLength, baseWidth, and pyramidHeight, that returns as a d
Rzqust [894]

Answer:

The C++ method is defined as follows:

double calcPyramidVolume(double baseLength, double baseWidth, double pyramidHeight){

   double baseArea = calcBaseArea(baseLength, baseWidth);

   double volume = baseArea * pyramidHeight;

   return volume;    

}

Explanation:

This establishes the calcPyramidVolume method

double calcPyramidVolume(double baseLength, double baseWidth, double pyramidHeight){

This invokes the calcBaseArea method to derive the base area of the pyramid

   double baseArea = calcBaseArea(baseLength, baseWidth);

This calculates the volume based on the base area

   double volume = baseArea * pyramidHeight;

This yields the calculated volume

   return volume;  

}

Refer to the attached document for the complete program that includes all required methods.

5 0
24 days ago
java methods Write a program whose input is a character and a string, and whose output indicates the number of times the charact
oksian1 [797]

Answer:

This is the JAVA code:

import java.util.Scanner; // to obtain user input

//class designed to count occurrences of a character within a string

public class CharacterCounter

//method that calculates how many times a character appears within a string

{ public static int CountCharacter(String userString, char character)

   {  

       int counter = 0;   //keeps track of the number of times the character appears in the string

for (int i=0; i<userString.length(); i++) //iterates over every character in the string

       { // if the character aligns with the one sought in the string

           if (userString.charAt(i) == character)

           counter++;  // increments the character count variable

       }  

       return counter;     } // returns the count of the character found in the string

public static void main(String[] args) {

    Scanner scanner = new Scanner(System. in);// accepts user input

    String string;

    char character;

       System.out.println("Enter a string"); // prompts the user for a string

       string = scanner.nextLine(); //captures the input string

       System.out.println("Enter a character");

       // requests the character input from the user

       character = scanner.next().charAt(0); //captures and reads the character

       System.out.println("number of times character "+character + " appears in the string " + string +": " + CountCharacter(string, character));  }  }

// calls CountCharacter method to display the frequency of a character in the input string

Explanation:

The CountCharacter() method contains a loop where index variable i traverses through each character of the string, incrementing the counter variable by 1 each time the sought character matches. This indicates that the counter maintains the tally of occurrences of the character in the string. The loop terminates when i exceeds the string length, which means the full string has been scanned for the specified character. Finally, the count variable yields the number of times the character was found in the string. The main() function receives user inputs for a string and a character to search for within that string. It subsequently invokes the CountCharacter() function to determine how many times the designated character appears in the specified string, displaying the result on the screen.

The program's output is provided in the attached screenshot.

5 0
1 month ago
Write measurable performance objectives.Suppose that a trainer has identified as a generalgoal for a training module,"Able to fo
8_murik_8 [892]

Response:

Damon ComSci 037-0945 Activity 11-3 Formulate measurable performance objectives.... Imagine a trainer has set a broad aim for a training module, saying, “Able to format printed output in accordance with a specification sheet.” First, revise this goal statement to specify a quantifiable performance objective.

Explanation:

6 0
24 days ago
Other questions:
  • 3.14 LAB: Input and formatted output: Caffeine levels A half-life is the amount of time it takes for a substance or entity to fa
    9·1 answer
  • This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The arrow dimensions are defined by
    15·1 answer
  • An author is preparing to send their book to a publisher as an email attachment. The file on their computer is 1000 bytes. When
    6·1 answer
  • Write a sequence of statements that create a file named "greeting" and write a single line consisting of "Hello, World!" to that
    5·1 answer
  • Splunk In most production environments, _______ will be used as the source of data input?
    12·1 answer
  • Tag groups can be nested up to ____ levels deep, with up to _______ tag subgroups under a single parent.
    14·1 answer
  • Consider the following skeletal C program: void fun1(void); /* prototype */ void fun2(void); /* prototype */ void fun3(void); /*
    11·1 answer
  • You work for a car rental agency and want to determine what characteristics are shared among your most loyal customers. To do th
    10·1 answer
  • Henry, a graphic artist, wants to create posters. Which software should Henry use for this purpose?
    13·1 answer
  • What term best describes the grammatical rules for writing proper code? paths syntax hyperlinks declarations
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!