SlideShare a Scribd company logo
1 of 2
Download to read offline
Program:
import java.util.Scanner;
public class SumOfDigits // Here we are taking class name as SumOfDigits
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); // scanning the input from the user
System.out.println("Enter number to find it's sum of Digits : ");
long n = input.nextLong();
int sum = sumDigits(n); // calling sumDigits(long n) method
System.out.println("The sum of Digits of a number is: " + sum);// printing value of sum
}
public static int sumDigits(long n)
{
int num = (int) (n);
int sum = 0; // initializing value of sum to zero
while (num > 0)
{
sum += num % 10; // Partition the given number into individual numbers
num = num / 10; // divides until all digits in a number has been covered
} //End of while loop
return sum;
}// End of sumDigits Method
}
Output:
Enter number to find it's sum of Digits :
456
The sum of Digits of a number is: 15
Solution
Program:
import java.util.Scanner;
public class SumOfDigits // Here we are taking class name as SumOfDigits
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); // scanning the input from the user
System.out.println("Enter number to find it's sum of Digits : ");
long n = input.nextLong();
int sum = sumDigits(n); // calling sumDigits(long n) method
System.out.println("The sum of Digits of a number is: " + sum);// printing value of sum
}
public static int sumDigits(long n)
{
int num = (int) (n);
int sum = 0; // initializing value of sum to zero
while (num > 0)
{
sum += num % 10; // Partition the given number into individual numbers
num = num / 10; // divides until all digits in a number has been covered
} //End of while loop
return sum;
}// End of sumDigits Method
}
Output:
Enter number to find it's sum of Digits :
456
The sum of Digits of a number is: 15

More Related Content

Similar to Programimport java.util.Scanner; public class SumOfDigits He.pdf

import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
Katecate1
 
I need to create a data type that implement the following interface -.docx
I need to create a data type that implement the following interface -.docxI need to create a data type that implement the following interface -.docx
I need to create a data type that implement the following interface -.docx
mckerliejonelle
 
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfCountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
aparnatiwari291
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
andreaplotner1
 
Create a class named Student that has the following member variables.pdf
Create a class named Student that has the following member variables.pdfCreate a class named Student that has the following member variables.pdf
Create a class named Student that has the following member variables.pdf
arrowvisionoptics
 
The java program that prompts user to enter a string and .pdf
  The java program that prompts user to  enter a string and .pdf  The java program that prompts user to  enter a string and .pdf
The java program that prompts user to enter a string and .pdf
DEEPAKSONI562
 
My code is below How do I write this code without usin.pdf
My code is below How do I write this code without usin.pdfMy code is below How do I write this code without usin.pdf
My code is below How do I write this code without usin.pdf
adianantsolutions
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
actocomputer
 
ConvertingSeconds.javaimport java.util.Scanner;public class Conv.pdf
ConvertingSeconds.javaimport java.util.Scanner;public class Conv.pdfConvertingSeconds.javaimport java.util.Scanner;public class Conv.pdf
ConvertingSeconds.javaimport java.util.Scanner;public class Conv.pdf
angelfashions02
 

Similar to Programimport java.util.Scanner; public class SumOfDigits He.pdf (18)

SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
 
PROGRAMMING QUESTIONS.docx
PROGRAMMING QUESTIONS.docxPROGRAMMING QUESTIONS.docx
PROGRAMMING QUESTIONS.docx
 
JAVA Question : Programming Assignment
JAVA Question : Programming AssignmentJAVA Question : Programming Assignment
JAVA Question : Programming Assignment
 
Prompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfPrompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdf
 
I need to create a data type that implement the following interface -.docx
I need to create a data type that implement the following interface -.docxI need to create a data type that implement the following interface -.docx
I need to create a data type that implement the following interface -.docx
 
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfCountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
Creating Interface- Practice Program 6.docx
Creating Interface- Practice Program 6.docxCreating Interface- Practice Program 6.docx
Creating Interface- Practice Program 6.docx
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
 
Create a class named Student that has the following member variables.pdf
Create a class named Student that has the following member variables.pdfCreate a class named Student that has the following member variables.pdf
Create a class named Student that has the following member variables.pdf
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
 
The java program that prompts user to enter a string and .pdf
  The java program that prompts user to  enter a string and .pdf  The java program that prompts user to  enter a string and .pdf
The java program that prompts user to enter a string and .pdf
 
My code is below How do I write this code without usin.pdf
My code is below How do I write this code without usin.pdfMy code is below How do I write this code without usin.pdf
My code is below How do I write this code without usin.pdf
 
