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
Related
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);
}
}
I'm trying to ask the user to input a three digit number, then have the code assign a new variable to the char of each digit using charAt() and put each digit into an array. So far it allowed me to input a number, but then it stops and doesn't do anything else, so I think it is a problem with this part. How would you do that?
The purpose is so that the computer with generate a three digit number, ask the user to input a number, then analyze the numbers to see how many digits of the guessed number are the same as in the generated number and how many of the correct digits are in the correct place. So if the generated number is 180, and you guess 481, then the digits correct would be 2 and the places correct would be 1.
import java.util.Scanner;
public class Main {
public static void main(String[] args){
char[] array = new char [3];
for (int i = 0; i < array.length; i++){
array[i] = (char)(Math.random() * 9);
}
char[] guess = {0, 0, 0};
System.out.println("I have a three digit number with no repeating digits. Guess the number.");
while (guess != array){
Scanner input = new Scanner(System.in);
char number1 = input.next().charAt(0);
char number2 = input.next().charAt(1);
char number3 = input.next().charAt(2);
guess[0] = number1;
guess[1] = number2;
guess[2] = number3;
int digit = 0;
int place = 0;
for(int n = 0; n < array.length; n++){
for(int d = 0; d < array.length; d++){
if(array[n] == guess[d]){
digit++;
}
}
}
for(int r = 0; r < array.length; r++){
if(array[r] == guess[r]){
place++;
}
}
System.out.println("Correct digits: " + digit);
System.out.println("Correct places: " + place);
}
System.out.println("Congratulations, you got it");
}
}
Scanner input = new Scanner(System.in);
String num = input.next();
char number1 = num.charAt(0);
char number2 = num.charAt(1);
char number3 = num.charAt(2);
To fix the problems you'll run into after this:
array[i] = (char) (Math.random() * 9 + '0');
You need to store the character correctly. Before, you were storing the ascii codes 0 - 9.
while (guess[0] != array[0] || guess[1] != array[1] || guess[2] != array[2]){
You need to compare each value of the array individually.
You need to store the number before sampling the characters.
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;
}
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;
}
I am working on an assignment which prompts the user to input an integer, and displays that integer with the digits separated by spaces, and provides the sum of those digits. I have this working, but my individual digit display is form the last digit to the first. How can I make it display the digits from first to last?
Here is what I have so far:
import java.util.*;
public class SeparateAndSum{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int num, temp, sum;
System.out.print("Enter a positive interger: ");
num = console.nextInt();
System.out.println();
temp = num;
sum = 0;
do
{
temp = num % 10;
sum = sum + num % 10;
num = num / 10;
System.out.print(" " + temp + " ");
}while (num > 0);
System.out.println("The sum of the digits = " + sum);
}
}
One option would be to use the String#valueOf(Integer) method.
Example
int input = 12345;
String inputStr = String.valueOf(input);
for(char c : inputStr.toCharArray()) {
// Print out each letter.
}
if you use the method String.valueOf(12345)
you can easily reverse the String with the method in this library:
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#reverse(java.lang.String)
StringUtils.reverse(String.valueOf(input));
Here is the solution
import java.util.*;
public class SeparateAndSum{
static Scanner console = new Scanner(System.in);
int num, temp, sum;
System.out.print("Enter a positive interger: ");
num = console.nextInt();
System.out.println();
temp = num;
sum = 0;
ArrayList<Integer> values = new ArrayList<>();
do
{
temp = num % 10;
sum = sum + num % 10;
num = num / 10;
values.add(temp);
}while (num > 0);
Collections.reverse(values);
Iterator<Integer> it = values.iterator();
while(it.hasNext()){
System.out.println(" "+it.next()+" ");
}
System.out.println("The sum of the digits = " + sum);
}
}
BTW you should have to import ArrayList etc.
I would highly recommend putting the number into a String and then reading it, parsing it, and use the number however you want. As follows,
int input = 12345;
String inputString = input + "";
int sum = 0;
for (int i = 0; i < inputString.length(); i++) {
sum += Integer.parseInt(inputString.charAt(i) + "");
}
System.out.println(sum);
However an alternative way, not as pretty is..
int input = 12345;
int sum = 0;
while (input > 0) {
int i = (input + "").length() - 1;
int n = (int) (input / Math.pow(10, i));
input -= (int) (n * Math.pow(10, i));
sum += n;
}
System.out.println(sum);