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 integerint 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
}
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;
}
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;
}
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); } }
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
first read nd write all individual digits then goto sum funtion use by adding all individual digits..
x -=y;
Action Script 1.0 to Action Script 3.0 depending on the version of Flash you are using.
Using while loop, write a program which calculates the product of digits from 1 to 5 and also show these no's vertically.
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"&even end if
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.
x -=y;
Action Script 1.0 to Action Script 3.0 depending on the version of Flash you are using.
no thanks
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 no's vertically.
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"&even end if
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 reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?
Reference:cprogramming-bd.com/c_page2.aspx# reverse number
seq 1 2 99
Reference:cprogramming-bd.com/c_page1.aspx# array programming
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; } } }