Question

In: Computer Science

in C programming Assume that we have a series that is find as follows: K1=1 K2=2...

in C programming

Assume that we have a series that is find as follows:

K1=1

K2=2

K3=3

K4=K1+K2+K3   

...

K(n) = K (n-1) + K(n-2) + K(n-3)

Here is the sequence:

1, 2, 3, 6, 11, 20, 37, ...

Write a code to print the Kth number in this sequence (not greater than 50) that is collected from the input (user will input a number smaller or equal to 50 and the program produce the entry in the series for that number).

Ex:

Inter an integer less than 50: 6

Output: 20

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

------------main.c----------------

#include <stdio.h>

int main()
{
   int k;
   long int k1 = 1, k2 = 2, k3 = 3;   //initialize first 3 numbers of the series

   printf("Enter an integer less than 50: ");
   scanf("%d", &k);                   //read the value of k

   long int kthNumber;                   //we have to find kth number
   if(k == 1)                           //if k == 1, kth number is k1
       kthNumber = k1;
   else if(k == 2)                       //if k == 2, kth number is k2
       kthNumber = k2;
   else if(k == 3)                       //if k == 3, kth number is k3
       kthNumber = k3;  
   else if (k > 3){                  
       long int temp;
       for(int i = 4; i <= k; i++){   //loop from i = 4 to k
           temp = k1 + k2 + k3;       //keep adding previous three numbers
           k1 = k2;                   // (k1, k2, k3) ---> (k2, k3, k1+k2+k3)
           k2 = k3;
           k3 = temp;
       }
       kthNumber = k3;                   //kth number will be in k3
   }
   printf("Output: %ld\n", kthNumber);   //print output
   return 0;
}

--------------Screenshots-------------------

----------------------Output------------------------------------

-----------------------------------------------------------------------------------------------------------------

Note: We have used long int to store kth number, because for kthNumber is very large for k > 35, and int cannot hold all the digits. We need long int data type which can hold larger integers.

-----------------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

Suppose K1 and K2 have the following distribution: Scenario Probability return K1 return K2 w(1)   ...
Suppose K1 and K2 have the following distribution: Scenario Probability return K1 return K2 w(1)    0.3 -10% 10% w(2)    0.4 0% 20% w(3)    0.3 20% -10% (a) Find the risk of the portfolio with w1 = 30% and w2 = 70%. (b) Find the risk of the portfolio with w1 = 50% and w2 = 50%. (c) Which of the portfolios above (in part (a) and (b)), has higher expected returns?
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
Consider the diprotic acid h2a with k1=1.00x10-4 and k2=1.00x10-8. find the pH and concentrations of h2a,...
Consider the diprotic acid h2a with k1=1.00x10-4 and k2=1.00x10-8. find the pH and concentrations of h2a, ha-, and a2- in (a) 0.100 M H2A (b) 0.100 M NaHA (c) 0.100 M Na2A
Consider a firm with the following production function: Q = K1/2L1/2 Assume that we are in...
Consider a firm with the following production function: Q = K1/2L1/2 Assume that we are in the short run so the capital stock is fixed at 4 units. The wage rate is $16 and the rental rate of capital is $10. Please graph the firm's short run margical cost, average variable cost, average fixed cost, and average total cost curves. Remember to label the axes and curves accurately.
k1 = k2 m1=9m^2 P1=P2 What is the relationship between P1 and P2? The momentum will...
k1 = k2 m1=9m^2 P1=P2 What is the relationship between P1 and P2? The momentum will be the same? K represents kinetic energy
A proposed mechanism for the reaction N2O5-->2NO2 +1/2O2 N2O5 k1--> NO2+NO3 <---k_1-- NO2 +NO3 --k2--> NO...
A proposed mechanism for the reaction N2O5-->2NO2 +1/2O2 N2O5 k1--> NO2+NO3 <---k_1-- NO2 +NO3 --k2--> NO +O2 +NO2 NO +NO3 - k3-->2NO2 Applying the steady-state approximation to show that the overall reaction rate is -d[N2 O5 ]/dt=k[N2 O5 ] In this process you should evaluate K in terms of K1, K_1, K2 and K3
Prove series 2, (-1/2), (2/9), (-1/8) is convergent by the alternating series test and find the...
Prove series 2, (-1/2), (2/9), (-1/8) is convergent by the alternating series test and find the number of terms required to estimate the sum of the series with an error of less than 0.05
Question 1: For the following assume you have a (2 years) time series data with the...
Question 1: For the following assume you have a (2 years) time series data with the daily price of one cryptocurrency(or multiple cryptos) for example Bitcoin. Also, you have other variables such as S&P500, VIX, transaction volume, EMWA, and also one (or more if you like) attention factors that could be searches on "bitcoin" on google, number of searches on wikipedia etc. You can add more or remove some of the variables that are mentioned if you think that some...
Question 1: For the following assume you have a (2 years) time series data with the...
Question 1: For the following assume you have a (2 years) time series data with the daily price of one cryptocurrency(or multiple cryptos) for example Bitcoin. Also, you have other variables such as S&P500, VIX, transaction volume, EMWA, and also one (or more if you like) attention factors that could be searches on "bitcoin" on google, number of searches on wikipedia etc. You can add more or remove some of the variables that are mentioned if you think that some...
For a Michaelis-Menten reaction, k1=5*107 M-1S-1, k-1=2*104 S-1 and k2=4*102 S-1. Calculate the Km and Ks...
For a Michaelis-Menten reaction, k1=5*107 M-1S-1, k-1=2*104 S-1 and k2=4*102 S-1. Calculate the Km and Ks for this reaction. Does the substrate achieve equilibrium? How would inspection of the rate constant immediately have given you the same answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT