Write a program that prints the even number between [n-m] [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
using while loops only i need a program that reads 2 integers n, m (n<= m) and prints all even numbers between n and m (inclusive.) For example if the input values are 2 and 9 the output must be 2 4 6 8. Or for the input 3 and 12 the program must produce 4 6 8 10 12.
This is what I imagine, but it does not work!
Please help!!!!
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value-> ");
int k = input.nextInt();
input = new Scanner(System.in);
System.out.print("Enter a larger value-> ");
int j = input.nextInt();
int i = 1;
while (i >= k) {
System.out.println(i);
i = i + 2;
System.out.println(i);
if (i < j) {
i = i + 2;
System.out.println(i);
}
}

Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value-> ");
int k = input.nextInt();
input = new Scanner(System.in);
System.out.print("Enter a larger value-> ");
int j = input.nextInt();
int i = k;
if(i % 2 == 1)
i++;
while (i <= j) {
System.out.println(i);
i = i + 2;
}

Try this, I have modified int i = k, while loop condition i < = j & added logic for even number
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value-> ");
int k = input.nextInt();
input = new Scanner(System.in);
System.out.print("Enter a larger value-> ");
int j = input.nextInt();
int i = k;
while (i <= j) {
if (i%2 == 0) {
System.out.println(i);
}
i++;
}

I did it with for , but maybe i'm late lol
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value-> ");
int k = input.nextInt();
System.out.print("Enter a larger value-> ");
int j = input.nextInt();
for (int i=k;i<=j; i++){
if (i%2 == 0){
System.out.println(i);
}
}

Here is my solution with little bit of help so you can learn
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value-> ");
int k = input.nextInt();
// >> input = new Scanner(System.in); do not need this line
System.out.print("Enter a larger value-> ");
int j = input.nextInt();
// int i = 1; you do not need extra value you can reuse k
if ( k % 2 != 0){ // number is odd
++k
}
while(k < j) {
System.out.println("" + k);
k += 2;
}

Related

print the nth digit of a number in JAVA

I want to print nth digit of a number (from left to right) in JAVA. Here's what I've tried, I'm a total noob.
public class NewClass {
public static void main(String args[]) {
System.out.println("enter number: ");
Scanner s = new Scanner(System.in);
int number = s.nextInt();
System.out.println("enter n: ");
Scanner n = new Scanner(System.in);
int num = n.nextInt();
for (int i = 1; i <= num; i++) {
number = number / 10;
};
System.out.println("The nth Digit = " + number);
}
Following #ryan 's explanation, I found a solve in my way.
package com.mycompany.lab;
import java.util.Scanner;
int reversed = 0, digit;
System.out.println("enter number: ");
Scanner s=new Scanner(System.in);
int number=s.nextInt();
while(number != 0) {
// get last digit from num
digit = number % 10;
reversed = reversed * 10 + digit;
// remove the last digit from num
number /= 10;
}
System.out.println("enter n: ");
Scanner n=new Scanner(System.in);
int num=n.nextInt();
for(int i=1;i<num;i++){
reversed = reversed / 10;
};
while(reversed > 10){
reversed = reversed % 10;
}
System.out.println("The nth Digit = " + reversed);
}
}

Code in Java to add two numbers is printing out sum backwards?

The following Java program (prints out addition problem of two given numbers) is printing the answer backwards ( for example, 563 instead of 365). How would I modify it so that it prints out the correct answer?
Note: I know that Im completing this problem in an unnecessarily complicated manner, but this is because we are only allowed to use primitive data types.
Thankyou.
//getting the number from user
Scanner linput = new Scanner(System.in);
System.out.print("Enter the first number:");
num = linput.nextInt();
System.out.println ("Enter the second number:");
num2 = linput.nextInt();
System.out.println (num);
System.out.println ("+");
System.out.println(num2);
System.out.println ("=======");
//making a copy of the input number
temp = num;
temp2 = num2;
//counting digits in the input number
while(num > 0)
{
num = num / 10;
count++;
}
while(num2 > 0)
{
num2 = num2 / 10;
count2++;
}
int answer = 0;
while(temp > 0 && temp2>0)
{
digit = temp % 10;
temp = temp / 10;
count--;
digit2 = temp2 % 10;
temp2 = temp2 / 10;
count2--;
answer = digit+digit2;
System.out.print(answer);
}
You problem statement is very unclear. If it's just simple addition of two numbers then you've really overcomplicated your implementation.
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number:");
int num = input.nextInt();
System.out.print("Enter the second number:");
int num2 = input.nextInt();
System.out.print(num + " + " + num2 + " = " + (num + num2));
I totally agree with all comments that this is over complicated to do a sum. But the answer to the initial question why the result is printed backwards is that that you calculate the sum from right to left (units, tens, hundreds, ...) but print them left to right (default behavior of print)

