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

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 :: class, interface, or enum expected java 
Java :: android java show hide keyboard: 
Java :: convert class to java 
Java :: calling this in constructor java 
Java :: how to concatenate two strings in java 
Java :: jasypt-spring-boot 
Java :: can i have both java7 and java 11 in mac 
Java :: add int to list java 
Java :: convert object array to int array java 
Java :: remove spaces java 
Java :: spigot plugin send message 
Java :: use of randomAccessfile() in java 
Java :: java string vers int 
Java :: Java get integer color from rgb 
Java :: java string equal 
Java :: how to convert iso-8859-1 to utf-8 in java 
Java :: android studio reg get float from numeric string 
Java :: java windowbuilder multiple monitors windowed mode 
Java :: debug cxf 
Java :: localdate to string java 
Java :: how to convert infix to postfix and prefix in java 
Java :: android get user defined device name programmatically 
Java :: 2d array length in java 
Java :: transparent card background android 
Java :: jackson deserialization fail-on-unknown-properties true 
Java :: jmeter set var jsr 
Java :: split on . in java 
Java :: java file creation date 
Java :: 7877777777777777777777777 
Java :: java empty array list vs null elements 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =