Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java program to find the sum of all the digits in the inputted number

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);
Comment

java Program for Sum of the digits of a given number

// 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));
    }
}
 
Comment

sum of digits java

   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);
    }
Comment

how to find sum of the digit of the numbers in java

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);


    }
}
Comment

program in java to find the sum of digits of a number

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);
    }
}
Comment

java sum of number

Input: n = 234
Output: 15 
Explanation: 
Product of digits = 2 * 3 * 4 = 24 
Sum of digits = 2 + 3 + 4 = 9 
Result = 24 - 9 = 15
Comment

PREVIOUS NEXT
Code Example
Java :: java get first day of the week 
Java :: java mongodb find with multiple conditions 
Java :: java stack methods 
Java :: How to convert long to string android studio - java 
Java :: useColorScheme returning light 
Java :: java store hexadecimal value 
Java :: passing array by reference java 
Java :: find maximum in array java 
Java :: how to update java on windows 10 
Java :: javafx textarea size 
Java :: java integer division tofloat 
Java :: android xml change button background 
Java :: object vs class c# 
Java :: java string length validation regex 
Java :: how to change checkbox color in android 
Java :: java for loop increment by 3 
Java :: max and min array number in java 
Java :: longest common subsequence of two strings 
Java :: final finally finalize 
Java :: how to find palindrome numbers in java 
Java :: enum java 
Java :: json array to list in java 
Java :: java multiple catch blocks 
Java :: java bufferedreader read all lines 
Java :: java swing windows 10 look and feel 
Java :: declaration of an array in java 
Java :: advantages of exception handling in java 
Java :: how to stop activity from another activity 
Java :: set text from strings.xml 
Java :: swing getsource 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =