long number, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any DIGIT number: ");
number = sc.nextLong();
sc.close();
// For Logic for adding all digits in the given number
for (sum = 0; number != 0; number /= 10) {
sum += number % 10;
}
System.out.println("ForLoop Sum of ALL digits: " + sum);
// Java program to compute
// sum of digits in number.
import java.io.*;
class GFG {
/* Function to get sum of digits */
static int getSum(int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int n = 687;
System.out.println(getSum(n));
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
System.out.println("sum of digits: " + sum);
}
import java.util.Scanner;
public class Assignmet8_Q1
{
public static void main (String[] args)
{
int First_Two_Digit_Finder,last_digit_finder,Middle_digit_finder,First_Digit_Finder;
int no,sum;
Scanner sod = new Scanner(System.in);
System.out.println("Enter a number: ");
no = sod.nextInt();
First_Two_Digit_Finder = no/10; // gets first 2 numbers
last_digit_finder = no%10; // gets last digit
Middle_digit_finder = First_Two_Digit_Finder%10; //gets the last digit of First_Two_Digit_Finder
First_Digit_Finder = First_Two_Digit_Finder/10; // gets the first digit of First_Two_Digit_Finder
// sample number = 153
// First_Two_Digit_Finder value = 15
System.out.println("First number is: " + First_Digit_Finder); // output = 1
System.out.println("Second number is: " + Middle_digit_finder); // output = 5
System.out.println("Last Digit is: " + last_digit_finder); // output = 3
sum = First_Digit_Finder + Middle_digit_finder + last_digit_finder;
System.out.println("Addition of the digits is: " + sum);
}
}
public class sum
{
public static void main(int a)
{
int b =0;
int c =0;
for (int i = a ; i>0 ; i=i/10)
{
b = i%10;
c = c+b;
}
System.out.println(c);
}
}
Input: n = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15