Any body print fabnoci series using for loop with using two variable in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Any body print fabnoci series using for loop with using two variable in c

In c

10th Jan 2021, 9:33 AM
Ankit Kumar
Ankit Kumar - avatar
17 Answers
+ 1
#include <stdio.h> int main() { n,i,a=0,b=1; printf ("enter number of terms"); scanf("%d",&n); printf ("%d",a); printf("%d",b); for(i=1;i<=n;i++) { i=a+b; printf("%d",i); a=b; b=i; } return 0; }
10th Jan 2021, 9:36 AM
Ankit Kumar
Ankit Kumar - avatar
+ 7
show your attempt first
10th Jan 2021, 9:35 AM
ÃKR
ÃKR - avatar
+ 7
Interstingly, you can also use maths to directly calculate nth Fibonacci number instead of finding it constructively Here you go👇 https://code.sololearn.com/cOaEokAsGoc8/?ref=app
10th Jan 2021, 10:01 AM
Arsenic
Arsenic - avatar
+ 6
Ankit Kumar Logic sabke paas hai but ye right place nahi hai challenge karne ka. Karna hai to feed post me karo and rules ko follow karo. https://www.sololearn.com/discuss/1316935/?ref=app
10th Jan 2021, 10:04 AM
A͢J
A͢J - avatar
+ 3
Using recursion import java.util.*; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) System.out.print(fab(i) + " "); } static int fab(int n) { if (n < 2) { return 1; } else { return fab(n - 1) + fab(n - 2); } } }
11th Jan 2021, 4:04 AM
A͢J
A͢J - avatar
+ 1
Yes int hoga
10th Jan 2021, 9:43 AM
Ankit Kumar
Ankit Kumar - avatar
+ 1
Thanks Pallavi
10th Jan 2021, 10:06 AM
Ankit Kumar
Ankit Kumar - avatar
+ 1
Arsenic you are right
10th Jan 2021, 10:11 AM
Behzod
Behzod - avatar
10th Jan 2021, 8:40 PM
\•/
\•/ - avatar
+ 1
https://code.sololearn.com/cRz5M5JSpKRc/?ref=app
11th Jan 2021, 3:32 AM
Biswajit Sahoo
Biswajit Sahoo - avatar
+ 1
Bhai ye three variable ke use se banaya gya hai
11th Jan 2021, 3:34 AM
Ankit Kumar
Ankit Kumar - avatar
0
I am not able to print this
10th Jan 2021, 9:36 AM
Ankit Kumar
Ankit Kumar - avatar
0
Lekin ye to three variable ke use se banaya gya hai
10th Jan 2021, 9:44 AM
Ankit Kumar
Ankit Kumar - avatar
0
Lekin mai two variable ke use se puch rahe hai
10th Jan 2021, 9:44 AM
Ankit Kumar
Ankit Kumar - avatar
0
Kisi ke pass logic nhi hai
10th Jan 2021, 9:48 AM
Ankit Kumar
Ankit Kumar - avatar
0
Ok I am trying to use two variables
11th Jan 2021, 3:36 AM
Biswajit Sahoo
Biswajit Sahoo - avatar
0
fibonacci sequence is formed by sum of 2 numbers. think it is a=0,b=1 for 1...n print(b) // 1, 1, 2, 3 b+=a a=b-a however clearly at least 4 variables since you will need loop counter and n counts :) to get rid of extra variables, just make it an infinite loop for(;;) ;) 0 variables would be possible assuming you know the number of digits to display. Would just be printing hardcoded string lol. why a=b'-a. At this point b'= b+a. So substituting a=b'-a -> b+a-a -> so b
12th Jan 2021, 12:49 AM
Javadev