Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (emanmahmoud):

write an algorithm to calculate the sum of the digits of a three-digit number ? ANY HELP

OpenStudy (anonymous):

class program { public static void main(String args[]) { int num = 405; int h = num/100; int rem = num%100; int t=rem/10; int u=rem%10; int sum= h+t+u; System.out.println("Hundred's digit =" + h); System.out.println("Ten's digit =" + t); System.out.println("Unit's digit =" + u); System.out.println("Sum of digits =" + sum); } }

OpenStudy (da_scienceman):

Nice try the which I feel is correct!

OpenStudy (da_scienceman):

public class b1 { public int retNum(int num) double num1 = num/100; double num2 = num%100; double num3 = num2/10; double num4 = num2%10; int val = (int) (num1 +num3 +num4); return val; }

OpenStudy (da_scienceman):

The I called the function using the following:

OpenStudy (da_scienceman):

class callfunc1 { public static void main(string args []) { b1 b10 = new b1() System.out.println("The sum is:" + b10.retNum(num); } }

OpenStudy (emanmahmoud):

ok thanx for you 2.. i got it

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!