DEV Community

Neelakandan R
Neelakandan R

Posted on

Decimal to binary,Binary to decimal,Fibonacci series without third variable

binary to decimal==>1001 to 9

package new_practice; //8421 public class bi_dec_pro { static int dec=0; public static void main(String[] args) { int no=1001; int base=2; int power=0; int d=finddec(no,base,power); System.out.println(d); } private static int finddec(int no, int base, int power) { while(no>0) { int rev=no%10; int result=rev*find_power(base,power); dec=dec+result; no=no/10; power++; } return dec; } private static int find_power(int base, int power) { int result=1; while(power>0) { result=result*base; power--; } return result; } } 
Enter fullscreen mode Exit fullscreen mode

output:9

**<u>decimal to binary==>9 To 1001</u>** package new_practice; public class dec_binary_num { public static void main(String[] args) { int no=9; String binary=""; while(no>0) { binary=(no%2)+binary; no=no/2; } System.out.println(binary); } } 
Enter fullscreen mode Exit fullscreen mode

output:1001

Fibonacci series without third variable

package new_practice; public class fib_no_series { public static void main(String[] args) { int f = -1; int s = 1; while ((f + s) < 13) { System.out.println(f + s + " "); s = f + s; f = s - f; } } } 
Enter fullscreen mode Exit fullscreen mode

0 1 1 2 3 5 8

Top comments (0)