answersLogoWhite

0


Best Answer

import java.util.*;

public class Digit

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter Number:");

String r = sc.nextLine();

int[] b = new int[r.length()];

String [] a = new String[r.length()];

int k = 0;

System.out.println("\n");

System.out.print("The Digits of the number are: ");

for (int i = 0; i < r.length(); i++)

{

a[i] = r.substring(i,i+1);

System.out.print(a[i]);

System.out.print(" ");

k = k + Integer.parseInt(a[i]);

}

System.out.println("\n");

System.out.print("The sum of the digits is: ");

System.out.println(k);

}

}

Output:

C:\>java Digit

Enter Number:123456789

The Digits of the number are: 1 2 3 4 5 6 7 8 9

The sum of the digits is: 45

Modified while loop

Answer// start off with an integer

int n = some_integer;

// this will keep track of the sum of the digits of n...

// we set it to n initially to simplify our loop logic

int sum_digits = n;

// this loop is to handle cases where the sum of the digits is initially greater than 10

// and we need to sum the digits again

while(sum_digits >=10) {

// reset sum_digits

sum_digits = 0;

// use log function to get the number of digits in n

int num_digits = (int) Math.log10(n) + 1;

// here is the actual work of the algorithm...

do {

// (n % 10) will return the last (right-most) digit of n,

// which we will add to our sum

sum_digits += n % 10;

// now divide n by 10 to get rid of the digit we just added

n /= 10;

}while( n > 0 ); // loop until we're done with this number

// set n to the sum of the digits in case we need to loop again

n = sum_digits

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

public static final int sumDigits(final int n) {

int _n = n;

int sum = 0;

while( _n > 0 ) {

sum += (_n % 10); // add last digit of _n to sum

_n /= 10; // divide _n by 10 to "chop off" the last digit

}

return sum;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

public static final int getSumDigits(int n) {

int sum = 0;

while( n != 0 ) {

// Add right most digit to sum

sum += n % 10;

// Chop off right most digit from n

n /= 10;

}

return sum;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

import java.util.*; class SumOfDigits { public static void main(String args[]) { int sum = 0; System.out.println("Enter multi digit number:"); Scanner input = new Scanner(System.in); int n = input.nextInt(); int t = n; while (n > 0) { int p = n % 10; sum = sum + p; n = n / 10; } System.out.println("sum of the digits in " + t + " is " + sum); } }

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

There's no direct method to sum up the digits in a string.

You can try the below code instead:

int i=10;

int j=20;

Integer digit=new Integer(i);

Integer digit1=new Integer(j);

Integer result=(digit+digit1);

String sum=result.toString();

System.out.println("Sum of i and j is : "+sum);

We cannot dereference an int to string. But we can use Wrapper class to convert it to String.

Hope it helps you..

Surendra.G

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

first read nd write all individual digits then goto sum funtion use by adding all individual digits..

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a java script program to find sum of the digits of a given integer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program to subtract integer y from integer x?

x -=y;


How do you write a flash program?

Action Script 1.0 to Action Script 3.0 depending on the version of Flash you are using.


Write a Shell program to find the sum of cube of individual digits of a number?

no thanks


How may ways are there to write 3 digit positive integer using 123456789 digits?

9*9*9 = 729 ways.


Using while loop write a program which calculates the product of digits from 1 to 5 and also show these nos vertically?

Using while loop, write a program which calculates the product of digits from 1 to 5 and also show these no's vertically.


Write a VB script program to calculate the sum of first 20 even numbers?

dim a(20) as integer dim i as integer dim n as integer private sub command_click() for i 1 to 20 Next i a(i) = inputbox ("enter any no.""no.""0") Next i for i = 1 to 20 print a(i) n=n/i if n mod 2 = 0 then Next i end if msgbox "even numbe"&amp;even end if


How do you Write A program in c language for checking a diagonal matrix?

Write a program in c++ that take input in a integer matrix of size 4*4 and find out if the entered matrix is diagonal or not.


Write a program that input a positive integer and prints a triangle using for loop?

write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?


How do you Write a C program to reverse an Integer number.?

Reference:cprogramming-bd.com/c_page2.aspx# reverse number


Write a program in shell script to display odd number from 0 to 100?

seq 1 2 99


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming


How would i write a program that takes an integer as an input and reverses its digits as an output?

In java you can do it like this: import java.util.Scanner; public class Sample { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Input an integer: "); int i = input.nextInt(); System.out.print("reverse:"); while(i != 0) { System.out.print(i % 10); i = i / 10; } } }