sum digits | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

sum digits

how do i write a program that gets a positive integer from the user and then sums the digits, using remainder and division math operators. (e.g the user types in 15, the program should add 1+5 and output the result)

14th Sep 2021, 10:28 PM
Bobby Dike
Bobby Dike - avatar
8 Answers
+ 5
Bobby Dike are you referring to something like this public class Program { public static void main(String[] args) { int sum = 0; for(int i = 1;i < 10; i++){ System.out.println(sum); sum += i; } } }
15th Sep 2021, 3:35 AM
BroFar
BroFar - avatar
+ 2
Bobby Dike n=0; while n! = 0 { r=n%10; System.out.println(r) ; n=n/10; } In the above algorithm I am printing out the numbers but you can sum up the numbers (assuming you know how to do it)
14th Sep 2021, 10:42 PM
Abhay
Abhay - avatar
15th Sep 2021, 8:57 AM
Parith (Faree)
Parith (Faree) - avatar
+ 1
Bobby Dike good luck figuring it out your own i guess or if you need help copy paste the code . Tag me using @ followed by my name if you copy paste the code or share the link.
14th Sep 2021, 11:34 PM
Abhay
Abhay - avatar
0
pls can you write a code of your example. i don't quite understand
14th Sep 2021, 10:37 PM
Bobby Dike
Bobby Dike - avatar
0
is it sum+=n?
14th Sep 2021, 10:45 PM
Bobby Dike
Bobby Dike - avatar
0
Bobby Dike sum+=r
14th Sep 2021, 10:48 PM
Abhay
Abhay - avatar
0
im having an error
14th Sep 2021, 10:56 PM
Bobby Dike
Bobby Dike - avatar