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
KonstantinChe [14]
2 years 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:
Stolb23 [73]2 years ago
8 0

Answer:

Here are the programs. I am writing C++ and Python programs:

a)

C++

#include<iostream>   

using namespace std;    

int factorial(int number)  {  

   if (number == 0)  

       return 1;  

   return number * factorial(number - 1);  }    

int main()  {  

   int integer;

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

   cin>>integer;

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

Python:

def factorial(number):  

   if number == 0:  

       return 1

   return number * factorial(number-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 has a method factorial that takes numbers as parameter and computes the factorial of that number by using recursion. For example if the number = 3

The base case is  if (number == 0)

and

recursive is return number * factorial(number - 1);    

Since number = 3 is not equal to zero so the method calls itself recursively in order to return 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)]

Now at factorial(0) the base condition is reached as number==0 So factorial(0) returns 1

Now the output is:

3 * 2 * 1* 1

return 6

So the output of this program is

Factorial of 3 is 6

b)

The method estimate_e takes a number i.e. num as parameter which represents the term and estimates the value of the mathematical constant e

The for loop iterates through each term. For example num = 3

then  

e = e + 1/factorial(i);  

The above statement calls works as:

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

since the number of terms is 3

e is initialized to 1

i  is initialized to 1

So the statement becomes:

e = 1 + 1/factorial(1)

factorial function is called which returns 1 since factorial of 1 is 1 So,

e = 1 + 1/1

e = 2

Now at next iteration at i = 2 and e = 2

e = 2 + 1/factorial(2)

e = 2 + 1/2

e = 2.5

Now at next iteration at i = 3 and e =3

e = 3 + 1/factorial(3)

e = 3 + 1/6

e = 3.16666

So the output is:

e: 3.16666

c)

The program computes the sum of series using formula:

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

The for loop iterates till the number of terms. Lets say in the above formula x is 2 and number of terms is 3. So the series become:

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

So number = 2

terms = 3

series =1

sumSeries = 1

i = 1

The statement series = series * number / (float)i; works as following:

series = 1 * 2 /1

series = 2

sumSeries = sumSeries + series;

sumSeries = 1 + 2

sumSeries = 3

At next iteration: i=2, series =2 , sumSeries =3

series = 2 * 2/2

series = 2

sumSeries = 3 + 2

sumSeries = 5

Now the loop breaks as i=3

So output returns the value of sumSeries i.e. 5

Output:

e^x: 5

You might be interested in
Write a program that reads students’ names followed by their test scores. The program should output each student’s name followed
bekas [8.4K]

Answer:

See explaination

Explanation:

#include <iostream>

#include <iomanip>

#include <fstream>

#include <string>

using namespace std;

void readStudentsData();

void levels(int&,int&,int&);

int main()

{

string studentsName[50];

int grade[50];

int highestScore = 0;

int belowAverage = 50;

int countStudents = 0;

int sum = 0;

int score[5]=

double average = 0;

ifstream infile;

ofstream outfile;

void readStudentsData();

infile.open("c:\\Data.txt");

int numOfStudents = 0;

infile >> studentsName[numOfStudents] >> grade[numOfStudents];

While (infile && numOfstudents < 50)

{

numOfStudents++;

infile >> studentsName[numOfStudents] >> grade[numOfStudents};

}

if (!infile)

{

cout << "File does not open." << endl;

return 1;

}

outfile.open("c:\\average.out");

outfile << fixed << showpoint;

outfile << setprecision(2);

infile >> studentsName;

infile >> score1 >> score2 >> score3 >> score4 >> score5 >>;

outfile << left << setw(20) << "Students Name"

<< setw(5) << "Score 1" << setw(5) << "Score 2"

<< setw(5) << "Score 3" << setw(5) << "Score 4"

<< setw(5) << "Score 5" << setw(5) << "Grade"

<< setw(5) << "Average" << setw(5) << "Below average"

<< setw(5) << "Highest" << endl;

if (average >= 90)

cout << "A" << endl;

else if (average >= 80)

cout << "B" << endl;

else if (average >= 70)

cout << "C" << endl;

else if (average >= 60)

cout << "D" << endl;

else

cout << "F" << endl;

}

