Digit Sum Of Integer Is Sum Of All Digits

Oct 13, 2014

Digit sum of an integer is the sum of all its digits. For example, the digit sum of 123 is 1+2+3 = 6

and the digit sum of 99 is 9 + 9 = 18.

But how to write the programm that will count those numbers?

View Replies


ADVERTISEMENT

User To Input Integer And Then Outputs Both Individual Digits Of Number And Sum Of Digits

Feb 2, 2015

Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.

First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.

import javax.swing.JOptionPane;
public class DigitsAndSum {
public static void main (String[] args) {
String str1;
String str2;
int int1 = 0;
int int2 = 0;

[Code] ....

View Replies View Related

How To Limit Users To Enter Only 12 Digit Input And Integer Value

Feb 8, 2014

I want to limit the program to only accept a 12 digit input and an integer value but I don't know how to program it,

import java.util.Scanner;
public class testing4
{
public static void main(String []args) {
Scanner reader = new Scanner(System.in);
String input;

[Code]...

View Replies View Related

5 Digit Integer - Print If Input Number Is Palindrome Or Not

Apr 21, 2015

I have to write a program that inputs a 5 digit integer from the keyboard and prints if the input number is a palindrome or not. I got it to work, but when my result prints it says "0 is a palindrome" or "0 is not a palindrome". How can I get rid of the 0 and replace it with the number input by the user?

import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
int number;
int digit;
int temp;

[code]....

View Replies View Related

Breaking Integer Into Individual Digits?

Oct 12, 2003

I'm trying to figure out how to read in an integer and break it down into a sequence of individual digits in reverse order. Example: an integer input of 12345 gives an output of54321.

View Replies View Related

Nesting Loops - Odd / Even And Zero Digits In Integer Value

Oct 13, 2014

So I am trying to do this program but I can't figure out how to nest it properly. The prompt is :

Design and Implement an application that determines and prints the number of odd,even,and zero digits in an integer value read from the keyboard.

The catch is, strings are not allowed. This is what I have so far :

import java.util.Scanner;
 public class OddEvenZero {
 public static void main(String[] args) {
int zero = 0, even = 0, odd = 0, num, num2;

[Code] ....

View Replies View Related

Program That Accept Input Of Positive Integer And Sum All Digits

Jan 28, 2015

I am attempting to write a program that accepts input of a positive integer, reports each digit and then the sum of all digits. This is what I have so far:

*/
 package numbersum;
import java.util.*; 
public class Week4NumberSum {
static Scanner console = new Scanner(System.in); 
/**
* @param args the command line arguments

[Code]...

It works if I enter 1234567891 (10 digits)
Enter a positive integer: 1234567891
digit: 1
digit: 2
digit: 3
digit: 4
digit: 5
digit: 6
digit: 7
digit: 8
digit: 9
digit: 1

The sum of the digits is 46

But if I enter 11 digits, it fails:

Enter a positive integer: 123456789123 (12)
Exception in thread "main" java.util.InputMismatchException: For input string: "123456789123"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at week4numbersum.Week4NumberSum.main(Week4NumberSum. java:26)

java - Scanner error with nextInt() - Stack Overflow gives me some infor for InputMismatchException - which I find described as In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.

I don't understand why the 10 digit integer is OK but the 11 or > digit integer is a mismatch.

View Replies View Related

Read From User Input (integer) And Reduce It By Multiplying Its Non-zero Digits

Nov 15, 2014

Write a program that reads from the user an integer and reduce it by multiplying its non-zero digits. The result of the multiplication is a number which is to be reduced as the initial one. This process continues until an integer of one single digit is obtained. For example:

64734502 (6 * 4 * 7 * 3 * 4 * 5 * 2) 20160 (2 * 1 * 6) 12 (1 * 2) 2

Your program should display the number obtained in every iteration.

Sample run1
Enter an integer: 64734502
After iteration 1: 20160
After iteration 2: 12
After iteration 3: 2

Sample run2
Enter an integer: 97737999
After iteration 1: 6751269
After iteration 2: 22680
After iteration 3: 192
After iteration 4: 18
After iteration 5: 8

View Replies View Related

Prompt User To Input Integer And Then Output Both Digits Of Number And Their Sum

Jun 7, 2015

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. 
 
Now I have a code for spacing out the integers, and I have a separate code for adding the digits. But I am not sure how to merge them together. Both of them work independently
 
Spacing code:
import java.util.*;
public class SumoftheIntegers
{
static Scanner console=new Scanner(System.in);
public static void main(String []args) {
int num1, test, rem;
int counter = 0;

[Code]...

Now the sum of the integers code:
 
import java.util.Scanner;
public class sum
{
    public static void main(String[] args)
    {
        // Create a Scanner
        Scanner input = new Scanner(System.in);
// Enter amount
        System.out.print("Enter an integer: ");
        int integer = input.nextInt();

[Code]...

View Replies View Related

Java Lotto - Randomly Generate Five-digit Number And Prompts User To Enter Five-digit Number

Sep 25, 2014

1. Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:

- The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
-The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
-The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
-The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.

- The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.

import java.util.Scanner;
import java.util.Random;
class Hw5 {
static int getPrize(int g1, int g2, int g3, int g4, int g5,
int u1, int u2, int u3, int u4, int u5) {

[code]...

View Replies View Related

Checking If Input Is Digit?

Sep 28, 2014

which method i can use so that the program checks if the input value is a digit or else outputs a message saying that `the value entered is not a number.

View Replies View Related

Digit Separation By Three Spaces Each

Oct 14, 2014

I am having a problem of how to write a program dat will input one number consisting of five digit,separate the number and print the digits separated from one another by three spaces each.. I tried to use divisional and remainder operation but it didn't work.

View Replies View Related

Loop The Drops Off Rightmost Digit?

Oct 22, 2014

I have to write a program that asks the user to enter an integer and then the program is supposed to display the integer each time with the rightmost digit dropped off. For example if the user enters 1048576 the program would display 104857 10485 1048 104 10 1 and 0. this is what I have so far

import java.util.*;
public class Lab5 {
public static void main(String[] args){
Scanner input= new Scanner(System.in);
System.out.print("Enter An Integar Value: ");
int number = input.nextInt();
while (number != 0){
number = number / 10;}
System.out.println(number);
}
}

I am not getting any errors, however when i run the program and supply an integer it skips displaying all the integers in-between and automatically displays 0.

View Replies View Related

Permutation Of 5 Digit Number With BigInteger

Jan 23, 2014

i have tried permutation with big Integer in Java. it works fine upto 4 integer input say 3456 P 2345 but nothing happens in console when i type 5 digit input..here is my code

public class cvic {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter n and r: ");
BigInteger n = scan.nextBigInteger();
BigInteger r = scan.nextBigInteger();
System.out.println("nPr = "+fact(n).divide(fact(n.subtract(r))));

[code]....

View Replies View Related

Random 3 Digit Letter No Duplicate

Nov 25, 2014

I need to know how many four letter strings I can generate that begin with the letter "v". Now I had been testing many ways to do this but anm stuck. I can generate a single line up only a certain amount. I need it to continue till it reaches the max number. So since it needs to always start with V i eliminated the need for four and only a 3 letter string.

package javaapplication4;
import java.util.Random;
public class JavaApplication4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Random r = new Random();
int c = r.nextInt(26)+ (byte)'a';

[code]...

View Replies View Related

JSF :: InputMask - Number Of Digit Limitation

Nov 23, 2014

<p:inputMask mask="?999999999" placeHolder ="" >

But my requirement is inputMask should take no limit no. of digits(only).but it is taking no. of 9 which i have given in mask attribute..

And if i am not giving mask attribute and i am handling with keyFilter(....regx) but it is allowed to paste alphabetic(Char) also. so mask attribute is required.

View Replies View Related

How To Extract First Digit From Int Array Element (java)

Nov 18, 2014

How do you extract first int digit from an int array element (java)?

if a[3] = 45, how do I extract 4 out of 45?

View Replies View Related

How To Import TXT File That Has Color And Four Digit Number

Apr 10, 2014

I am trying to figure out how to import a txt file that has a color and a four digit number. Everything that I have tried comes up with some kind of error.

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.InputMismatchException;

[Code] ....

I couldn't find an edit button. I need to store the String and int from the txt file into an array. The problem that I keep having is that the compiler seems to want it as either two ints or two Strings.

View Replies View Related

Do Java Specs Allow Quotient That Is Off In Least Significant Digit

Jan 24, 2014

I wrote a little piece of code like so:

public class Oott
{
public static void main ( String[] arguments)
{
System.out.println( 1.0 / 13.0);
}
}

When I run this it prints out "0.07692307692307693". Is that an error? Rounded correctly it should be "0.07692307692307692", shouldn't it? The repeating series is "076923". Do the Java specs allow a value returned that's off in the least significant digit?

View Replies View Related

Can't Seem To Get Value For Integer 1 And Integer 2 To Pass With SetValue Method

Aug 10, 2014

public class MyInteger {
private int value;
public MyInteger(int number){
value = number;
System.out.println("Constructor created with value of " + value);

[code]....

I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers.

View Replies View Related

Display Ten Digit Phone Number As Both String And Long

Feb 17, 2014

I'm trying to output a ten digit phone number as a string on one file, and write another program to write it as a long because I don't know what value I need to use in order to get the code to run properly on either one.

View Replies View Related

Comparing Array Or Int To 4 Digit Number From Random Generator?

Apr 4, 2015

How do I compare an array (or maybe int?) (example: 3572) to a 4digit number from random generator? how many of the four digit match the generated numbers.

View Replies View Related

Java Programming To Input Number And Display All Its Prime Digit?

Jan 9, 2015

I am new to java programming and using bluej for programming and i have tried this question what i have have given in title ... how to find out the errors in the following program:-

class Primedig {
public void getinput(int n) {
int ctr=0;
while(n!=0) {
int r=n%10;
for(int i=1;i<=r;i++)

[code] .....

output:-

For example input is 243 the output comes only 3 not 2& 3 both.

View Replies View Related

Java Program That Randomly Generates Five-digit Lottery Number And Prompts User For Input

Sep 25, 2014

Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:

• The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
• The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
• The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
• The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
• The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.

and here is my code. I cant get it to print the right statements.

import java.util.Scanner;
import java.util.Random;
class Hw5 {
static int getPrize(int g1, int g2, int g3, int g4, int g5,
int u1, int u2, int u3, int u4, int u5) {
//code for determining the prize comparing (g1, g2, g3, g4, g5) to (u1, u2, u3, u4, u5)
if (u1 == g1 && u2 == g2 && u3 == g3 && u4 == g4 && u5 == g5)

[code]....

View Replies View Related

How To Sum Digits

Jan 30, 2015

I have to seperate a number 9876 to 6 7 8 9, to 9 8 7 6. I need to know how to sum the digits. I have gotten to this point ::

import java.util.*;
public class week4program
{
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
int num1;
int digit;
int sum = 0;

[code]....

To this point its gives me the seperate integers. OK but how do I get the variable integers into seperate containers and then add them up?My assignment is to do that, and what I have above gets me to where I have seperate digits, but how do I catch them into seperate entities to be added to a sum?

View Replies View Related

Sum Of Odd Digits Of Input

Oct 14, 2014

I have it so it gives me the sum of any digit, i just cant figure out how to get only the odd digits to add up. for example if I were to type 123. The odd numbers = 4 but using this program i get 6

class SumOfDigits {
public static void main(String args[]) {
int sum = 0;
System.out.println("Enter multi digit number:");

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved