Write a Program to Read an Integer and Sum of Digits in Java

How to observe the Sum of Digits in Coffee?

Terminal updated on Jun 17,2021 11.5K Views

4 / 29 Weblog from Coffee Programs

Java is a versatile programming language. There are various methods through which we can calculate the sum of digits in Coffee. Permit us talk over those methods in detail. Below given is a list which we would use to solve our problem.

  • By using for loop
  • By using static method
  • By using recursion
  • By using control line arguments

Lets begin!

Past using for loop

Programmers normally utilizeloops to execute a prepare of statements. For  loop is used when they need to iterate a part of theprograms multiple times. It is specially used in cases where the number of iterations is fixed!

Code:

            class SumOfDigits { public static void chief(String arg[]) { long n,sum; Scanner sc=new Scanner(Organisation.in); Arrangement.out.println("Enter a number "); n=sc.nextLong(); for(sum=0 ;northward!=0 ;north/=ten) { sum+=north%10; } System.out.println("Sum of digits "+sum); } }        

Output:
Enter a number
456
Sum of digits
15

The side by side segment tell you how to use static method to attain the goal.

Past using static method

In Coffee, a static method is a method that belongs to a class rather than an case of a class. The method is accessible to every case of a course, merely methods defined in an instance are only able to be accessed by that member of a class.

Code:

            class SumOfDigits { public static void principal(String arg[]) { long n,s; Scanner sc=new Scanner(System.in); System.out.println("Enter a number "); northward=sc.nextLong(); s=sum(northward); System.out.println("Sum of digits "+s); } static int sum(long num) { int sum=0; while(num!=0) { sum+=num%ten; num/=10; } render sum; } }        

Output:
Enter a number
678
Sum of digits
21

Permit u.s.a. now motility to another method that is recursion.

Past using recursion

Lawmaking:

            import coffee.util.Scanner; course SumOfDigits  { int sum=0; int sum(long num) {  if(num!=0) { sum+=num%ten; num/=ten; sum(num); } render sum; }  public static void main(String arg[]) { long northward,res; SumOfDigits southward=new SumOfDigits(); Scanner sc=new Scanner(Organization.in); System.out.println("Enter a number "); northward=sc.nextLong();  Organization.out.println("Sum of digits "+s.sum(n));  } }        

Output:
Enter a number
345
Sum of digits
12

Lastly, we will use control line arguments to calculate the sum of the digits of a number of a number.

Past using command line arguments

Lawmaking:

            class SumOfDigits { public static void chief(String arg[]) { long n,sum=0; Organisation.out.println("Enter a number "); due north=Long.parseLong(arg[0]); while(n!=0) { sum+=n%x; n/=ten; } System.out.println("Sum of digits “ +sum);   } }        

Output:
Enter a number
123
Sum of digits
6

With this, we take reached towards the end of this tutorial on "Sum of Digits in Java ". I hope Information technology helped you in some way. Keep reading, keep exploring! We volition keep digging the Java world together. Stay tuned!

Brand certain you lot practice as much as possible and revert your experience.

This brings us to the cease of our blog on "Java Programme to find Sum of Digits in Java".  I hope yous found this blog informative and added value to your noesis.
Check out theJava Certification Training by Edureka, a trusted online learning company with a network of more than than 250,000 satisfied learners spread across the globe. Edureka's Java J2EE and SOA training and certification form is designed for students and professionals who want to be a Coffee Developer. The course is designed to give yous a head beginning into Java programming and train y'all for both core and advanced Java concepts along with various Coffee frameworks like Hibernate & Leap.

Got a question for us? Delight mention information technology in the comments section of this "Sum of Digits in Coffee" commodity and we will get dorsum to you equally soon as possible.

bradleyintoommand.blogspot.com

Source: https://www.edureka.co/blog/java-program-for-sum-of-digits/

0 Response to "Write a Program to Read an Integer and Sum of Digits in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel