Please put some more effort on your query, for sure you can come up with your answers by yourself and you will learn faster. For now You can refer to below answer.
package com.barnwal.jeetendra.learn;
import java.util.Scanner;
public class Assignment4 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the first integer:");
int first = s.nextInt();
System.out.print("Enter the second integer:");
int second = s.nextInt();
System.out.println("How many integers are between " + first + " and "
+ second + "???");
// To print number of integer between entered number
if (second > first) {
System.out.println("Answer : " + (second - first - 1));
// To print the numbers
for (int i = first + 1; i < second; i++)
System.out.print(i + " ");
} else {
// To print number of integer between entered number
System.out.println("Answer : " + (first - second - 1));
// To print the numbers
for (int i = second + 1; i < first; i++)
System.out.print(i + " ");
}
}
}
Answer from Jeetendra on Stack OverflowVideos
Please put some more effort on your query, for sure you can come up with your answers by yourself and you will learn faster. For now You can refer to below answer.
package com.barnwal.jeetendra.learn;
import java.util.Scanner;
public class Assignment4 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the first integer:");
int first = s.nextInt();
System.out.print("Enter the second integer:");
int second = s.nextInt();
System.out.println("How many integers are between " + first + " and "
+ second + "???");
// To print number of integer between entered number
if (second > first) {
System.out.println("Answer : " + (second - first - 1));
// To print the numbers
for (int i = first + 1; i < second; i++)
System.out.print(i + " ");
} else {
// To print number of integer between entered number
System.out.println("Answer : " + (first - second - 1));
// To print the numbers
for (int i = second + 1; i < first; i++)
System.out.print(i + " ");
}
}
}
To avoid the unnecessary if-else statement to look which value is bigger, you can also use the functionality of the class java.lang.Math like this
Scanner s = new Scanner(System.in);
System.out.print("Enter the first integer:");
int first = s.nextInt();
System.out.print("Enter the second integer:");
int second = s.nextInt();
int small = Math.min(first, second) ;
int big = Math.max(first, second);
System.out.println("How many integers are between " + small + " and " + big + "???");
System.out.println("Answer : " + (big - small + 1));
// To print the numbers
for (int i = small; i <= big; i++)
System.out.print(i + " ");
In Java 8 you can do it in this way using IntStream
int lowNum = Integer.parseInt(minInput.getText());
int highNum = Integer.parseInt(maxInput.getText());
StringBuilder sb = new StringBuilder();
IntStream.rangeClosed(lowNum, highNum).forEach(no -> {
sb.append(no);
});
outputLabel.setText(sb.toString());
Concatenate each of the values to your String, as is you get only the last value.
int lowNum = Integer.parseInt(minInput.getText());
int highNum = Integer.parseInt(maxInput.getText());
StringBuilder sb = new StringBuilder(String.valueOf(lowNum));
for (int i = lowNum + 1; i <= highNum; i++) {
sb.append(", ").append(i);
}
outputLabel.setText(sb.toString());
As you are incrementing value first so you can try this approach
import java.util.Scanner;
public class Practice2 {
public static void main(String[] args) {
int num1;
int num2;
Scanner input= new Scanner(System.in);
System.out.println("Input one integer");
num1= input.nextInt();
System.out.println("Enter another integer");
num2= input.nextInt();
while(num1<num2-1) {
num1 += 1;
System.out.println(num1);
}
while(num2<num1-1){
num2+=1;
System.out.println(num2);
}
}
}
You can use a for-loop for this purpose. Though you would need to include some logic to ensure that num1 is the smaller of the two numbers.
Note the num1 + 1 is there to make the first number non-inclusive.
A for-loop is broken down into 3 components
start: i = num1 + 1
condition: i <= num2
ensure i is less than or equal to num2
action: i++
after each iteration, i will be incremented by 1
import java.util.Scanner;
public class Practice2 {
public static void main(String[] args) {
int num1;
int num2;
Scanner input= new Scanner(System.in);
System.out.println("Input one integer");
num1 = input.nextInt();
System.out.println("Enter another integer");
num2 = input.nextInt();
for (int i = num1 + 1; i <= num2; i++) {
System.out.println(num2);
}
}
}
Wrong main() method declaration. You have to pass an array as the only parameter for this function.
Then either declare your start and stop variables as local, and do the job inside the main method itself, or create a new function you call from the main() method.
No more explanation needed, this is Java basics. You should read a Java lesson.
There are 2 problem with this code
- Main method doesn't have proper signature
make main like
public static void main(String ar[]){
}
and create another static method to accept two int variable
- duplicate local variable declaration
remove
int i;
You already declare and initialize as a part of for loop
It will give you duplicate local variable error
The print method will simply convert its argument to a string and write out the result. Therefore, if you want to display the two numbers concatenated, you will need to do this yourself by converting them to a string (either explicitly, or using "" as you've already mentioned).
If you want to avoid building the string yourself, you'd probably need to use the printf() method:
System.out.printf("%d%d", a, b);
try
System.out.print(a+""+b)
or
System.out.print(a+" "+b)
if you want a space between them
FirstNumber and SecondNumber are assigned to 0, but we actually want to get it assigned from Scanner. here are some useful examples. https://www.w3schools.com/java/java_user_input.asp
the while loop does nothing because the conditional code is an empty statement. the link might be very helpful. https://www.tutorialspoint.com/java/java_while_loop.htm
import java.util.Scanner; public class EveryOther { public static void main(String[] args) { int FirstNumber = 0; int SecondNumber = 0; Scanner scanner = new Scanner(System.in); System.out.print("FirstNumber: " ); FirstNumber = scanner.nextInt(); System.out.print("Second Number: "); SecondNumber = scanner.nextInt(); while (FirstNumber < SecondNumber){ System.out.println(FirstNumber); FirstNumber = FirstNumber + 2; } }}
So suppose you have your firstNumber = 5 and secondNumber = 10 then every other number starting from firstNumber in this case will be 5,7,9 that means you just have to increase firstNumber's value by 2 in every iteration of loop. It can be done as follows:
while (firstNumber < secondNumber ){
System.out.println(firstNumber );
firstNumber = firstNumber + 2;
}
Hope it helps you understand.
You cannot decide that a number if prime if it is not divisible by a number. You need to add to the result at the end of the inner loop.
There are quite a few problems in your code. Here's the working one
for(int i= beg; i<= end; i++) {
boolean prime = true; //start by assuming the current number is prime
for(int j=2; j<i; j++) { // Loop till j < i
if(i%j == 0) {
prime = false; //Set the current number as not prime if it is divisible by any number lesser than it
}
}
if (prime) {
res += i+ " "; //Add to result
}
}
Note: As sanit@ answer says, you can terminate the loop much earlier. Refer to this to know why looping till square root of the number is enough.
The answers above aren't enough to solve this. At any run of the second loop, you wanna check if the i enter every if of any j. Define a boolean,for each i, check if for each j if j is dividing him, (while j isn't i itself). If so, then don't do anything, and keep running.Otherwise, concatenate i with the res so far, and print it at the end;
boolean flag=true;
for(int i= beg; i<= end; i++)
{
for(int j=2; j<= end; j++)
{
if(i%j== 0 && i!=j)
{
flag=true;
res= i+ " ";
}
}
if(!flag){
res+=i +" ";}
flag=false;
}
return res;
}