Fibonacci Series Program - Exiting For Loop Early If Exceed Specified Number

Feb 13, 2015

I'm trying to change the code on a Fibonacci series program that would allow me to exit the loop early if I exceed a specified number. The user enters any 2 random numbers (which will be the 1st 2 no.'s of the Fibonacci sequence printed to screen) and then continues up to a 'limit' on the number of numbers set in code. Here's the code:

int[] array = new int[limit]; //Define an array whose length is set by an int value for limit!!
array[0] = x; //User supplies a int value for x which takes the 1st position in the array!!
array[1] = y; //...and an int value for y in the 2nd position!!
 for (int i = 2; i < limit; i++) //Start from the 3rd position of the array when carrying out calculations!! {
array[i] = array[i-1] + array[i-2];

[Code] ....

To exit the code/ 'limit' early if the array prints a number higher than 100, I tried putting a 'while' condition before the last line, as follows:

while (array[i] < 100)
System.out.print(array[i] + " ");

Can I even use a 'while' loop within an array, or is there some other way I need to integrate it?

View Replies


ADVERTISEMENT

Write A Program To Print Fibonacci Series Up To 100

Dec 31, 2014

My code:

public class Fibonacci {
/**
* @param args
*/
public static void main(String[] args) {
int f[]=new int[100];
for(int j=0; j<100;j++){
f[j]=j;

[Code] ....

How can I improve my code?

View Replies View Related

How To Start Fibonacci Series In Particular Range

Jun 11, 2014

How to print 10 numbers in Fibonacci serious aftet given number?

Like 13 21 34...

View Replies View Related

Fibonacci Series Using Java Multithreading

Feb 8, 2014

Program to generate Fibonacci series program using java multithreading.

View Replies View Related

Print Fibonacci Series Till User Wants It

Jun 12, 2014

I wrote java program to print fibonacci series till user wants it! But I'm getting this compilation error -

fibo.java:Incompatible types
found:java,lang.String
required:int
n=s.readline();

import java.awt.*;
import java.io.*;
import java.util.*;
class fibo {
public static void main(String args[])throws IOException {
int arr[]=new int[100];

[Code] ......

View Replies View Related

Program That Calculates Fibonacci Number

Nov 19, 2014

Its a program that calculates Fibonacci number.This program uses recursion.

import java.util.Scanner;
public class FibonacciMemoization{ 
static int[] fib = new int[60];
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a Number :");
int number = input.nextInt();

[code]...

View Replies View Related

While Loop With Two Sentinel Values Is Terminating Early

Sep 25, 2014

The program is supposed to print all even and odd numbers between 50 and 100 using ONE while or do while loop. The evens work, but the program terminates after printing 51 for the odds, which doesn't satisfy the second value sentinel value for the while loop.

public class EvenOdd {
public static void main(String args[]) {
int x = 50;
int y = 50;
int i = 0;
int j = 0;
System.out.print("Even numbers between 50 and 100: ");

[Code] ....

View Replies View Related

Checking Array For Title - Exiting For Loop

Oct 4, 2014

i want to check my array for a title, if the title is there i want to call a method, if the title is not there i want an error message. I am not sure how to get this. i think my code would work if it didn't always finish the method. for example if true the method stops and does run whats after the for loop. but that isn't happening.

void markItemOnLoan(String title, String name, String date){
int i=0;
for (i = 0; i < numberOfItems; i++){
if (items[i].getTitle().equals(title)){
items[i].markOnLoan(name,date);
}
}
System.out.println("ERROR: Can not find that item. ");
}

View Replies View Related

YES / NO Loop In Fibonacci Sequence

Mar 23, 2014

Fibonacci Sequence Code:

public class FibSeqByIanNeumann {
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
int ctr, num1, num2, fib, maxTimes;
System.out.print("How many sequences do you want?: ");
maxTimes = get.nextInt(); //inputs the maxium limit of the fib sequence
 
[code]....

how to do a simple YES/NO Loop so I can try to have it work on my code if I want to try to do the Fibonacci Sequence again.Now I think it might have something to do with a do/while loop.

View Replies View Related

For Loop For Fibonacci Code

Sep 22, 2014

I need to write a For Loop that prints out the first 12 Fibonacci numbers:1 1 2 3 5 8 13 21 34 55 89 144.The problem I am having is that I can not get the first two 1 numbers. I only get 1.2.3.5.8.....

int a=1;
int b=1;
for (int i=1; i<12;i++)
{
System.out.print(a+" ");
a=a+b;
b=a-b;
}

Do I need to add another For Loop that sub-tracks so that I can get the first digit of 1?

View Replies View Related

For Loop - Calculate Total Of Series Of Numbers?

Nov 10, 2014

So basically, I've been trying to create a for loop that asks the user for a number between 1 and 30, then calculates the total of the series of numbers. For example, if 15 is entered, the calculation should be 1/15+2/14+3/13+...15/1 which would equal approximately 38.1. The problem I'm having is that the code doesn't loop whenever I type a number for some reason, which results in a very incorrect calculation. Here is my code so far:

import java.util.Scanner;
public class HmwLoop {
 public static void main(String[] args) {
double sum = 0;
for (double num1 = 1, num2 = 30; num1 <= 30 && num2 >= 1; num1++, num2--)

[Code] .....

View Replies View Related

Finding Nth Term In Fibonacci Sequence Using While Loop

Oct 30, 2014

I have to find where in the fibonacci sequence a at number belongs, using a while loop.

Example

>55 is a Fibonacci number whose order in the sequence is 11
>35 is not a Fibonacci number. However, it lies between Fibonacci numbers 34 (order: 10) and 55 (order: 11)

import java.util.Scanner;
public class While
{
public static void main(String[] args)
{
System.out.println("Welcome to the Fibonacci Sequence Detector");

[Code] ....

View Replies View Related

Adding Fractions - Loop For Calculating Total Of Series Of Numbers

Mar 8, 2014

I'm just not noticing why it won't display the answer. I'm trying to solve this book problem......

"Write a for loop that calculates the total of the follower series of numbers:

1/30 + 2/29 + 3/28......+30/1"

Here is what I have..

public static void main(String[] args) {
double total = 0;
for (double a = 1, b = 30; b < 1; a++, b--) {
total += (a / b);
}
System.out.println(total);
}
}

When launched, the output is 0.0. I tried changing the variables a and b to doubles but didn't change anything...

View Replies View Related

Finding Nth Number In Fibonacci Sequence

Oct 30, 2014

I have to find where in the fibonacci sequence a at number belongs, using a if or while loop.

Example

>55 is a Fibonacci number whose order in the sequence is 11
>35 is not a Fibonacci number. However, it lies between Fibonacci numbers 34 (order: 10) and 55 (order: 11)

import java.util.Scanner;
public class While {
public static void main(String[] args) {
System.out.println("Welcome to the Fibonacci Sequence Detector");
Scanner in = new Scanner(System.in);

[Code] .....

View Replies View Related

Fibonacci Number - Recursive Function

Jul 8, 2014

I wrote this tail recursive function that mirrors the iterative version, except that the loop in the iterative version is replaced by an if statement here and then a recursive call. Is this truly recursive? I have seen the fibo(n-1) + fibo(n - 2) version, but is this also an acceptable recursive solution? Why is it never solved this way?

public class FiboRecursive {
public static int fibo (int n) {
int sum = 0;
int n1 = 1;
int n2 = 1;
if (n == 1 || n == 2) {
sum = 1;

[Code] ...

View Replies View Related

Fibonacci Sequence - Calculate Nth Number

Apr 29, 2014

Write a recursive method that calculates the Nth number in the Fibonacci sequence. The first and second numbers in the sequence (the base cases) are both 1. After that, each subsequent number is the sum of the previous two. Stated a bit more formally:

fib(n)={1fib(n−1)+fib(n−2)n<2otherwise

For example, here is the first few numbers in the sequence:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Your fib method should be part of a class named Fibonacci. In addition to the fib method, the Fibonacci class should have a main method that calls fib(9). If the result doesn't equal 34, you should print an error message. Otherwise, it should print out a message saying that it was successful.

After writing your Fibonacci class, answer the following question: How many times is the fibonacci method called when calculating the 5th number in the sequence?

View Replies View Related

How To Display Number Series In Ascending Order

Aug 9, 2014

tfrreee i want to display given series in ascending order in java prog here is an error : E:Javajdkin>java ascend 505671Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at ascend.main(ascend.java:9)

Java Code:

class ascend
{
public static void main (String args[])
{
int s[]={ 50,71,81,21,7};
int i;
for(i=0;i<=s.length;i++)

[Code]...

View Replies View Related

Determine If User Input Is Fibonacci Number Or Not

Oct 20, 2014

Part A: While Loop Program

Write a program that detects Fibonacci numbers. Prompt the user to input a positive integer. Upon input, the program will determine if the number is either a Fibonacci number or not. If a Fibonacci number, then the order of the number in the sequence must be output. If not a Fibonacci number, then the Fibonacci numbers above and below it (including their order in the sequence) must be output. Once it finishes, the program will prompt the user for a new number. The program will exit if the user enters a string (such as “quit”) instead of an integer. Use the sample output file, fib-seq-det.txt, to view a sample session

This is my project, I wrote a programs that tells you if the input number is a fibonacci number or not. For some reason it only works for some Fibonacci numbers but not all of them.

import java.util.Scanner;
public class While
{
public static void main(String[] args) {
System.out.println("Welcome to the Fibonacci Sequence Detector
");
Scanner in = new Scanner(System.in);
System.out.print("Please input a number for analysis: ");
int input = in.nextInt();

[Code] ....

View Replies View Related

Calculate Nth Fibonacci Number And Returns That To User

May 1, 2014

I have to create a program that calculates the nth Fibonacci number and returns that to the user. Fibonacci said his number sequence would describe the ideal breeding patterns of immortal rabbits. So, you are going to make this vision a reality.

First, take in a numeric value from the user and calculate that value in the fibonacci series. Next, find an image of a rabbit and display the image on a GUI (put the image as an icon on a label!) the number of times returned by the algorithm (Put all the aforementioned labels on one panel with FlowLayout!).

You need to remove the old images from the Panel. Probably the easiest way to do this is to create a whole new panel and remove the old one (hint: the remove method of JPanel should come in handy)

You could use an array of JLabels
You will need to create a new JLabel and add each member of the array to the panel
I should be able to scroll to see any images that are off screen

I am having difficultly on to making the array list for JLabel, and getting the Fibonacci sequence to show the pictures of rabbits. Below is my current code.

import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rabbit extends JFrame

[Code] ....

View Replies View Related

Write A Program To Determine Sum Of Following Harmonic Series For A Given Value Of N

May 9, 2014

write a program to determine the sum of the following harmonic series for a given value of n:1+1/2+1/3+.............................+1/n the value of n should be given interactively through the keyboard.

View Replies View Related

Program To Display Both Largest And Smallest Of A Series Of 5 Integers

Apr 27, 2014

Here is a simple program on applets that is giving me a bit of headache since it cannot always display the two numbers that it is supposed to: the largest and smallest of the five input integers.....I cannot see where i went wrong, you can always create separately the HTML file to load the applet in an appletviewer of your won size.

*
23.6 (Largest and Smallest) Write an applet that reads five integers, determines which are the largest and smallest integers in the group and prints them. Draw the results on the applet.
*/
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

[Code] .....

View Replies View Related

Java Program That Prints Out Taylor Series For Mathematical Constant E

May 10, 2015

I am trying to write a java program that prints out the number that is the mathematical constant e. As you input a number, the larger it gets , the closer it comes to 2.71828 . Here is my code:

//taylor series that prints out e^1=1+1/1!+1/2!+1/3!+.....
import java.util.Scanner;
public class taylor_1
{
public static void main(String args[]) {
 Scanner input=new Scanner(System.in);
int factorial =1;

[Code] .....

Here is the output of my code:

enter n
9
Taylor series is 9.0

View Replies View Related

How To Close JFrame Without Exiting Application

Apr 10, 2014

How to make a Jframe close without closing the whole operation, I started off using exit on close and changed it to hide on close and i also tried dispose on close for some reason it wont work. Here is what I have.

import java.awt.*;
import javax.swing.*;
public class GUILauncheralt{
public static void main(String[] args){
RetrieveWindow gui = new RetrieveWindow();
gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
gui.setSize(400, 200);

[Code] .....

View Replies View Related

Making 2D Array And Its Not Filling Each Box - Code Terminates Early And Gives Error

Apr 18, 2015

Here is the code: I believe the issue is in the nest for loop with I use

f1.length

But I want to not use the individual sizes I put in, something more suitable so I don't have to change the the values everywhere I may need to.

public class ForestFireDemo {
public static void main(String[] args) {
Forest[][] f1;
f1 = new Forest[10][5];
for(int i =0 ; i < f1.length; i++) {

[Code] ....

View Replies View Related

How To Create Withdraw / Deposit Feature So It Can't Exceed Maximum Limit

Nov 25, 2014

So lets say I got maximum of 20 deposit, I want to create a method/function (idk what proper name for it is) so that you cannot deposit more than 20 nor withdraw more than you have in your account, so this function checks your account, if your total is for example 18 and you're trying to deposit 10, it rejects it and doesn't add to your total and pops up with a message, vice versa for withdraw.

This is what I have so far

Not I already have the maximum limit + total feature created just not displayed here

public void depositMoney (int dMoney) {
if(DMoney > 0 ) {
totalMoney = totalMoney + dMoney;
}
else {
System.out.println("Please insert money more than 0");

[Code] .....

View Replies View Related

Create Circular Array Which Prints Out 8 Numbers That Increase By One And Don't Exceed 9

Jan 31, 2015

I'm trying to create a circular array which prints out 8 numbers that increase by one and don't exceed 9. If they do, the remaining numbers are printed from 0 on-wards. I have code below which does this job, but it doesn't really use an array to loop back.

Java Code:

package Practice;

public class Practice
{
public static void main(String[] test)
{
number(7);
number(9);

[code]...

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved