/* 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);
}
}
Labels: Core Java
Subscribe to:
Post Comments (Atom)
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 ??"
@ dj
while running d code type
java SumDigit < argument>
eg :
java SumDigit 45
if i enter 120345 thn what would be the result??
As per the program the sum should be 15..
it's not working........
you must correct it...........
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);
}
}
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);
}
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
The logic used is cool. But it would be better if you use while(a != 0) instead of repeating for loop 10 times.
the loop in sum of didigts is wrond
it shoukd be while(a<10)
the loop in sum of didigts is wrond
it shoukd be while(a<10)
Thanks man! Great help.. cheers!
how about summing only the odd or even placed integers? how could the code be modified to do that?
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);
}
}
}
GOOD WORK THANK'S
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.
thanks a lot..
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");
}
}
its dosent working properly......
it doesnt working....
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;
}
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?
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);
}
}
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...........