Computer programming 2 chapter 1-lesson 2
Computer programming 2  chapter 1-lesson 2Computer programming 2  chapter 1-lesson 2
Computer programming 2 chapter 1-lesson 2
 
Write a static method named numberoddSum that accepts a positive inte.pdf
Write a static method named numberoddSum that accepts a positive inte.pdfWrite a static method named numberoddSum that accepts a positive inte.pdf
Write a static method named numberoddSum that accepts a positive inte.pdf
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
 
ConvertingSeconds.javaimport java.util.Scanner;public class Conv.pdf
ConvertingSeconds.javaimport java.util.Scanner;public class Conv.pdfConvertingSeconds.javaimport java.util.Scanner;public class Conv.pdf
ConvertingSeconds.javaimport java.util.Scanner;public class Conv.pdf
 

More from anushasarees

Properties of enantiomers Their NMR and IR spec.pdf
                     Properties of enantiomers Their NMR and IR spec.pdf                     Properties of enantiomers Their NMR and IR spec.pdf
Properties of enantiomers Their NMR and IR spec.pdf
anushasarees
 
  import java.util.;import acm.program.;public class FlightPla.pdf
  import java.util.;import acm.program.;public class FlightPla.pdf  import java.util.;import acm.program.;public class FlightPla.pdf
  import java.util.;import acm.program.;public class FlightPla.pdf
anushasarees
 
There are so many java Input Output classes that are available in it.pdf
There are so many java Input Output classes that are available in it.pdfThere are so many java Input Output classes that are available in it.pdf
There are so many java Input Output classes that are available in it.pdf
anushasarees
 
The main organelles in protein sorting and targeting are Rough endop.pdf
The main organelles in protein sorting and targeting are Rough endop.pdfThe main organelles in protein sorting and targeting are Rough endop.pdf
The main organelles in protein sorting and targeting are Rough endop.pdf
anushasarees
 
Successfully supporting managerial decision-making is critically dep.pdf
Successfully supporting managerial decision-making is critically dep.pdfSuccessfully supporting managerial decision-making is critically dep.pdf
Successfully supporting managerial decision-making is critically dep.pdf
anushasarees
 
SolutionTo know that the team has identified all of the significa.pdf
SolutionTo know that the team has identified all of the significa.pdfSolutionTo know that the team has identified all of the significa.pdf
SolutionTo know that the team has identified all of the significa.pdf
anushasarees
 

More from anushasarees (20)

Properties of enantiomers Their NMR and IR spec.pdf
                     Properties of enantiomers Their NMR and IR spec.pdf                     Properties of enantiomers Their NMR and IR spec.pdf
Properties of enantiomers Their NMR and IR spec.pdf
 
O2 will be released as Na+ will not get reduce bu.pdf
                     O2 will be released as Na+ will not get reduce bu.pdf                     O2 will be released as Na+ will not get reduce bu.pdf
O2 will be released as Na+ will not get reduce bu.pdf
 
Huntingtons disease and other hereditary diseas.pdf
                     Huntingtons disease and other hereditary diseas.pdf                     Huntingtons disease and other hereditary diseas.pdf
Huntingtons disease and other hereditary diseas.pdf
 
ionic character BaF MgO FeO SO2 N2 .pdf
                     ionic character BaF  MgO  FeO  SO2  N2  .pdf                     ionic character BaF  MgO  FeO  SO2  N2  .pdf
ionic character BaF MgO FeO SO2 N2 .pdf
 
Nitrogen can hold up to 4 bonds. In sodium amide.pdf
                     Nitrogen can hold up to 4 bonds.  In sodium amide.pdf                     Nitrogen can hold up to 4 bonds.  In sodium amide.pdf
Nitrogen can hold up to 4 bonds. In sodium amide.pdf
 
C. hydrogen bonding. between N and H of differen.pdf
                     C. hydrogen bonding.  between N and H of differen.pdf                     C. hydrogen bonding.  between N and H of differen.pdf
C. hydrogen bonding. between N and H of differen.pdf
 
  import java.util.;import acm.program.;public class FlightPla.pdf
  import java.util.;import acm.program.;public class FlightPla.pdf  import java.util.;import acm.program.;public class FlightPla.pdf
  import java.util.;import acm.program.;public class FlightPla.pdf
 
We Know that    Amines are generally basic in naturebecause of th.pdf
We Know that    Amines are generally basic in naturebecause of th.pdfWe Know that    Amines are generally basic in naturebecause of th.pdf
We Know that    Amines are generally basic in naturebecause of th.pdf
 
There are so many java Input Output classes that are available in it.pdf
There are so many java Input Output classes that are available in it.pdfThere are so many java Input Output classes that are available in it.pdf
There are so many java Input Output classes that are available in it.pdf
 
Three are ways to protect unused switch ports Option B,D and E is.pdf
Three are ways to protect unused switch ports Option B,D and E is.pdfThree are ways to protect unused switch ports Option B,D and E is.pdf
Three are ways to protect unused switch ports Option B,D and E is.pdf
 
The water turns green because the copper(II)sulfate is breaking apar.pdf
The water turns green because the copper(II)sulfate is breaking apar.pdfThe water turns green because the copper(II)sulfate is breaking apar.pdf
The water turns green because the copper(II)sulfate is breaking apar.pdf
 
The mutation is known as inversion. In this a segment from one chrom.pdf
The mutation is known as inversion. In this a segment from one chrom.pdfThe mutation is known as inversion. In this a segment from one chrom.pdf
The mutation is known as inversion. In this a segment from one chrom.pdf
 
The main organelles in protein sorting and targeting are Rough endop.pdf
The main organelles in protein sorting and targeting are Rough endop.pdfThe main organelles in protein sorting and targeting are Rough endop.pdf
The main organelles in protein sorting and targeting are Rough endop.pdf
 
Successfully supporting managerial decision-making is critically dep.pdf
Successfully supporting managerial decision-making is critically dep.pdfSuccessfully supporting managerial decision-making is critically dep.pdf
Successfully supporting managerial decision-making is critically dep.pdf
 
SolutionTo know that the team has identified all of the significa.pdf
SolutionTo know that the team has identified all of the significa.pdfSolutionTo know that the team has identified all of the significa.pdf
SolutionTo know that the team has identified all of the significa.pdf
 
Solutiona) Maximum bus speed = bus driver delay + propagation del.pdf
Solutiona) Maximum bus speed = bus driver delay + propagation del.pdfSolutiona) Maximum bus speed = bus driver delay + propagation del.pdf
Solutiona) Maximum bus speed = bus driver delay + propagation del.pdf
 
Solution Polymerase chain reaction is process in which several co.pdf
Solution Polymerase chain reaction is process in which several co.pdfSolution Polymerase chain reaction is process in which several co.pdf
Solution Polymerase chain reaction is process in which several co.pdf
 
Doubling [NO] would quadruple the rate .pdf
                     Doubling [NO] would quadruple the rate           .pdf                     Doubling [NO] would quadruple the rate           .pdf
Doubling [NO] would quadruple the rate .pdf
 
Correct answer F)4.0 .pdf
                     Correct answer F)4.0                            .pdf                     Correct answer F)4.0                            .pdf
Correct answer F)4.0 .pdf
 
D.) The system is neither at steady state or equi.pdf
                     D.) The system is neither at steady state or equi.pdf                     D.) The system is neither at steady state or equi.pdf
D.) The system is neither at steady state or equi.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

Programimport java.util.Scanner; public class SumOfDigits He.pdf

  • 1. Program: import java.util.Scanner; public class SumOfDigits // Here we are taking class name as SumOfDigits { public static void main(String[] args) { Scanner input = new Scanner(System.in); // scanning the input from the user System.out.println("Enter number to find it's sum of Digits : "); long n = input.nextLong(); int sum = sumDigits(n); // calling sumDigits(long n) method System.out.println("The sum of Digits of a number is: " + sum);// printing value of sum } public static int sumDigits(long n) { int num = (int) (n); int sum = 0; // initializing value of sum to zero while (num > 0) { sum += num % 10; // Partition the given number into individual numbers num = num / 10; // divides until all digits in a number has been covered } //End of while loop return sum; }// End of sumDigits Method } Output: Enter number to find it's sum of Digits : 456 The sum of Digits of a number is: 15 Solution Program: import java.util.Scanner; public class SumOfDigits // Here we are taking class name as SumOfDigits {
  • 2. public static void main(String[] args) { Scanner input = new Scanner(System.in); // scanning the input from the user System.out.println("Enter number to find it's sum of Digits : "); long n = input.nextLong(); int sum = sumDigits(n); // calling sumDigits(long n) method System.out.println("The sum of Digits of a number is: " + sum);// printing value of sum } public static int sumDigits(long n) { int num = (int) (n); int sum = 0; // initializing value of sum to zero while (num > 0) { sum += num % 10; // Partition the given number into individual numbers num = num / 10; // divides until all digits in a number has been covered } //End of while loop return sum; }// End of sumDigits Method } Output: Enter number to find it's sum of Digits : 456 The sum of Digits of a number is: 15