Java Tutorial

# Java program to compute sum of digits

This simple java program is used to compute the sum of digits for the given number as user input.

```import java.util.Scanner;
class SumOfDigits
{
public static void main(String arg[])
{
long n,sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number ");
n=sc.nextLong();
for(sum=0 ;n!=0 ;n/=10)
{
sum+=n%10;
}
System.out.println("Sum of digits of a number is "+sum);
}
}

E:\Java-app>javac SumOfDigits.java

E:\Java-app>java SumOfDigits
Enter a number
4567
Sum of digits of a number is 22

```

## Java Program to Compute Sum of Digits and Product of Digits using while loop

This java program is used to calculate the sum of digits for the given number as user input.

This java program is also used to calculate the product of the digits for the given number as user input.

```import java.util.Scanner;

public class Digits {

public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Digits Sum and Product");
System.out.println("-----------------------");
System.out.print("Enter Number: ");
int number = in.nextInt();
int remainder, temp, sum=0, product=1;
temp = number;
while (temp!=0){
remainder = temp % 10;
sum = sum + remainder;
product = product * remainder;
temp = temp / 10;
}
System.out.println("Sum of digits of Number '"+number+"'': "+sum);
System.out.println("Product of digits of Number '"+number+"'': "+product);
}
}
```
Output:
```D:\Java_Programs>javac Digits.java
D:\Java_Programs>java Digits
Digits Sum and Product
-----------------------
Enter Number: 153
Sum of digits of Number '153'': 9
Product of digits of Number '153'': 15
```

## Java Program to Compute Sum of Digits and Product of Digits using for loop

```import java.util.Scanner;

public class Digits {

public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Digits Sum and Product");
System.out.println("-----------------------");
System.out.print("Enter Number: ");
int number = in.nextInt();
int remainder, sum=0, product=1;
for (int temp=number; temp!=0; temp = temp / 10){
remainder = temp % 10;
sum = sum + remainder;
product = product * remainder;
}
System.out.println("Sum of digits of Number '"+number+"'': "+sum);
System.out.println("Product of digits of Number '"+number+"'': "+product);
}
}
```
Output:
```D:\Java_Programs>javac Digits.java
D:\Java_Programs>java Digits
Digits Sum and Product
-----------------------
Enter Number: 153
Sum of digits of Number '153'': 9
Product of digits of Number '153'': 15
```

## Java Programming Computes Sum of Digits and Product of Digits using do while loop

```import java.util.Scanner;

public class Digits {

public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Digits Sum and Product");
System.out.println("-----------------------");
System.out.print("Enter Number: ");
int number = in.nextInt();
int remainder, temp, sum=0, product=1;
temp = number;
do{
remainder = temp % 10;
sum = sum + remainder;
product = product * remainder;
temp = temp / 10;
}while (temp!=0);
System.out.println("Sum of digits of Number '"+number+"'': "+sum);
System.out.println("Product of digits of Number '"+number+"'': "+product);
}
}
```
Output:
```D:\Java_Programs>javac Digits.java
D:\Java_Programs>java Digits
Digits Sum and Product
-----------------------
Enter Number: 453
Sum of digits of Number '453'': 12
Product of digits of Number '453'': 60
```

Java Tutorial