Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

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


    }
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #find #sum #digit #numbers #java
ADD COMMENT
Topic
Name
4+7 =