average = static_cast<double>(score1+score2+score3+score4+score5) /5;

void level(int &highestScore,int &belowAverage,int &sum);

{

int level,high = 0,low = 17;

infile >> level;

while(level!=-1)

{

sum = sum + level;

if (level > high)

{

high = level;

infile >> level;

}

else

if (level < low)

{

low = level;

infile >> level;

}

else

if infile >> level;

}

outfile << ' ' << high;

else

if (high > highestScore)

highestScore=high;

else

if (low < belowAverage)

belowAverage=low;

}

do

{

cout << "Students Name: ";

cin >> name

cout << "Input score: " << endl;

cin >> score1>> score2>> score3>> score4>> score5;

cout << Students Name << Highest score);

}

While (highest score !=0);

infile.close();

outfile.close();

return 0;

}

5 0
2 years ago
Robin wants her presentation to move from one slide to another with special motion effects. Which option should Robin use?
topjm [15]
If this is in power point, then she should use the <em>transitions </em>tab on the ribbon. =)
3 0
2 years ago
Read 2 more answers
In cell M2, enter a formula using a nested IF function as follows to determine first if a student has already been elected to of
aleksley [76]

Answer:

Following are the code to this question:

code:

=IF(EXACT(I2,"Yes"),"Elected",IF(EXACT(K2,"Yes"),"Yes","No"))

Explanation:

In the given the data is not defined so we explain only  the above code, but before that, we briefly define working of if the function that can be defined as follows:

  • The If() function, is used only when one of the logical functions and its value must return the value true. or we can say it only return a true value.
  • In the above function, a column "Elected" is used that uses other column values to check this column value is equal if this condition is true it will return "yes" value.

8 0
2 years ago
#You may modify the lines of code above, but don't move them! #When you Submit your code, we'll change these lines to #assign di
Musya8 [376]

Answer:

The solution code is written in Python:

  1. mystery_string = "Programming"
  2. output = ""
  3. for x in mystery_string:
  4.    output += x  
  5.    print(output)

Explanation:

Firstly, create a variable mystery_string to hold a random string (Line 1).

Create an output variable to hold an output string (Line 2).

Create a for-loop to traverse the mystery_string character by character (Line 4). In the iteration, get a character from the mystery_string, and concatenate it with output string (Line 5). Print the output string (Line 6) before proceed to the next iteration.

8 0
2 years ago
Kris, an IT manager at Park Infosystems, is handling four projects simultaneously. Each project has loaned and shared resources
Natalija [7]

Answer:

many-to-many relationship

Explanation:

They are different types of relationship between projects. A many-to-many relationship occurs when a row in a table associates with many related rows in another table. For example the relationship between an employee and a project, An employee can work on many projects and a project can have many employees working on it.

8 0
2 years ago
Read 2 more answers
Other questions:
  • Name an analog quantity other than temperature and sound.
    14·1 answer
  • A storyboard is essentially an outline of a web page's content and design<br> -True or False
    8·2 answers
  • Assume that a kernel is launched with 1000 thread blocks each of which has 512 threads. If a variable is declared as a shared me
    6·1 answer
  • You decide to buy some stocks for a certain price and then sell them at anotherprice. Write a program that determines whether or
    11·1 answer
  • ________ is a free online service that enables a user to contact a cell phone number to hear who answers the telephone without i
    5·1 answer
  • Suppose that, even unrealistically, we are to search a list of 700 million items using Binary Search, Recursive (Algorithm 2.1).
    11·1 answer
  • Why do agriculture and natural resource systems vary from state to state?
    8·1 answer
  • In the next five years there are expected to be over _____ unfilled jobs in the US in computer science.
    10·1 answer
  • How to code 2.9.5: Four colored triangles {Code HS}
    10·1 answer
  • You work at a computer repair store. A customer is having trouble with their hard drives. The computer is not recognizing all th
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!