Question

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 computes and returns the result of: 1 x 1/2 x 1/3 x …. x 1/n using a loop, 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

In case of any queries,please comment. I would be very happy to assist all your queries.Please give a Thumps up if you like the answer.

Program

#include<stdio.h>

//Function to calculate
double multF(int n)
{
double res=1;
for (int i=1; i<=n; i++) //Loop
res =res*((double)1/(double)i);
return res;
}

int main()
{
double result;
int number;
  
   //Reading input
printf ("Enter a number between 1 and 20 :");
scanf ("%d",&number);
  
   if((number<=20)&&(number>=1))
   {
   //Calculating series
result=multF(number);

   //Printing results
   printf("Result = %lf\n",result);
   }
   else
       printf("Wrong Input !!!\n");
   return 0;
}

Output

Enter a number between 1 and 20 :21
Wrong Input !!!


Enter a number between 1 and 20 :0
Wrong Input !!!


Enter a number between 1 and 20 :21
Wrong Input !!!


Enter a number between 1 and 20 :1
Result = 1.000000


Enter a number between 1 and 20 :2
Result = 0.500000


Enter a number between 1 and 20 :3
Result = 0.166667

Program Screenshot

#include<stdio.h> //Function to calculate double mult(int n) { double res=1; for (int i=1; i<=n; i++) // Loop res =res*((doub

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...
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 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:...

  • 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()...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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....

  • 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)...

  • 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...

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