Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
November 11, 2019

How to Perform Sum of the Digits in a Given Number in Java





In this blog, we will learn about how to sum of the digits in a given number in Java. This will be asked in the most of the interviews.This can be implemented using the below way:




In the below snippet, we have done the following:

  1. Created the below variables of int type
    • Number (assigned it with some value)
    • Sum (assigned it with some value)
    • Reminder
  2. By using while loop until the number value is greater than 0. we have done the below:
    • Performed modular division of the number and stored the result in the reminder
    • Stored the reminder value in the Sum
    • Performed Division operation of the number and stored the result in the Number
public class SumOfDigitsInANumber {

	public static void main(String[] args) {
	
		int number=2587, reminder, sum = 0;
		
		while(number>0)
		{
			reminder = number % 10;
			sum = sum + reminder;
			number = number / 10;
		}
		System.out.println("The sum of digits in a given number is:" +sum);
	}

}
Output:
-------
The sum of digits in a given number is:22




Please watch the Youtube video for better understanding.




Share this post: on Twitter on Facebook on Google+

How to Reverse a String in Java How to Remove Whitespaces from a String in Java

Related Posts

How To SWAP Two Numbers in Java Without using Temp Variable

Java IQs

How to SWAP Two Numbers in Java Without Temp variable or Without Third variable

How to Swap Two Numbers in Java with Temp Variable

Java IQs

How to SWAP Two Numbers in Java using Temp Variable

How to Read Properties file in Java

Java IQs, Uncategorized

How to Read Data From Properties File in Java

Compare two arrays in java with out inbuilt functions

Java IQs

How to Compare Two Arrays in Java without built-in functions

Compare two arrays in java

Java IQs

How to Compare Two Arrays in Java using built-in functions

Remove White Spaces From A String in Java

Java IQs

How to Remove Whitespaces from a String in Java

How To Reverse A String in Java

Java IQs

How to Reverse a String in Java

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023