Question

Write a working C program that will do the following: 1.Include your name, Lab Test number,...

Write a working C program that will do the following:

1.Include your name, Lab Test number, and studentId as comments in the first lines of theprogram

2.User is first prompted to enter an integer number between 1 and 20, to be stored asvariable named number. If the number is outside the range 1-20, the program will end.

3.If the number is within the allowed range, variable number is passed to function multF().The multF() function computes and returns the result of: 1 x 1/2 x 1/3 x .... x 1/n using aloop, where n is a number entered by the user.

4.The result is then returned to main() and displayed on the screen (from main()!).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

/*
* Write Your Name, LabTestNumber, studentId here
*/

#include <stdio.h>

// Function to compute 1 x 1/2 x 1/3 x .... x 1/n using aloop where n is passed perimeter
double multF(int number) {
double value = 1;
// loop i from 1 to number
for(int i = 1; i<= number; i++) {
value = value * 1/i;
}
return value;
}

int main()
{
int number;
printf("Enter a number between 1 and 20: ");
// scanf ask user to enter the number
scanf("%d",&number);
// compute only when number is between 1 and 20
if(number >= 1 && number <= 20) {
// %g displays the computed value in either decimal representation or scientific notation.
printf("%g",multF(number));
}
return 0;
}
Screenshot of code:

} main.c 1- /* 2 * Write Your Name, LabtestNumber, studentId here 3 */ 4 5 #include <stdio.h> 6 7 // Function to compute 1 x

Output:

Enter a number between 1 and 20: 5 0.00833333 ... Program finished with exit code 0 Press ENTER to exit console. I

Enter a number between 1 and 20: 15 7.64716e-13 Program finished with exit code 0 Press ENTER to exit console. I

Note: If you want your output to be displayed as decimal representation only not in scientific notation then In line 26 instead of using "%g" use "%0.25f" or "%0.30f". "%0.25f" will display 25 digits after decimal point and "%0.30f" will display 30 digits after decimal point.

If you're still having any doubt then please feel free to ask in the comment section.

Add a comment
Know the answer?
Add Answer to:
Write a working C program that will do the following: 1.Include your name, Lab Test number,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a working C program that will do the following: 1. Include your name, Lab Test...

    Write a working C program that will do the following: 1. Include your name, Lab Test number, and studentId as comments in the first lines of the program 2. User is first prompted to enter an integer number between 1 and 20, to be stored as variable named number. If the number is outside the range 1-20, the program will end. 3. If the number is within the allowed range, variable number is passed to function multF(). The multF() function...

  • Write a working C program that will do the following: 1. Include your name, Lab Test...

    Write a working C program that will do the following: 1. Include your name, Lab Test number, and student id as comments in the first lines of the program 2. User is first prompted to enter an integer number between 1 and 20, to be stored as variable named number. If the number is outside the range 1-20, the program will end. 3. If the number is within the allowed range, variable number is passed to function multF(). The multF()...

  • Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...

    Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is your actual student ID number) that has one integer input (N) and one double input (x) and returns a double output S, where N S = n 0 and X2 is given by 0 xeVn n 0,1 Хл —{2. nx 2 n 2 2 m2 x2 3 (Note: in the actual quiz, do not expect a always, practice...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Homework 1 - Simple Array Handling Write a working C++ program to ask the user for...

    Homework 1 - Simple Array Handling Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently, each grade is provided, one at a time. Allow for a maximum of...

  • Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program...

    Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program should prompt the user to enter a number n. The program will compute and display sum based on the series defined above. firstPrime: is 2 secondPrime: the first prime number after 2 thirdPrime: the third prime number …. nth prime: the nth prime number Your program must be organized as follows: int main() { //prompt the user to enter n //read n from the...

  • Write a C program to do the following: 1. Write a C function named find that...

    Write a C program to do the following: 1. Write a C function named find that receives a one-dimensional array of type character named arr and its size of type integer. After that, the function must select a random index from the array. The function must find if the character stored in the randomly selected index comes before all the characters to its left alphabetically. Finally, the function prints to the screen the element if the element meets the condition...

  • Exercise #1: Write a C program that contains the following steps (make sure all variables are...

    Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a...

  • In the last lab (Lab 5), you wrote a program named seriesFunction.c to compute the value...

    In the last lab (Lab 5), you wrote a program named seriesFunction.c to compute the value of S(x) for the following series. Your program prompted user to enter two values, x and n. You defined a function to compute the sum of this series. Your function took two arguments, x and n; and returned the sum of the series. S(x) = x + (x+1) + (x+2) + (x+3) + (x+4) + + (x + n -1) + (x + n)...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT