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
ADVERTISEMENT
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
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
Feb 3, 2014
import java.util.Scanner;
public class CubesSum {
public static void main (String [] args){
int input;
System.out.println("Enter a positive integer:");
Scanner in = new Scanner(System.in);
input = in.nextInt();
As you can see I'm using a while loop. For part B which is to modify to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits. So for example, 371 = 3³+7³+1³. How to do it? I need to wrap a for loop around my while loop...
View Replies
View Related
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
View Related
Aug 29, 2014
Make a program which takes input from the user (6-digit number) and check how many times each digit gets repeated in the number. Java GUI was used (Netbeans 6.5.1).This is my code. I took the input via a JTextField named tf and displayed the output on JTextArea ta.
- arrays not used as we haven't studied them yet
int num= Integer.parseInt(tf.getText());
int no=num;
if(num<100000)
ta.setText("Invalid Input. Please Enter 6 digit number");
int n1= num%10;
num=num/10; //stores last digit
[code]....
How do I omit the last 5 lines? This problem occurs whenever I input a digit with any recurring digit.What changes/additions should I make?
View Replies
View Related
Feb 4, 2015
The following code splits an integer into separate digits. It then adds 7 to each of those digits, and then finds the remainder when the result is divided by ten. To do this it uses the Modulus operator.
public class manipulateIntegers {
public manipulateIntegers() {
System.out.println("Enter a four digit Integer");
String y = System.console().readLine();
int[] list = new int[5];
[code].....
I understand completely about what modulus does vis-à-vis fetching the remainder, but I don't understand why using the modulus operator gets you each digit individually?
for example, this snippet splits the digits of an integer - but how??
int number; // = some int
while (number > 0) {
print( number % 10);
number = number / 10;
}
and why does number have to be reassigned as number / 10?
View Replies
View Related
Jan 14, 2014
How can i make a method that makes sure that a String only contains digits and "-" (dash) ... (I want to make sure that the method can see if the user has written in an okay telephone number...)
This is what I've done this far.. But the method retuns false if you type - or +
static public boolean telephoneValidation(JTextField tf) {
boolean svar;
try {
Integer.parseInt(tf.getText());
svar = true;
}
catch(NumberFormatException fel)
[code]...
View Replies
View Related
Apr 25, 2014
why exactly this isn't printing out the correct way.I'm supposed to print out 100 elements from an array but assign them random integers between 0 and 9.I've tried troubleshooting the issue and when I do it seems i'm only getting one element assigned.
public class Exercise_6_7{
public static void main(String[] args){
int[] counts = new int[100];
for(int i=0; i<counts.length; i++){
counts[i] = (int)(Math.random()*10);
[code]....
View Replies
View Related
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
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
Apr 21, 2015
I am currently trying to solve a programming problem on this site, and the problem includes working on a 100 digits of decimal places of a certain irrational number. I tried using BigDecimal class but it displays, correctly me if I am wrong, just 20+ decimal digits. Is there other way to do this?
View Replies
View Related
Nov 7, 2014
Taking two input strings that are integers such as "1234" and "567" then multiplying as you would do by hand. Trying to get them saved in the n3 new string but something is going wrong. You also need to account for the zeroes that have to be added just like on paper.
String mulN(String n1, String n2) {
int p = 0, tmp = 0; int zeros = 0;
String n3= "";
String r = "";
for(int i = n2.length()-1; i >= 0; i--) {
[Code] .....
View Replies
View Related
Mar 15, 2015
What I am trying to do is extract the digits from between the words 'deal' and 'of'. I was wondering if this was possible to do with a regular expression or if I should just use a scanner.
UNIQUE Passive - Cleave: Basic attacks deal 20% to 60% of total Attack Damage as bonus physical damage to enemies near the target on hit (enemies closest to the target take the most damage).
Is there a regular expression that I could use in order to extract the '20' and the '60' from the above text. Below is what I have tried, but does not work.
(?<=deals)(d+(?=%))(?=of)
just to make a clarification, these numbers will not be the only numbers and the strings themselves will change. Everything can be considered a variable. The only constants between each string would be the words 'deal' and 'of'. The regex pattern "d+" is not a viable expression for my case
View Replies
View Related
Apr 26, 2015
This is the code that I have so far, It will only print out the digit if it is entered first..How to I get it to print out all digits? and I am getting an error that the c is not initialized
import java.util.*;
//Write a program that prompts the user for some text. Output only the digits in that text. Hint: Use a loop and the Character.isDigit method.
public class Q1
{
[Code].....
View Replies
View Related
Jun 19, 2014
You are to write a parallel program that will receive a large array of unsorted integers containing digits from 0 to 99. The program will have to calculate the SUM and AVERAGE of ODD number digits between 25 and 75 (inclusive).
The user may specify the size or the array and the total processor that the machine has. The size of the array must be divisible by number of processor. Based on the given size, the computer will generate random integer numbers automatically to populate the array.
The program should display the status of the calculation for each processor. The result should be displayed after all calculations are completed.Error messages should be displayed appropriately
a) Write a sequential (non-parallel) program that will accomplish above task.
b) Write a concurrent (parallel) program that will produce the result of the above task.
View Replies
View Related
Sep 3, 2014
Consider the sequence of digits from 1 through N (N<=9) in increasing order: 1 2 3 4 N
Insert either a +(for addition) or a - (for subtraction) between each of the digits so that the resultant sum is zero. Print all possible combinations that sum to zero.
Example: Enter a number: 7
1+2-3+4-5-6+7=0
1+2-3-4+5+6-7=0
1-2+3+4-5+6-7=0
1-2-3-4-5+6+7=0
View Replies
View Related
Jul 21, 2014
Given a Numbers instance, whose fields are arrays of all the built-in Java numeric types (int, long, float, double, big-decimal, etc), write a method to sort all the numbers into a master list, and then print out the numbers where the number of digits past the decimal point is equal to the index of the number in the master list.
Is there a function in Java that will give me just the numbers after the decimal? I tried Decimalformat but couldn't get it to work. Here is what I have so far; however, I think I might be on the wrong track.
public class Numbers
{
public static void main(String[] args) {
Byte bNum = new Byte((byte) -50);
Integer iNum = new Integer(168);
Long lNum = new Long(100000L);
Short sNum = new Short((short) 10000);
Float fNum = new Float(12.19f);
Double dNum = new Double(23.123);
BigDecimal bd = new BigDecimal("3.14159265358979323846");
[code]....
View Replies
View Related
Feb 23, 2015
I am trying to create a method that reverses the digits of a number.
import java.util.*;
public class KDowlingWeek7 {
static Scanner console = new Scanner (System.in);
//Scanner input = new Scanner(System.in);
public static void main(String[] args) {
[Code] .....
The error I get is :
Error: cannot find symbol System.out.print(reverseDigit + " ");
Symbol: variable reverseDigit
Location: class KDowlingWeek7
View Replies
View Related
Feb 4, 2015
I have an array made that represents digits and I am trying to make a method so that if there are zeros in front of the first significant digit I want to trim them, I understand you can't re size arrays so I have created a new array, but my code doesn't seem to run correctly? Here is my code I can't figure out what is wrong I've tried everything: (I put stars around the error**)
package music;
import java.util.Random;
public class Music {
private int length; // length of the array
private int numOfDigits; // number of actual digits in the array
int[] musicArray;
[Code] .......
View Replies
View Related
May 2, 2014
I Have been playing with this for while, and I can not seem to get my head around, how I can do this. I need to get the last two digits of a year that a user will enter. For example, a date like July 2 , 2014, I want to get the last two digits of year 2014 which are "14" and divide it by 4. This will give me 3.5; how ever I will disregard the ".5" and just keep the 14. I have no problem doing the division, my biggest this is how to just get the last two digits of a year. This is what I have so far, it is basically a template of how I want my program; I just need getting the last 2 digits of a year.
public class DateCalc
{
public static void main (String[] args)
{
String month;
int day;
int year;
[Code] ....
" This will give me 3.5; how ever I will disregard the ".5" and just keep the 14 ". I meant that I just want to keep the 3 from the 3.5 that will be generated when you divide 14 by 4.
View Replies
View Related
Mar 21, 2014
For example, if i am given 9864
Imust output 4689
Without use of arrays.
View Replies
View Related
May 28, 2013
If you have an enumeration type with instances whose publicly accepted names are numbers, e.g. a Chevy 396 and I'm sorting this car according to makes and models that I've already laid out within enum types for each. Here the make instance is "Chevy" and the model instance is just "396".
I know I could make this "396" into "Ch396" and this would work programmatically. But this might confuse some people who just expect to see "396".
In short, why are enum instances not allowed to be of String type rather than just standard Java identifiers ?
View Replies
View Related
Apr 6, 2014
Where do I find a subroutine similar to this:
String formatDouble(double dblNum,int NumOfDecDigits) { }
For years I have been using output subroutines I wrote long ago.
Now I need this kind and can't remember where it is.
How do I print doubles to a fixed number of digits to right of the point?
View Replies
View Related
Jun 11, 2014
You are to write a parallel program that will receive a large array of unsorted integers containing digits from 0 to 99. The program will have to calculate the SUM and AVERAGE of ODD number digits between 25 and 75 (inclusive). The user may specify the size or the array and the total processor that the machine has. The size of the array must be divisible by number of processor. Based on the given size, the computer will generate random integer numbers automatically to populate the array.
The program should display the status of the calculation for each processor. The result should be displayed after all calculations are completed.Error messages should be displayed appropriately
a) Write a sequential (non-parallel) program that will accomplish above task.
b) Write a concurrent (parallel) program that will produce the result of the above task.
View Replies
View Related
Nov 6, 2014
I have returned with yet another problem but I think this one might be a bit easier to fix.
So the whole MO of this program is to take user input, and display the sum of the digits of the number they entered. I am supposed to do this by utilizing methods. So far this is what I have and when I compile it tells me that it "cannot find symbol", which I don't understand as I define what "m" is in the for loop. The other error is that it says:
"Exercise6_2.java:22: error: incompatible types: possible lossy conversion from long to int
return result;
^
I don't understand why it's giving me this error nor do I understand why result seems to inherently be an int. (Also the public static int sumDigits(long n) method was given to me by the book so that is what I am supposed to use).
import java.util.Scanner;
public class Exercise6_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a integer");
long n = input.nextLong();
[Code]...
View Replies
View Related