tempNum=number; while(number>0) { remdr=number%10; total=total+remdr; number=number/10; } System.out.print("Sum of digits of number "+tempNum+" is "+total); } } Output: Enter a Number : 5385 Sum of digits of number 5385 is 21 That’s all about Java program to add digits o...
Java program to find Sum of Digits in Java Here is our complete Java program to solve this problem. As explained in first paragraph, it does not use any library method instead uses division and modulus operator to calculate sum of digits of a number. import java.io.Console; import java.ut...
how to find the sum of digits of an integer number in Java. In thefirst part, we have solved this problem without using recursion i.e. by using a while loop and in this part, we will solve it by using recursion. It's good to know different approaches to solving the same problem, ...
public static void main(String[] args) { long number; Scanner inputScanner = new Scanner(System.in); System.out.print("Enter an integer:"); number = inputScanner.nextLong(); System.out.printf("The sum of the digits in %d is %d", number,sumDigits(number)); inputScanner.close(); } ...
n = int(input("Enter the number: ")) print(sumDigits(n)) Output Enter the number: 12345 15 Method 7:The cool method We kind of have discussed this method above. But its amalgamation of different python tricks – Run n = [int(d) for d in input("Enter the number : ")] ...
import java.util.Scanner;public class N {public static int sumDigits(long n){int sum=0;while(n>0){int m=(int)(n%10);sum=sum+m;n=n/10;}return sum;}public static void main(String[] args) {try{System.out.println("请输入数字:");Scanner sc=new Scanner(System.in);long l=sc....
// Java program to find the sum of digits of a number // using the recursion import java.util.*; public class Main { static int sum = 0; public static int sumOfDigits(int num) { if (num > 0) { sum += (num % 10); sumOfDigits(num / 10); } return sum; } public static ...
19 changes: 19 additions & 0 deletions 19 SumOfDigits.java @@ -0,0 +1,19 @@ import java.util.Scanner; public class SumOfDigits { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter n No: "); int n = sc.nextInt(); int ...
1#define_for(i,a,b) for(int i = (a);i < b;i ++)23classSolution4{5public:6intsumOfDigits(vector<int>&A)7{8intmm = A[0];9_for(i,0,A.size())10{11if(A[i]<mm)12mm =A[i];13}14intS =0;15while(mm)16{17S += mm%10;18mm /=10;19}20if(S&0x1)21return0;22return...
import java.util.Scanner;public class N {public static int sumDigits(long n){int sum=0;while(n>0){int m=(int)(n%10);sum=sum+m;n=n/10;}return sum;}public static void main(String[] args) {try{System.out.println("请输入数字:");Scanner sc=new Scanner(System.in);long l=sc....