Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've tried looking for the answer to this question, but I haven't had a lot of luck. Basically I am asked to create the Fibonacci Code, and then allow a user input to look for there input in the sequence. If its in the sequence then it shows what index. If its not then it shows the two closest numbers to it.
So if a user inputs 4, the nearest elements would be 3 and 5 and the indexes would be 4 and 5.
I'm basically struggling with finding the nearest elements. I'm not exactly sure how to do it.
****update****
So I did figure it out thank you
1.Store the previous Fibonacci number in a buffer (you can initialize with -1)
2.update the buffer after every new number is calculated.
3.if the current number is not equal to the new number
3.A check if the number is greater than buffer and less than the new number
3.A.1)If yes, those two are your nearest numbers.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int userInput;
Fibonacci.fibonacciAlgor(5);
}
public static void fibonacciAlgor(int userInput)
{
int i=0;
int buffer=-1;
int x=0,y=1;
System.out.println("Input: " + userInput);
while(i<1000000){
if(x==userInput){
System.out.println("Belongs to sequence: Yes "");
break;
}
else{
if(userInput>buffer&&userInput<x){
System.out.println("Belongs to sequence: No ");
System.out.println("Nearest Elements: " + buffer+","+x);
break;
}
}
buffer=x;
int temp=y;
y=x+y;
x=temp;
i++;
}
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to print an array's element with it's index next to it. If the array element is even, it must display the element and its correlating index number, if the element is odd, it must display the element and "1".
For example:
Input: int[] array = new int [25]
Output: 0:0, 1:1, 2:2, 3:1, 4:4, 5:1, 6:6, 7:1 and so on
How would I do this?
You could do something like, You can check if a number is even then print the number and index separated by a colon else print number and 1 separated by a colon, This can be achieved by using for loop construct.Would recommend you to follow proper guide/tutorial as you look like a beginner in java.
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
int counter = 0;
for(int i : array) {
if(i % 2 == 0)
System.out.print(i+":"+counter);
else
System.out.print(i+":1");
if(counter<array.length-1)
System.out.print(", ");
counter++;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to write A program To check and Print Out If The Number Is Odd Or Even.
This Is What I Have Programmed So Far:
import java.util.Scanner;
public class Irs_Lab
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter an integer :: ");
int num = kb.nextInt();
}
}
I Heard You can Use A % (mod) to get the remainder of division.
Can anyone help me on this.
You can use the modular operator to see if a number is even or odd(as you said). When dividing a number by 2, if it is even it will have a remainder of 0, if it is odd it will have a remainder of 1. Using if(a % 2 == 0){//even}else{//odd} is your likely solution
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm new in Java and I have a task that's going like this:
Generate one number between 0-9 and a second number between 10-99.
You need to check how many times the first number appears in the second number.
(For example: The first number is 5 and the second one is 55 so the first number (5) apears 2 times in the second number (55) ).
Btw, you need to make it without arrays.
package randomNumber_01;
import java.util.Random;
public class third {
public static void main (String[] args){
Random n = new Random();
int first = n.nextInt(9)+1;
System.out.println("First numnber: "+first);
int second = n.nextInt(99)+10;
System.out.println("Second number: "+second);
System.out.println("I stuck here");
}
}
int count=0;
if((second%10)==first){
count++;
}
if(Math.round((second/10))==first){
count++;
}
System.out.println(first+" occurs "+count+" times in "+second);
Maybe this can help you ;), replace your last println by this 8 lines ;)
but as shmosel sait in comment, be carreful it's
int first = n.nextInt(10);
int second = n.nextInt(90)+10;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was required to make a code that accepted a binary number (1's and 0's) and then counted how many ones were in that binary number. My code fulfills this purpose.
The second part of the exercise is this: if the user enters a number that is NOT binary, I must output that there is an error and keep prompting the user until they give a binary number.
Can someone show me how to incorporate this? I have tried several times but cannot make it click. Thanks! Here is my code.
import java.util.Scanner;
public class NewClass
{
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in);
int i = 0, count = 0;
String number;
System.out.println("Please enter a binary number.");
number = scan.next();
String number1 = "1";
while ((i = number.indexOf(number1, i++)) != -1) {
count++;
i += number1.length();
}
System.out.println("There are "+ count + " ones in the binary number.");
}
}
You already know how to find all of the 1's; you should be able to do something similar to find all of the 0's.
If the sum of those two counts is not the length of the string, there must be at least one illegal character in it.
If you use the parseInt method you can do
Integer.parseInt("1100110", 2) returns 102
or in your case
int intVal = Integer.parseInt(number, 2);
as per the javadocs
Throws:
NumberFormatException - if the String does not contain a parsable int.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to validate input values to pass only integers that are divisible by 10. The code below is failing.
public static void main(String args[]) {
Scanner scan =new Scanner(System.in);
ArrayList<Integer> liste = new ArrayList<Integer>(); // I have filled my array with integers
int x=scan.nextInt();
int y=x%10;
do{
if(y==0){
liste.add(x);}
else if(y!=0){
System.out.println("It is not valid"); continue;
}
else
{System.out.println("Enter only integer"); continue;
}
}while(scan.hasNextInt()); }
System.out.println(liste);
System.out.println("Your largest value of your arraylist is: "+max(liste));
You're calling scan.nextInt() twice. Each time you call it, it will read another int from the input. Thus, if your input was something like
10
5
13
then the 10 would pass the scan.nextInt()%10==0 check, and then 5 would be added to the list. Store the result of scan.nextInt() in a variable first, so the value won't change.
Instead of
if(scan.nextInt()%10==0){
liste.add(scan.nextInt());}
do
int num = scan.nextInt();
if(num%10 == 0){
liste.add(num);
}