Find Sum Of Digits Of A Number

24 comments - Post a comment

/* Simple Java Program To Find the Sum of digits of number */

class SumDigit
{
public static void main(String args[])
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}

 
This Post has 24 Comments Add your own!
DJ - October 3, 2008 at 7:20 AM

when i run this program this is showing....
C:\jdk\bin>javac Sumdigit.java
C:\jdk\bin>java Sumdigit
Exception in thread "main" java.lang.NoClassDefFoundError: Sumdigit (wrong name:
SumDigit)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

C:\jdk\bin>javac Sumdigit.java

C:\jdk\bin>java Sumdigit
Exception in thread "main" java.lang.NoClassDefFoundError: Sumdigit (wrong name:
SumDigit)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

"can't find where the problem is ??"

Lionel - October 3, 2008 at 8:15 PM

@ dj

while running d code type

java SumDigit < argument>
eg :
java SumDigit 45

Unknown - January 11, 2009 at 7:25 AM

if i enter 120345 thn what would be the result??

Anonymous - January 11, 2009 at 3:39 PM

As per the program the sum should be 15..

Fabregas - June 8, 2009 at 9:20 PM

it's not working........
you must correct it...........

Prashanth - August 11, 2009 at 6:30 PM

Copy this rectified code and compile with file name SumDigit.java. At execution java SumDigit a.
a-> any integer.
the code is..

class SumDigit
{
public static void main(String args[])
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}

CinderblockClock - October 27, 2009 at 8:06 AM

I modified that program and when I tried this it worked for me, but I used a Scanner class to prompt for the value. It's essentially the same as what was already posted though:

import java.util.Scanner;

class SumDigit {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
int sum;
int i;
int a;
int Digit;

a = Input.nextInt();
sum = 0;
for(i=1;i<=10;i++)
{
Digit = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("The sum of the digits is "+sum);
}

Unknown - November 6, 2009 at 4:10 AM

public int sumdigits(int n) {
int t=0;
do
{t+= n%10;n/=10;}
while (n>0);
return t;
}

this method does the same is shorter and uses less memory

Wasim - January 18, 2010 at 10:03 PM

The logic used is cool. But it would be better if you use while(a != 0) instead of repeating for loop 10 times.

Anonymous - June 21, 2010 at 1:31 PM

the loop in sum of didigts is wrond
it shoukd be while(a<10)

Unknown - June 21, 2010 at 1:31 PM

the loop in sum of didigts is wrond
it shoukd be while(a<10)

Unknown - August 9, 2010 at 7:17 PM

Thanks man! Great help.. cheers!

Anonymous - September 5, 2010 at 10:39 PM

how about summing only the odd or even placed integers? how could the code be modified to do that?

Anonymous - October 6, 2010 at 1:59 PM

try dis
import java.util.Scanner;

public class SumOfDigits {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
System.out.print("Enter a positive integer: ");
n = in.nextInt();
if (n <= 0)
System.out.println("Integer you've entered is nonpositive.");
else {
int sum = 0;
// algorithm step by step
// base: sum = 0, n = 123
// step1: n % 10 = 3, n / 10 = 12
// sum = 3, n = 12
// step2: n % 10 = 2, n / 10 = 1
// sum = 5, n = 1
// step3: n % 10 = 1, n / 10 = 0
// sum = 6, n = 0
// stop: (n != 0) is false
while (n != 0) {
// add last digit to the sum
sum += n % 10;
// cut last digit
n /= 10;
}
System.out.println("Sum: " + sum);
}
}
}

Anonymous - February 2, 2011 at 7:18 PM

GOOD WORK THANK'S

123 - April 26, 2011 at 12:48 PM

i want to sum of all digits of negative(-ve) number also then....
U sum only for positive number only.. i want negative also...
could you please help me...
Possible to send me the solution in my mail id.

Mudita - August 14, 2011 at 11:20 PM

thanks a lot..

Arshad Hussain - September 23, 2011 at 11:01 PM

class Check
{
public static void main(String args[])
{
int n=Integer.parseInt(arg[0]);
int i=1, s=0;
while(i<=n/2)
{
if(n%i==0)
s=s+i;
i++;
}

when i run this program then following error is produced..
D:\good>javac Check.java
Check.java:5:cannot find symbol
symbol : variable arg
location : class Check
int n=Integer.parseInt(arg[0]);
1 error
plz tell me problem.
if(s==n)
System.out.println("Perfect");
else
System.out.println("Not Perfect");
}
}

prabh738 - October 29, 2011 at 8:09 PM

its dosent working properly......

prabh738 - October 29, 2011 at 8:10 PM

it doesnt working....

relyk - March 15, 2012 at 3:28 AM

public static int digitSum( int nbr )
{
int digit = 0;
int sum = 0;
while( nbr != 0 ) // Returns 0 after all digits pushed passed decimal.
{
digit = nbr % 10; // Returns first digit. Ignores decimal part of number because type int.
sum = sum + digit; // Add digit to sum.
nbr = nbr / 10; // Cut the digit from the decimal.
}
return sum;
}

Sadikhasan Palsaniya - May 10, 2012 at 2:18 PM

There are two problems in your program:
1). Correct SumDigit.java instead of Sumdigit.java and compile and run it.

2). Your loop iterate 10 times which is fix but loop iterate is depends on entered number and condition of look like this

while(a>0){
}

correct code and implement it.....
Correct?

Anonymous - May 14, 2012 at 2:09 PM

use
import java.io.*;
public class sum_of_dig
{
public static void main(String args[])throws IOException
{
BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
int a, b=0, c=0;
System.out.println("Enter a Number");
a=Integer.parseInt(x.readLine());
while(a>0)
{
c=a%10;
b=b+c;
a=a/10;
}
System.out.println("The sum is:"+b);
}
}

Anonymous - May 14, 2012 at 2:24 PM

import java.io.*;
public class sum_of_dig
{
public static void main(String args[])throws IOException
{
BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
int a, b=0, c=0;
System.out.println("Enter a Number");
a=Integer.parseInt(x.readLine());
while(a>0)
{
c=a%10;
b=b+c;
a=a/10;
}
System.out.println("The sum is:"+b);
}
}
use this it works...........

Post a Comment