1import java.util.Scanner;
2public class SumDigit {
3 public static void main(String[] argh){
4 System.out.println("Enter int");
5 Scanner input = new Scanner(System.in);
6 int n = input.nextInt();
7 int sum = 0 ;
8 int i = 0;
9 print( i =Sum_of_digits(sum,n));
10 }
11
12 public static int Sum_of_digits(int sum,int n){
13 if(n<=0)
14 return sum ;
15 sum += n%10;
16 return Sum_of_digits(sum,n/10);
17 }
18
19 public static void print(int n){
20 System.out.print(n);
21 }
22}
1long number, sum;
2Scanner sc = new Scanner(System.in);
3System.out.println("Enter any DIGIT number: ");
4number = sc.nextLong();
5sc.close();
6// For Logic for adding all digits in the given number
7for (sum = 0; number != 0; number /= 10) {
8 sum += number % 10;
9 }
10System.out.println("ForLoop Sum of ALL digits: " + sum);
1import java.io.*;
2public class sd
3{
4 public static void main(String [] args)throws IOException
5 {
6 InputStreamReader hi = new InputStreamReader(System.in);
7 BufferedReader in = new BufferedReader(hi);
8 System.out.println("Enter the number");
9 int num=Integer.parseInt(in.readLine());
10 int sum=0;
11 while(num>=0)
12 {
13 int rem=n;
14 sum+=rem;
15 int quo=n/10;
16 n=quo;
17 }
18 System.out.println(sum);
19 }
20}
1//To find the sum of even digits in a given Number
2
3 public static int getEvenDigitSum(int number){
4 if(number<0){
5 return -1;
6 }
7 int finalNumber=0;
8 while(number>0){
9 if((number%10)%2==0){
10 finalNumber+=number%10;
11
12 }
13 number=number/10; //takes out last digit to test the next digit
14
15
16 }
17 return finalNumber;
18 }
19//Output: ex:number=12323 -- Ans: 4
1public class SumNatural {
2
3 public static void main(String[] args) {
4
5 int num = 100, sum = 0;
6
7 for(int i = 1; i <= num; ++i)
8 {
9 // sum = sum + i;
10 sum += i;
11 }
12
13 System.out.println("Sum = " + sum);
14 }
15}