Java Exercise 31: Lower limit and upper limit [closed] - java

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 having trouble with an exercise.
The instructions are:
Create a program that asks the user for the first number and the last number and then prints all numbers between those two.
Below is my code. If the first number is greater than the last number, the program prints nothing.
System.out.println("First: ");
int first = Integer.parseInt(reader.nextLine());
System.out.println("Second: ");
int second = Integer.parseInt(reader.nextLine());
int number = first;
while (first <= second) {
System.out.println(number);
number = first + 1;
}

Related

How to just calculate the last number that we just input? [closed]

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
Example we input 20182391, how to just calculate the last digit of number? like "add the last digit with 2"
Thank you
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
int lastDigit = input % 10;
//Do whatever you want to.
public static void main(String[] args){
Integer input = 20182391;
System.out.println(input % 10 + 2);
}
This should work

How Can I Write A Statement To Print Out If The Number Is Odd Or Even in Java [closed]

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

Java - How to only accept positive numbers [closed]

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 3 years ago.
Improve this question
I write a program where I need to take user input (prices) and giving the sum of all prices as output. Naturally prices can't be negative, so my question is: How does the program accept positive numbers only?
You want something like this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a price: ");
double number = input.nextDouble();
while (number < 0 ) {
System.out.print("Sorry, but your price must be a positive decimal. Enter a price: ");
number = input.nextDouble();
}
System.out.println("Your price is " + number);
}
}
Use a while loop to keep re-checking if the entered price meets your standards.

How to find the closest element in an array [closed]

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++;
}
}
}

How to check how many times one number appears in the second number? [closed]

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;

Categories