Java Program To Find Sum of Individual Digits of Given Number - FindersAdda : Tutorials of Make Money, Computer or Internet

Monday, 20 November 2017

Java Program To Find Sum of Individual Digits of Given Number




In this article, I am going to share a Java program which find sum of individual digits of a given number that means it takes input from user at run time and then produce output.

java program





For example, if user take input a number like 52364 then the output will be 


              5 + 2 + 3 + 6+ 4 = 20



import java.util.Scanner;
class Individual
{
       public static void main(String[] args)
        {
                Scanner sc=new Scanner(System.in);
                int n,r,s=0;
                System.out.print("Enter Value of n :");
                n=sc.nextInt();
                int t=n;
                while(n>0)
                {
                        r=n%10;
                        s=s+r;
                        n=n/10;
                }
                System.out.println("Sum of each digit of Number = "+s);
         }

}




- : Output : - 


Output of the above program



Explanation : - In the above program, we have taken a class which name is Individual. In the main method, we have made object of Scanner class to take input and then we have entered the value of n and put the value of n in variable t. After that, while loop is executed to check n is greater than zero. If condition is true then the statement is executed.


Final Words

I hope you liked this post so much. If you loved this post, Then, share with your friends and also give some suggestion that what should i improve in this post. Thank You So Much...... 

About Author

SUMIT KUMAR GUPTA
SUMIT KUMAR GUPTA

Author & Editor

Myself Sumit Kumar Gupta & 21 year's old person. I am a programmer and blogger. I spend much more time on programming and blogging and helps other programmers and bloggers.

0 comments:

Post a Comment

We are eager to see your comments.
Comment Rule :

1. Don't use any link in the comment box. If you have any problem related to link then contact me with the contact form.

2. Don't use the wrong word if you write the wrong word in comment box then it will get spam.