1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Darya [45]
3 years ago
9

Using language c, find the nth Fibonacci, knowing that nth Fibonacci is calculated by the following formula: - If n = 1 Or n = 2

then F(n) = 1 - If n>2 then F(n) = F(n-1) + F(n-2)
Computers and Technology
1 answer:
Nina [5.8K]3 years ago
4 0

Answer:

#include <stdio.h>

int fib(int n) {

 if (n <= 0) {

   return 0;

 }

 if (n <= 2) {

   return 1;

 }

 return fib(n-1) + fib(n-2);

}

int main(void) {

 for(int nr=0; nr<=20; nr++)

   printf("Fibonacci %d is %d\n", nr, fib(nr) );

 return 0;

}

Explanation:

The code is a literal translation of the definition using a recursive function.

The recursive function is not per se a very efficient one.

You might be interested in
What does the security element of non-repudiation mean in e-commerce cybersecurity? A. Data needs to be available at all times.
VARVARA [1.3K]

Answer:

Non-repudiation is the assurance that someone cannot deny the validity of something. Non-repudiation is a legal concept that is widely used in information security and refers to a service, which provides proof of the origin of data and the integrity of the data

Explanation:

8 0
3 years ago
Write the following function without using the C++ string class or any functions in the standard library, including strlen(). Yo
dolphi86 [110]

Answer:

The function in C++ is as follows

int chkInd(string str1, string str2){    

int lenstr1=0;

while(str1[lenstr1] != '\0'){  lenstr1++;  }

int index = 0; int retIndex=0;

for(int i=lenstr1-1;i>=0; i--){

   while (str2[index] != '\0'){

       if (str1[i] == str2[index]){

           retIndex=1;

           break;         }

       else{   retIndex=0;      }

  index++;    }

  if (retIndex == 0){   return i;   }else{return -1;}}

}

Explanation:

This defines the function

int chkInd(string str1, string str2){    

First, the length of str1 is initialized to 0

int lenstr1=0;

The following loop then calculates the length of str1

while(str1[lenstr1] != '\0'){  lenstr1++;  }

This initializes the current index and the returned index to 0

int index = 0; int retIndex=0;

This iterates through str1

for(int i=lenstr1-1;i>=0; i--){

This loop is repeated while there are characters in str2

   while (str2[index] != '\0'){

If current element of str2 and str1 are the same

       if (str1[i] == str2[index]){

Set the returned index to 1

           retIndex=1;

Then exit the loop

           break;         }

If otherwise, set the returned index to 0

       else{   retIndex=0;      }

Increase index by 1

  index++;    }

This returns the calculated returned index; if no matching is found, it returns -1

  if (retIndex == 0){   return i;   }else{return -1;}}

}

4 0
3 years ago
The following program declares an array of char named as myString There are 6 following cases (a, b, c, d, e, f) to access myStr
yuradex [85]

Answer:

See explaination

Explanation:

a.

myString is "Hello the world"

b.

prints "15"

c.

This is invalid.

We have to use strcpy_s to copy strings

FIX:

strcpy_s(s,"Marylane");

d.

reading string upto length 80 from the user and stored it in myString variable

e.

prints the string enetered by user to console

f.

replacing 7th character by 't'

3 0
2 years ago
Determining Uses for Outer Joins
Ilia_Sergeevich [38]

Answer:

a query that shows all customers, whether or not they placed an order

Explanation:

EDG 21

4 0
2 years ago
Your son is complaining that his smartphone is broken. He tells you that he cannot connect to the internet, nor can he make or r
ycow [4]

Answer:

the correct and only reasonable answer is option B

3 0
2 years ago
Other questions:
  • A decrease in the Short-Run Aggregate Supply Curve is associated with what?
    8·1 answer
  • Luentifying message rullidl Upuuns
    12·1 answer
  • Which of the following actions would help people determine their interests?
    11·1 answer
  • I need help fixing this please
    6·2 answers
  • True or False The two types of general construction projects are residential for homes or dwellings and commercial for a commerc
    7·1 answer
  • Write at least and explain four types of escape sequences and create an example in an IDE which consist of the mentioned escape
    7·1 answer
  • What are the two main parts to a VR experience.
    12·1 answer
  • On a Windows system, which Task Manager tab would you use to adjust the priority given to a specific program
    13·1 answer
  • Can somebody please help me with these few questions?
    11·1 answer
  • When you insert a new slide in a presentation, where is it placed in relation to the existing slides?.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!