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 program named Combinations.java that gets user input for two integers n and k, then...
write a program named Combinations.java that gets user input for two integers n and k, then computes and displays the value of n choose k using the efficient method of equation. you may assume that the user's input makes sense (i.e, n and k are non negative, and n is at least k). your code should work for any reasonably small non- negative values of n and k, including zero.
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 recursive ARM Assembly program that takes two integers as input and outputs the greatest...
Write a recursive ARM Assembly program that takes two integers as input and outputs the greatest common divisor. *I am using Eclipse DS-5 Community Workspace with A64 Instruction Set) Use the following algorithm: // Given two integers m and n: if (m < n) gcd(n, m) if n is a divisor of m gcd(m, n) = n else gcd (m, n) = gcd (n, m % n) Your program must be recursive. You must create a function that calls itself,...
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...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
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 that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT