C/C++ program to find the nth Fibonacci number?
The Fibonacci sequence is a sequence of numbers in which the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.
In this problem, we will find the nth number in the Fibonacci sequence. To do this, we will calculate all the numbers and print n items.
Input:8 Output:0 1 1 2 3 5 8 13
Instructions
0+1=1 1+1=2 1+2=3 2+3=5
Use a For loop to sum the first two items as the next item
Example
#include<iostream> using namespace std; int main() { int t1=0,t2=1,n,i,nextTerm; n = 8; for ( i = 1; i <= n; ++i) { if(i == 1) { cout << " " << t1 ; continue; } if(i == 2) { cout << " " << t2 << " " ; continue; } nextTerm = t1 + t2 ; t1 = t2 ; t2 = nextTerm ; cout << nextTerm << " "; } }
Output
0 1 1 2 3 5 8 13
The above is the detailed content of C/C++ program to find the nth Fibonacci number?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to write an algorithm to find the least common multiple in Python? The least common multiple is the smallest integer between two numbers that can divide the two numbers. In mathematics, solving the least common multiple is a basic mathematical task, and in computer programming, we can use Python to write an algorithm for solving the least common multiple. The following will introduce the basic least common multiple algorithm and give specific code examples. The mathematical definition of the least common multiple is: If a is divisible by n and b is divisible by n, then n is the least common multiple of a and b. To solve the minimum

A simple calculator is a calculator that performs some basic operations, such as "+", "-", "*", "/". The calculator can perform basic operations quickly. We will use the switch statement to make a calculator. Example Operator−‘+’=>34+324=358Operator−‘-’=>3874-324=3550Operator−‘*’=>76*24=1824O

Numpy is a well-known scientific computing library in Python, which provides rich functions and efficient computing methods for processing large multi-dimensional arrays and matrices. In the world of data science and machine learning, matrix inversion is a common task. In this article, I will introduce how to quickly solve the matrix inverse using the Numpy library and provide specific code examples. First, let's introduce the Numpy library into our Python environment by installing it. Numpy can be installed in the terminal using the following command: pipinsta

Title: Use C language programming to implement the greatest common divisor solution. The greatest common divisor (Greatest Common Divisor, GCD for short) refers to the largest positive integer that can divide two or more integers at the same time. Solving for the greatest common divisor can be very helpful for some algorithms and problem solving. In this article, the function of finding the greatest common divisor will be implemented through C language programming, and specific code examples will be provided. In C language, you can use the Euclidean Algorithm to solve the maximum

How to use Python to implement the algorithm for solving factorial? Factorial is an important concept in mathematics. It means that a number is multiplied by itself minus one, then multiplied by itself minus one, and so on until it is multiplied to 1. Factorial is usually represented by the symbol "!". For example, the factorial of 5 is expressed as 5!, and the calculation formula is: 5!=5×4×3×2×1=120. In Python, we can use loops to implement a simple factorial algorithm. A sample code is given below: deffacto

Parity sort algorithm is also called brick sort, which is a sorting technique similar to bubble sort. This sorting technique is divided into two phases: odd phase and even phase, which are performed simultaneously in each iteration until all elements are sorted. The odd phase of this programming technique is similar to bubble sort, but only sorts elements with odd indices. Likewise, the even stage only sorts elements with even indices. To illustrate this concept more clearly, let us take an example: Input:a[]={3,5,7,6,1,4,2}Output:1234567Explanation Even-odd sorting, also known as brick sorting, is a A simple sorting technique designed with parallel processing in mind. It uses comparison to perform a comparison on its elements

Here we will see an efficient way to check if the nth Fibonacci term is a multiple of 10. Assume that the Fibonacci terms are {0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987}. So here the 15th Fibonacci number (counting from 0) is divisible by 10. For 16 it will return true. One of the simplest ways is to generate Fibonacci numbers up to a given term and check if it is divisible by 10? But this solution is not good because it doesn't work for larger items. Another good way is as follows - Fibonacci terms - 0,1,1,2,3,5,8,13,21,34,55,89,144,233,3

To learn how to find the greatest common divisor in C language, you need specific code examples. The greatest common divisor (Greatest Common Divisor, GCD for short) refers to the largest positive integer among two or more integers that can divide them. The greatest common denominator is often used in computer programming, especially when dealing with fractions, simplifying fractions, and solving problems such as the simplest ratio of integers. This article will introduce how to use C language to find the greatest common divisor and give specific code examples. There are many ways to solve the greatest common divisor, such as Euclidean
