Answers for "write a java method to compute the sum of the digits in an integer"

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 than read an integer and calculate the sum of its digits and write the number of each digit of the sum in English

import java.util.*;
public class Main 
{
	public static void main(String[] args) 
	{
        int m, n, sum = 0;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the number:");
        
        m = sc.nextInt();
        while(m > 0)
        {
            n = m % 10;
            sum = sum + n;
            m = m / 10;
        }
        
        System.out.println("Sum of Digits:"+sum);
        
        int v,a,r=0;
        
        String[] number = {"zero","one","two","three","four","five","six","seven","eight","nine"};
      
      while(sum!=0)
      {
             a=sum%10;
             r=r*10+a;
            sum=sum/10;
      }
      
      System.out.print("In Inglish : ");
      
      while(r!=0)
      {
            v=r%10;
            System.out.print(number[v]+" ");
            r=r/10;
      }
    }
}
Posted by: Guest on May-15-2021

Code answers related to "write a java method to compute the sum of the digits in an integer"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language