Saturday, May 18, 2024
HomeJavaWrite a Program to Discover Sum of Digits in Java

Write a Program to Discover Sum of Digits in Java


One of many frequent programming observe query thrown to newbies is to write down a program to calculate the sum of digits in an integral quantity. For instance, if the enter is 123456 then output or sum of the digit is (1+2+3+4+5+6) = 21. An extra situation is you cannot use any third social gathering or library technique to unravel this downside. This program is just not so simple as it seems to be and that is why it is a good train, it’s essential to know some fundamental programming strategies e.g. loops, operators, and logic formation to unravel this downside. Let’s examine how we are able to remedy this downside utilizing Java programming language. So as to calculate the sum of digits, we should get digits as numbers. So your first problem is how do you get the digits as numbers?  How can we extract 6 out of 123456?

If in case you have carried out workout routines like
palindrome test or reversing quantity, then it is best to know that there’s very outdated strategy of getting final digit from a quantity through the use of modulus operator. If we do 123456percent10 then we’ll get 6, which is final digit. So as to get all digits we are able to use a loop, one thing like whereas loop.

Now our subsequent problem is how can we cut back quantity in every iteration in order that our loop will end as quickly as we’re carried out with all digits of quantity? Now coming from identical palindrome downside, you should utilize strategy of dividing quantity by 10 to eliminate final digit or cut back it by issue of 10.

For instance 123456/10 provides you with 12345, which is one digit lower than authentic quantity. So you bought your finish situation for whereas loop, test till quantity is just not equal to zero. These two strategies are essential and can be utilized in number of downside, so at all times bear in mind these.

Java program to seek out Sum of Digits in Java

Right here is our full Java program to unravel this downside. As defined in first paragraph, it doesn’t use any library technique as an alternative makes use of division and modulus operator to calculate sum of digits of a quantity.

import java.io.Console;
import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Situation;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
*
* The right way to discover sum of digits in Java
*
* @writer Javin Paul
*/
public class SumOfDigits{

    public static void primary(String args[]) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter a quantity to calculate sum of digits");
        int quantity = sc.nextInt();

        // Bear in mind quantity/10 reduces one digit from quantity
        // and numberpercent10 provides you final digit
        int sum = 0;
        int enter = quantity;
        whereas (enter != 0) {
            int lastdigit = enter % 10;
            sum += lastdigit;
            enter /= 10;
        }

        System.out.printf("Sum of digits of quantity %d is %d", quantity, sum);

        // closing Scanner to stop useful resource leak
        sc.shut();

    }

}

Please enter a quantity to calculate sum of digits
101
Sum of digits of quantity 101 is 2

Please enter a quantity to calculate sum of digits
123
Sum of digits of quantity 123 is 6

That is all on how do you calculate sum of digits of a quantity in Java. It is an fascinating train and you’re welcome to seek out different answer as properly. Attempt to not see the answer earlier than doing it as a result of if you happen to can give you logic by your personal, you’ll study so much. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments