{"id":2163,"date":"2018-05-27T09:11:00","date_gmt":"2018-05-27T09:11:00","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2163"},"modified":"2024-12-14T05:17:47","modified_gmt":"2024-12-14T05:17:47","slug":"sum-of-digits-java-program","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/sum-of-digits-java-program\/","title":{"rendered":"Sum of Digits Java Program"},"content":{"rendered":"<h1>Sum of Digits Java Program<\/h1>\r\n<p>In this post, we will write a java program to calculate the sum of digits of a number. We will take input from the user with a prompt to enter the number.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2171\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Modulus-Operator-in-Java.png\" alt=\"Modulus Operator\" width=\"1711\" height=\"889\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Modulus-Operator-in-Java.png 1711w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Modulus-Operator-in-Java-300x156.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Modulus-Operator-in-Java-1024x532.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Modulus-Operator-in-Java-768x399.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Modulus-Operator-in-Java-1536x798.png 1536w\" sizes=\"auto, (max-width: 1711px) 100vw, 1711px\" \/><\/p>\r\n<h2>Eclipse IDE Setup<\/h2>\r\n<p>Some steps and instructions to create a Java application on Eclipse IDE:<\/p>\r\n<ul>\r\n<li>Create Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Package in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/\">https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Class in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/\">https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/<\/a><\/p>\r\n<ul>\r\n<li>Run Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/<\/a><\/p>\r\n<h2>Java Program<\/h2>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Scanner;\r\n\/**************************************************\r\n * DigitSum.java\r\n * @program   \t: DigitSum to calculate the sum of \r\n *                digits in the number.\r\n * @web        \t: www.TestingDocs.com\r\n * @version     : 1.0\r\n **************************************************\/\r\npublic class DigitSum {\r\n  private int number;\r\n  private int sum;\r\n\r\n  \/**\r\n   * @return the number\r\n   *\/\r\n  public int getNumber() {\r\n    return number;\r\n  }\r\n\r\n  \/**\r\n   * @param number the number to set\r\n   *\/\r\n  public void setNumber(int number) {\r\n    this.number = number;\r\n  }\r\n\r\n  \/**\r\n   * @return the sum\r\n   *\/\r\n  public int getSum() {\r\n    return sum;\r\n  }\r\n\r\n  \/**\r\n   * @param sum the sum to set\r\n   *\/\r\n  public void setSum(int sum) {\r\n    this.sum = sum;\r\n  }\r\n\r\n  \/**\r\n   * @param number\r\n   * @param sum\r\n   *\/\r\n  public DigitSum(int number) {\r\n    this.number=number;\r\n    this.sum = 0;\r\n  }\r\n\r\n  private void calculateSum() {\r\n    int remainder = number;\r\n    int digit = 0;\r\n    while(remainder &gt; 0) {\r\n      digit= remainder%10;\r\n      remainder= remainder\/10;\r\n      \r\n      this.sum= this.sum + digit;\r\n    }\r\n  }\r\n\r\n  public static void main(String args[]) {\r\n    int number=0;\r\n    Scanner keyboard = new Scanner(System.in);\r\n    System.out.println(\"Enter number :=\");\r\n    number = keyboard.nextInt();\r\n\r\n    DigitSum ds= new DigitSum(number);\r\n    ds.calculateSum();\r\n    System.out.println(\"Sum of Digits := \" + ds.getSum());\r\n  }\r\n}\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<h2>Program Output<\/h2>\r\n<p>Enter number :=<br \/>1687<br \/>Sum of Digits := 22<\/p>\r\n<h2>Screenshot<\/h2>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2170\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-of-Digits-of-NUmber-Java-Program.png\" alt=\"Sum of Digits of Number\" width=\"1762\" height=\"1028\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-of-Digits-of-NUmber-Java-Program.png 1762w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-of-Digits-of-NUmber-Java-Program-300x175.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-of-Digits-of-NUmber-Java-Program-1024x597.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-of-Digits-of-NUmber-Java-Program-768x448.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-of-Digits-of-NUmber-Java-Program-1536x896.png 1536w\" sizes=\"auto, (max-width: 1762px) 100vw, 1762px\" \/><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n<h2>Java Tutorials<\/h2>\r\n<p>Java Tutorial on this website:<\/p>\r\n<ul>\r\n<li><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/li>\r\n<\/ul>\r\n<p>&nbsp;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Sum of Digits Java Program In this post, we will write a java program to calculate the sum of digits of a number. We will take input from the user with a prompt to enter the number. &nbsp; &nbsp; Eclipse IDE Setup Some steps and instructions to create a Java application on Eclipse IDE: Create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2163","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2163"}],"version-history":[{"count":13,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2163\/revisions"}],"predecessor-version":[{"id":26449,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2163\/revisions\/26449"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}