JAVA Program to Find Sum of Digit of Number

JAVA Program to Find Sum of Digit of Number:

import java.io.*;
class sod
{
 public static void main(String args[])
 {
 String a="1234";
 int sum=0,rem=0,len;
 len=a.length();
 int no=Integer.parseInt(a);
 for(int i=1;i<=len;i++)
 {
  rem=no%10;
  sum=sum+rem;
  no=no/10;
 }
 System.out.println("Sum of Digit is: "+sum);
 }
}

OUTPUT:

Sum of Digit is: 10

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.