1answer.
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
denis-greek [22]
3 years ago
15

. Write a recursive function names factorial to compute the factorial of the parameter. Also write the main function, where you

ask the user to input a non-negative integer number. Then call the function factorial and display the result.
Computers and Technology
1 answer:
r-ruslan [8.4K]3 years ago
4 0

Answer:

Following are the code in c language

#include <stdio.h> // header file

long int factorial(int n); // declaration of factorial function

int main() // main function

{

   int n; // variable declaration

   printf("Enter a positive integer: ");

   scanf("%d", &n); // input number

   while(n<0) // checking the condition if number is negative

{

    printf("please enter a positive number:");

    scanf("%d", &n);

}

   printf("Factorial of %d = %ld", n, factorial(n)); // calling  factorial function

   return 0;

}

long int factorial(int n1) // recursive definition of factorial

{

   if (n1 >= 1)

       return n1*factorial(n1-1);

   else

       return 1;

}

Explanation:

In this program, it ask for a positive number input. if the value enter by user is negative then it again ask for positive input i.e positive number. Then it calls the recursive function of factorial. the recursive function factorial calculate the factorial recursively. suppose user input 4 then it goes to the if  part of program i.e return n*factorial(n-1); that means return 4*factorial(3) again recursive function call itself .this process repeated until it meets the base condition.  when a base condition meets, it return factorial of the given number.

output

Enter a positive integer: 5

factorial of 5=120

Enter a positive integer: -8

please enter a positive number:4

factorial of 4=24

You might be interested in
The changing of values for an object through a system is represented by the _____. (Points : 6) communication diagram
Fed [463]

Answer:

The correct option is communication diagram

Explanation:

The communication diagram represents the change of values for an item by a system.

A communication diagram is an expansion of the diagram of objects showing the objects together with the texts traveling from one to another. Besides the connections between objects, the communication diagram demonstrates the messages that the objects send to one another.

The correct option is a) communication diagram

8 0
2 years ago
​ A wire service is also known as a ____.
charle [14.2K]
The answer is C press agency
4 0
3 years ago
When saving a document or drawing, you determine the destination folder in which the file will be saved by?
bekas [8.4K]

making a selection in the Save in: box.<span>

</span>
8 0
2 years ago
"assignment is to create a program that will read a value from an array, and then place this value in another array with the loc
White raven [17]

Answer:

#include <iostream>//including libraries

using namespace std;

int main()

{

int arr[6] = { 0,1,2,3,4,5 };//make sure size of arr is 1 less than secArr

int secArr[7];//second array (1 element bigger)

for (int i = 0;i < 6;i++)//looping through each element (6 times)

{

 secArr[i + 1] = arr[i];//transferring elements to second array and shifting by 1 cell

 cout << secArr[i + 1] << endl;//printing elements of second array

}

return 0;//terminating program

}

Explanation:

The array size can range from any number. just make sure to keep arr one less than secArr. This is because we need the room for the extra element. This task is to help you understand how array work and how to parse through them using loops. For loops are the best for this task because even if you think intuitively, they work for as long as there are items in the array. and you can define the size yourself.

5 0
2 years ago
Write a program that uses the function strcmp() to compare two strings input by the user. The program should state whether the f
Andru [333]

user_str1 = str ( input ("Please enter a phrase: "))

user_str2 = str ( input("Please enter a second phrase: "))

def strcmp (word):

user_in1 = int (len(user_str1))

user_in2 = int (len(user_str2))

if user_in1 > user_in2:

return "Your first phrase is longer"

elif user_in1 < user_in2:

return "Your second phrase is longer"

else:

return "Your phrases are of equal length"

3 0
3 years ago
Other questions:
  • What are the elements of an autobiographical narrative similar to those of a short story?
    7·1 answer
  • Which of the following is NOT true about RMI? RMI uses the socket connection, including opening and closing the socket. RMI allo
    8·1 answer
  • The mac group does not replace the primary functions of eocs or other dispatch organizations. True or False
    8·1 answer
  • Higher-speed Ethernet technologies use an electronic device known as a Hub rather than a switch True/False
    15·1 answer
  • In Scheme, the form (symbol-length? 'James) will return: Group of answer choices 0 5 6 error message
    11·1 answer
  • True/False: On the piano, middle C is located to the left of the 2 black keys in the middle.
    9·2 answers
  • KDS Company has 3 departments: 1. Dept X 2. Dept Y 3. Dept Z. Design a program that will read as input each department's sales f
    6·1 answer
  • Claire wants to use a conditional statement in JavaScript that provides a block of statement to be executed if the condition is
    7·2 answers
  • Which of the following is not a bus type A. Address bus B. Data bus C. Memory bus D. Control bus ​
    7·2 answers
  • C++
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!