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
}
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.
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?
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.
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?
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
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; } } }