Create a java application that displays the sum of the digits of a number

I am relatively new to Java and I'm currently learning while, do while and for loops. I want to create an application that displays the sum of the digits of a number using these concepts but I have no idea how. I previously created an application that displayed ONLY THE DIGITS of a number. Here it is.
int digit;
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter a positive integer: ");
digit = input.nextInt();
} while (digit <= 0);
input.close();
String sdigit = digit + "";
for (int i = 0; i < sdigit.length(); i++){
System.out.println(sdigit.charAt(i));
}
I'm trying to think of a possible way to expand on this program, but I have no idea why. Once again, this program is not what I need, what I need is somehow to sum the digits of a number using for or while loops. Thank you!
not much code has to be added for summing the digits :
First solution : using a substract with '0' character
int digit;
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter a positive integer: ");
digit = input.nextInt();
} while (digit <= 0);
input.close();
String sdigit = digit + "";
int sum=0;
for (int i = 0; i < sdigit.length(); i++){
System.out.println(sdigit.charAt(i));
sum = sum + (sdigit.charAt(i) - '0');
}
System.out.println("Sum is : "+sum);
Second solution : using Integer.parseInt which converts String to int :
int digit;
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter a positive integer: ");
digit = input.nextInt();
} while (digit <= 0);
input.close();
String sdigit = digit + "";
int sum=0;
for (int i = 0; i < sdigit.length(); i++){
System.out.println(sdigit.charAt(i));
sum = sum + Integer.parseInt(sdigit.subString(i,i+1));
}
System.out.println("Sum is : "+sum);
int digit;
System.out.println("Enter a positive integer: ");
number= Integer.parseInt(System.console().readLine());
int sum=0;
int currDigit = 0;
while( number / 10 > 0) {
currDigit = number % 10; //fetching last digit
System.out.println(currDigit);
sum = sum + currDigit;
number = number / 10;
}

Java - How to add separated numbers

This is my code:
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
String happyNumber = input.nextLine();
int happyNum = Integer.parseInt(happyNumber);
happyNum *= happyNum;
int answer = 0;
for (char ch : Integer.toString(happyNum).toCharArray()) {
int digit = ch - '0';
answer = digit * digit;
System.out.print(answer);
}
For example:
Enter a number:7
The output is:
16
81
Now guys, I want to add 16 and 81. The sum will be 97. I have tried research and all but still, I can't solve this simple problem.
Use a sum to keep track of your total =)
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
String happyNumber = input.nextLine();
int happyNum = Integer.parseInt(happyNumber);
happyNum *= happyNum;
int answer = 0;
int sum = 0; //NEW
for (char ch : Integer.toString(happyNum).toCharArray()) {
int digit = ch - '0';
answer = digit * digit;
sum = sum + answer;//NEW
System.out.print(answer);
}
System.out.print("Sum: " + sum);//NEW
Try and understand the change of code. I added //NEW to each line I added

How can I write this same statement using for loops instead of while loops?

The idea here is to learn how to apply different methods to the same problem in order to learn and see the differences
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value-> ");
int k = input.nextInt();
input = new Scanner(System.in);
System.out.print("Enter a larger value-> ");
int j = input.nextInt();
int i = k;
while (i <= j)
{
if (i%2 == 0)
{
System.out.println(i);
}
i++;
}
I am trying this, but it is clearly wrong, how can I fix it, or where should i look?
for(i<=j;i % 2 ==0;i++)
{
System.out.println(i);
}}
When doing conversion from one loop to the other, it will be much easier if we first understand the intention of the original loop. From what you have:
while (i <= j){
if (i%2 == 0)
System.out.println(i);
i++;
}
In plain English, it means:
From the lower bound input to the upper bound input, print out all the even numbers (inclusive of zeroes).
Now based on that, we write a for-loop.
for(int x=k, x<=j; x++) //k is lower bound, j is upper bound
if(x % 2 == 0) //if current number is even or 0
System.out.println(x); //print that number
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value-> ");
int k = input.nextInt();
input = new Scanner(System.in);
System.out.print("Enter a larger value-> ");
int j = input.nextInt();
int i = k;
for (;i <= j;i++) {
if (i%2 == 0) {
System.out.println(i);
}
}
Translated in for loop.
for (int i = k; i <= j; i++) {
if (i%2 ==0) {
System.out.println(i);
}
}

Categories