In: Computer Science
C programming language.
**I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you!
This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is best suited for a particular function. You will have the option to use "if", "switch", "while", "do-while", and/or "for" statements for the below problem.
Labs are held in a “closed” environment such that you may ask your TA questions. Please use your TAs knowledge to your advantage. You are required to move at the pace set forth by your TA. Have a great time! Labs are a vital part to your education in CptS 121 so work diligently.
Tasks:
1. a. With your team, write a program that reads in each of the integer values in a file and determines if the sum of the integers is prime. Use functions where appropriate!
b. Determine if the sum of the individual digits, in the sum of the integers, is prime. Use functions where appropriate!
2. With your team, write a program that determines the factorial of n, represented n!, where n is entered by the user. Make sure that your program checks to see if n is greater than or equal to 0. As long as n is negative your program should continue to prompt for another value for n. Recall 0! is equal to 1 and 1! is also equal to 1. The factorial of a number such as n! is equal to n * (n – 1) * (n – 2) *… * 1. Use functions where appropriate!
3. Write a program that determines the Fibonacci number for the nth term. Recall, the first Fibonacci number is 0 and the second Fibonacci number is 1. The next number in the sequence is always determined by the sum of the previous two terms or numbers in the sequence. Fib (n) = Fib (n-1) + Fib (n-2). The n value should be entered by the user. Use functions where appropriate!
4. Write a program that generates a random number between -100 to 100 and requires that the user guesses which number was generated. If the user guesses too high, then the program should say so. If the user guesses too low, then the program should say so again. The program should continue to loop until the user guesses the correct number. If the user guesses a number outside the range of -100 to 100, then the program should just prompt the user to re-enter a number without indicating if the guess was too high or too low. Keep track of the total number of guesses taken by the user. Use nested loops to solve this problem. Use functions where appropriate!
SOLUTION FOR PART 2:-
CODE FOR THE FOLLOWING PROGRAM:-
#include <stdio.h>
int main()
{
//variables used in the program
int factorial=1,n;
//If user enter negative value of n prompt user to enter again
do{
printf("Enter the number whose factorial you want to calculate: \n");
scanf("%d",&n);
}while(n<0);
//If n is greater than 0 calculate it's factorial
if(n>0){
for(int i=1;i<=n;i++){
factorial*=i;
}
printf("The factorial of %d is %d",n,factorial);
}
//If the number is zero then print 1
else if(n==0){
printf("The factorial of %d is 1",n);
}
return 0;
}
STEPS FOR FINDING FACTORIAL:-
Our program is asking user to enter a number whose factorial user want to find, if he/she enters a negative number we will prompt them to enter a value again because factorial of negative number doesn't exsist. Then we will check whether the entered value is 0 or greater than 0 if it is 0, we will simply print 1 as its factorial, if it is greater than 0 we will calculate it's factorial using a for loop and then print it's factorial.
SCREENSHOT OF THE PROGRAM AND SAMPLE OUTPUT:-
SAMPLE OUTPUT:-
HAPPY LEARNING