• Slider Banner

    The Programming World

    Learn about top programming languages, tools and frameworks.

    Read more
  • Slider Banner

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read more
  • Slider Banner

    Prepare for Technical Interview

    Over 1 Million+ interview questions for different technical profiles.

    Read more

Java program to find sum of digits of a given number.

Below program will return sum of digits of a given number

package testjava.controlflow;

import java.util.Scanner;

public class SumOfDigitsOfNumber
{
	private static Scanner sc;
	
	public static void main(String[] args)
	{
		int Num, Rem, Sum = 0;
		sc = new Scanner(System.in);
		
		System.out.println("Please Enter any Number: ");
		Num = sc.nextInt();
		
		while(Num > 0)
		{
			Rem = Num % 10;
			Sum = Sum+ Rem;
			Num = Num / 10;
		}
		System.out.format("Sum of the digits of Given Number = %d", Sum);
	}
}

Output

What is final keyword in Java?

final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:

final variable: When the final keyword is used with a variable then its value can't be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

final method
: When a method is declared final then it can't be overridden by the inheriting class.

Subscribe to