Question

In: Computer Science

PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT)...

PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT) AND CALCULATES THE NTH FIBONACCI SUM USING RECURSION.

EXAMPLE: OLD VERSION 0,1,1,2,3.. USING USER INPUT: 0, N, M ,N+M, (N+M)+M

PART 2: WRITE THE SAME PROGRAM USING USER INPUT BUT USING A LOOP IN STEAD OF RECURSION.

PYTHON

Solutions

Expert Solution

I have written the program using PYTHON PROGRAMMING LANGUAGE.

USING RECURSION :

OUTPUT :

CODE :

#function definition for Fibonacci

def Fibonacci(N,M,Nthposition):

if(N<=0):

return "InvalidInput" #in case of invalid input

elif(Nthposition-1 == 0):

print(N,end=" ")

return N

else:

print(N,end=" ")

#calling Fibonacci function recursively

return Fibonacci(M,N+M,Nthposition-1)

if(__name__ == "__main__"):

#taking user input for N and M values

N = int(input("Enter an integer value for N : \t"))

M = int(input("Enter an integer value for M : \t"))

print("\nFibonacci Series \n0",end=" ")

#calling Fibonacci function

result = Fibonacci(N,M,N)

#To validate the result and printing the reuslt on the console

if(result == "InvalidInput"):

print("Invalid Input !!!")

else:

print("\n\nOUTPUT : \n{:d}th Fibonacci SUM is {:d}".format(N,result))

USING LOOP :

OUTPUT :

CODE :

if(__name__ == "__main__"):
#taking user input for N and M values
N = int(input("Enter an integer value for N : \t"))
M = int(input("Enter an integer value for M : \t"))

#calling Fibonacci function
NOriginal = N
Nthposition = N
#in case if N value is negative or 0
if(N<=0):
    print("Invalid Input !!!")
else:
    print("\nFibonacci Series \n0",end=" ")
    #while loop to calculate the fibnocci Series
    while(Nthposition - 1 !=0):
      print(N,end=" ")
      N , M = M , N+M
      Nthposition -= 1#decreasing the Nthposition by value 1
    print(N,end=" ")
    #to print the result on the console
    print("\n\nOUTPUT : \n{:d}th Fibonacci SUM is {:d}".format(NOriginal,N))

Thanks..


Related Solutions

Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
Question 1: 5pts Write a program to receive two integers as user input and then print:...
Question 1: 5pts Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: 10pts An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is...
Question 1: Write a program to receive two integers as user input and then print: Sum...
Question 1: Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is compounded monthly....
Write a program, ArrayRange, that asks the user to input integers and that displays the difference...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display...
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
Using Java, write a program that takes in two integers from the keyboard called m and...
Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value between 1 and 3 (where 1=paper, 2=scissor, and 3=rock). This input should be passed into a string function called player_RPS(), and returns a string value indicating the selection of paper, scissors, or rock (as mentioned above). Next, the returned string value, along with a generated input from the computer, should be passed into a void function called RPS_comparison(), and determines whether the user’s input...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT