go go go go javapenguin hehe maypa dre ko storya saona patabang dah... dako2 tana ko grade sa JAVA..... hahaha
go go go go javapenguin hehe maypa dre ko storya saona patabang dah... dako2 tana ko grade sa JAVA..... hahaha
most of the answers of these problems can be done, in the console, but ang lingaw ani nga answer no, kanang mo.gamit Swing components, ug kanang naa GUI bah, kay magkabuang lagi na ug tubag sa ilang maestro! Hahahahahaha
Bantay mooooo! Explain jud mo anaaaa!!! Ahahahahaha!
idola ning javapenguin
ai kalimti ug earese ngan sa author inig pass ninyo
While I'm waiting for netbeans 6.5 download and installation, I'll make problem #2, this is an interesting example of recursion (a method calling itself)
This reminds me of the subject numerical methods a long time agoCode:/* * 2. Write a Java class called Fibonacci which defines a main() method that asks the user to enter a positive * integer n, and computes the nth Fibonacci number. The nth Fibonacci number is defined by this formula: * o 0th Fibonacci number: 0 * o 1st Fibonnaci number: 1 * o nth Fibonacci number (for n >= 2): (n-1)th Fibonacci number + (n-2)th Fibonacci number */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author javapenguin */ public class Fibonacci { public static void main(String[] args) { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); boolean invalid = true; String number = new String(); while (invalid) { invalid = false; System.out.println("Please enter a positive integer number: "); try { number = input.readLine(); } catch (IOException ex) { System.err.println(ex); invalid = true; } int trynum = 0; try { trynum = Integer.parseInt(number); } catch (NumberFormatException nfe) { invalid = true; } if (trynum < 0) invalid = true; number = String.valueOf(trynum); } System.out.println("Fibonacci of "+number+" is "+fibonacci(Integer.parseInt(number))); } static int fibonacci(int n) { int fn = 0; if (n == 0) fn = 0; //can be omitted if (n == 1) fn = 1; if (n >= 2) fn = fibonacci(n-1)+fibonacci(n-2); return fn; } }
good work on recursion javapenquin, here is my take on the iterative side!
Note: if you want to display the Fibonacci Number Sequence, put the console output inside the loop.Code:a = 0; b = 1; for(x = 1; x <= num; x++) { a = a + b; b = a - b; } System.out.println("Fibonacci Number of "+ num + " is: "+b);
I like the simplicity of your approach, I may be wrong but I think you need to put +1 in num of your for loop
Fibonacci number - Wikipedia, the free encyclopediaCode:a = 0; b = 1; for(x = 1; x <= num+1; x++) { a = a + b; b = a - b; } System.out.println("Fibonacci Number of "+ num + " is: "+b);
Update 1.1
ang wla pa n solve ky numbers 11-14-17-20-22-23-24
^_^
Wala na lain nga java programmer ganahan mo answer? hehehe
Code:/* 20. Write a Java class called KillWhiteSpace, which defines a main() method that asks the user to enter a String s, and displays a new String consisting of all the characters in s, but with the leading and trailing white space removed. In addition, every sequence of spaces within the String is reduced to one space only. You MUST NOT use any methods defined in the String class other than charAt() and length()./* */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author javapenguin */ public class KillWhiteSpace { public static void main(String[] args) { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String s = new String(); System.out.println("Please enter a string: "); try { s = input.readLine(); } catch (IOException ex) { System.err.println(ex); } s = s+" "; String ns = new String(); for (int i = 0; i < s.length()-1; i++) { if (!(s.charAt(i) == ' ' && s.charAt(i+1) == ' ')) { ns = ns + s.charAt(i); } } String nns = new String(); if (ns.length()>1 & ns.charAt(0) == ' ') for (int i = 1; i < ns.length(); i++) { nns = nns+ns.charAt(i); } else { nns = ns; } System.out.println("New string > "+nns); } }
Similar Threads |
|