+ 3
Any body print fabnoci series using for loop with using two variable in c
In c
20 Antworten
+ 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;
}
+ 7
show your attempt first
+ 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
+ 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
+ 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);
}
}
}
+ 1
Yes int hoga
+ 1
Thanks Pallavi
+ 1
Arsenic you are right
+ 1
https://code.sololearn.com/cRz5M5JSpKRc/?ref=app
+ 1
Bhai ye three variable ke use se banaya gya hai
0
I am not able to print this
0
Lekin ye to three variable ke use se banaya gya hai
0
Lekin mai two variable ke use se puch rahe hai
0
Kisi ke pass logic nhi hai
0
Ok I am trying to use two variables
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