Answers for "sum of digits java"

1

sum of digits java

import java.util.Scanner;
public class SumDigit {
    public static void main(String[] argh){
        System.out.println("Enter int");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int sum = 0 ;
        int i = 0;
        print( i =Sum_of_digits(sum,n));
    }

    public static int Sum_of_digits(int sum,int n){
        if(n<=0)
            return sum ;
        sum += n%10;
        return Sum_of_digits(sum,n/10);
    }

    public static void  print(int n){
        System.out.print(n);
    }
}
Posted by: Guest on July-13-2021
0

Java program to find the sum of all the digits in the inputted number

long number, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any DIGIT number: ");
number = sc.nextLong();
sc.close();
// For Logic for adding all digits in the given number
for (sum = 0; number != 0; number /= 10) {
	sum += number % 10;
	}
System.out.println("ForLoop Sum of ALL digits: " + sum);
Posted by: Guest on November-12-2020
1

sum of digits in java

import java.io.*;
public class sd
{
  public static void main(String [] args)throws IOException
  {
     InputStreamReader hi = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(hi);
        System.out.println("Enter the number");
        int num=Integer.parseInt(in.readLine());
    int sum=0;
    while(num>=0)
    {
      int rem=n;
      sum+=rem;
      int quo=n/10;
      n=quo;
    }
    System.out.println(sum);
  }
}
Posted by: Guest on November-06-2020
0

to get sum of even digits java

//To find the sum of even digits in a given Number

    public static int getEvenDigitSum(int number){
        if(number<0){
            return -1;
        }
        int finalNumber=0;
        while(number>0){
           if((number%10)%2==0){
               finalNumber+=number%10;

           }
           number=number/10; //takes out last digit to test the next digit


        }
        return finalNumber;
    }
//Output: ex:number=12323 -- Ans: 4
Posted by: Guest on September-16-2020
0

sum of numbers in java

public class SumNatural {

    public static void main(String[] args) {

        int num = 100, sum = 0;

        for(int i = 1; i <= num; ++i)
        {
            // sum = sum + i;
            sum += i;
        }

        System.out.println("Sum = " + sum);
    }
}
Posted by: Guest on July-22-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language