write an algorithm to calculate the sum of the digits of a three-digit number ? ANY HELP
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); } }
Nice try the which I feel is correct!
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; }
The I called the function using the following:
class callfunc1 { public static void main(string args []) { b1 b10 = new b1() System.out.println("The sum is:" + b10.retNum(num); } }
ok thanx for you 2.. i got it
Join our real-time social learning platform and learn together with your friends!