SlideShare a Scribd company logo
Write a Java program in which a method named digitSum that accepts an integer as a parameter
and returns the sum of the digits of that number. For example, the call digitSum(29107) returns
2+9+1+0+7 or 19. For negative numbers, return the same value that would result if the number
were positive. For example, digitSum(-456) returns 4+5+6 or 15.The call digitSum(0) returns 0
Solution
DigitSumTest.java
import java.util.Scanner;
public class DigitSumTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = scan.nextInt();
int sum = digitSum(n);
System.out.println("The sum of digits is "+sum);
}
public static int digitSum (int n) {
int sum = 0;
if(n == 0){
return 0;
}
else if(n < 0){
n = -n;
}
do {
sum += n % 10;
} while ((n = n / 10) != 0);
return sum;
}
}
Output:
Enter an integer: -456
The sum of digits is 15
Enter an integer: 29107
The sum of digits is 19

More Related Content

Similar to Write a Java program in which a method named digitSum that accepts a.pdf

Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
Neil Mathew
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programsAbhishek Jena
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
RohitkumarYadav80
 
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
premsrivastva8
 
import java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdfimport java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdf
apexelectronices01
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
Quratulain Naqvi
 
C programming
C programmingC programming
C programming
Samsil Arefin
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad27
 
Java programs
Java programsJava programs
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
aravlitraders2012
 

Similar to Write a Java program in which a method named digitSum that accepts a.pdf (11)

Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
 
import java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdfimport java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdf
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
C programming
C programmingC programming
C programming
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
Java programs
Java programsJava programs
Java programs
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
 

More from naveenkumar29100

Question Completion Status QUESTION In the absence of oxygen, animal.pdf
Question Completion Status QUESTION In the absence of oxygen, animal.pdfQuestion Completion Status QUESTION In the absence of oxygen, animal.pdf
Question Completion Status QUESTION In the absence of oxygen, animal.pdf
naveenkumar29100
 
Short Answer 1. What are the 5 mechanisms of evolutionary ch.pdf
Short Answer 1. What are the 5 mechanisms of evolutionary ch.pdfShort Answer 1. What are the 5 mechanisms of evolutionary ch.pdf
Short Answer 1. What are the 5 mechanisms of evolutionary ch.pdf
naveenkumar29100
 
Review Preparation for Statistics A statistics instructor wanted to s.pdf
Review Preparation for Statistics A statistics instructor wanted to s.pdfReview Preparation for Statistics A statistics instructor wanted to s.pdf
Review Preparation for Statistics A statistics instructor wanted to s.pdf
naveenkumar29100
 
Problem 3-8BRIDGEPORT ADVERTISING TRIAL BALANCE DECEMBER 31, 2.pdf
Problem 3-8BRIDGEPORT ADVERTISING TRIAL BALANCE DECEMBER 31, 2.pdfProblem 3-8BRIDGEPORT ADVERTISING TRIAL BALANCE DECEMBER 31, 2.pdf
Problem 3-8BRIDGEPORT ADVERTISING TRIAL BALANCE DECEMBER 31, 2.pdf
naveenkumar29100
 
Q1 Define the three types of data flow. Q2 What are the t.pdf
Q1 Define the three types of data flow. Q2 What are the t.pdfQ1 Define the three types of data flow. Q2 What are the t.pdf
Q1 Define the three types of data flow. Q2 What are the t.pdf
naveenkumar29100
 
Multiple choice A young girl is staring at the raindrops running do.pdf
Multiple choice A young girl is staring at the raindrops running do.pdfMultiple choice A young girl is staring at the raindrops running do.pdf
Multiple choice A young girl is staring at the raindrops running do.pdf
naveenkumar29100
 
mitosis is a special set of events that occur in what region of .pdf
mitosis is a special set of events that occur in what region of .pdfmitosis is a special set of events that occur in what region of .pdf
mitosis is a special set of events that occur in what region of .pdf
naveenkumar29100
 
If you were looking at an mRNA and saw the codon AUG, what would you .pdf
If you were looking at an mRNA and saw the codon AUG, what would you .pdfIf you were looking at an mRNA and saw the codon AUG, what would you .pdf
If you were looking at an mRNA and saw the codon AUG, what would you .pdf
naveenkumar29100
 
If the proportion of an African population that is susceptible to Ma.pdf
If the proportion of an African population that is susceptible to Ma.pdfIf the proportion of an African population that is susceptible to Ma.pdf
If the proportion of an African population that is susceptible to Ma.pdf
naveenkumar29100
 
Identify nursing diagnoses goal and outcome criteria and interven.pdf
Identify nursing diagnoses goal and outcome criteria and interven.pdfIdentify nursing diagnoses goal and outcome criteria and interven.pdf
Identify nursing diagnoses goal and outcome criteria and interven.pdf
naveenkumar29100
 
How would you arrange these concepts in terms of concreteness (from .pdf
How would you arrange these concepts in terms of concreteness (from .pdfHow would you arrange these concepts in terms of concreteness (from .pdf
How would you arrange these concepts in terms of concreteness (from .pdf
naveenkumar29100
 
For 12 pages randomly selected from War and Peace by Leo Tolstoy, th.pdf
For 12 pages randomly selected from War and Peace by Leo Tolstoy, th.pdfFor 12 pages randomly selected from War and Peace by Leo Tolstoy, th.pdf
For 12 pages randomly selected from War and Peace by Leo Tolstoy, th.pdf
naveenkumar29100
 
Explain what an enumerated type isSolutionEnum is also called.pdf
Explain what an enumerated type isSolutionEnum is also called.pdfExplain what an enumerated type isSolutionEnum is also called.pdf
Explain what an enumerated type isSolutionEnum is also called.pdf
naveenkumar29100
 
Explain how the three germ layers participate in the formation of ext.pdf
Explain how the three germ layers participate in the formation of ext.pdfExplain how the three germ layers participate in the formation of ext.pdf
Explain how the three germ layers participate in the formation of ext.pdf
naveenkumar29100
 
Ethical issue What would you doTony, a data analyst for a major c.pdf
Ethical issue What would you doTony, a data analyst for a major c.pdfEthical issue What would you doTony, a data analyst for a major c.pdf
Ethical issue What would you doTony, a data analyst for a major c.pdf
naveenkumar29100
 
Compare and contrast the structure of a nephron with that of an alve.pdf
Compare and contrast the structure of a nephron with that of an alve.pdfCompare and contrast the structure of a nephron with that of an alve.pdf
Compare and contrast the structure of a nephron with that of an alve.pdf
naveenkumar29100
 
Classless addressing was introduced to extend the life of the IPv ad.pdf
Classless addressing was introduced to extend the life of the IPv ad.pdfClassless addressing was introduced to extend the life of the IPv ad.pdf
Classless addressing was introduced to extend the life of the IPv ad.pdf
naveenkumar29100
 
C++ Complexity analysis Represent the time complexity of the follow.pdf
C++ Complexity analysis Represent the time complexity of the follow.pdfC++ Complexity analysis Represent the time complexity of the follow.pdf
C++ Complexity analysis Represent the time complexity of the follow.pdf
naveenkumar29100
 
Can someone provide a solution for this assignmentPurpose of This.pdf
Can someone provide a solution for this assignmentPurpose of This.pdfCan someone provide a solution for this assignmentPurpose of This.pdf
Can someone provide a solution for this assignmentPurpose of This.pdf
naveenkumar29100
 
Before first law of thermodynamics, there is zeroth law of thermodyn.pdf
Before first law of thermodynamics, there is zeroth law of thermodyn.pdfBefore first law of thermodynamics, there is zeroth law of thermodyn.pdf
Before first law of thermodynamics, there is zeroth law of thermodyn.pdf
naveenkumar29100
 

More from naveenkumar29100 (20)

Question Completion Status QUESTION In the absence of oxygen, animal.pdf
Question Completion Status QUESTION In the absence of oxygen, animal.pdfQuestion Completion Status QUESTION In the absence of oxygen, animal.pdf
Question Completion Status QUESTION In the absence of oxygen, animal.pdf
 
Short Answer 1. What are the 5 mechanisms of evolutionary ch.pdf
Short Answer 1. What are the 5 mechanisms of evolutionary ch.pdfShort Answer 1. What are the 5 mechanisms of evolutionary ch.pdf
Short Answer 1. What are the 5 mechanisms of evolutionary ch.pdf
 
Review Preparation for Statistics A statistics instructor wanted to s.pdf
Review Preparation for Statistics A statistics instructor wanted to s.pdfReview Preparation for Statistics A statistics instructor wanted to s.pdf
Review Preparation for Statistics A statistics instructor wanted to s.pdf
 
Problem 3-8BRIDGEPORT ADVERTISING TRIAL BALANCE DECEMBER 31, 2.pdf
Problem 3-8BRIDGEPORT ADVERTISING TRIAL BALANCE DECEMBER 31, 2.pdfProblem 3-8BRIDGEPORT ADVERTISING TRIAL BALANCE DECEMBER 31, 2.pdf
Problem 3-8BRIDGEPORT ADVERTISING TRIAL BALANCE DECEMBER 31, 2.pdf
 
Q1 Define the three types of data flow. Q2 What are the t.pdf
Q1 Define the three types of data flow. Q2 What are the t.pdfQ1 Define the three types of data flow. Q2 What are the t.pdf
Q1 Define the three types of data flow. Q2 What are the t.pdf
 
Multiple choice A young girl is staring at the raindrops running do.pdf
Multiple choice A young girl is staring at the raindrops running do.pdfMultiple choice A young girl is staring at the raindrops running do.pdf
Multiple choice A young girl is staring at the raindrops running do.pdf
 
mitosis is a special set of events that occur in what region of .pdf
mitosis is a special set of events that occur in what region of .pdfmitosis is a special set of events that occur in what region of .pdf
mitosis is a special set of events that occur in what region of .pdf
 
If you were looking at an mRNA and saw the codon AUG, what would you .pdf
If you were looking at an mRNA and saw the codon AUG, what would you .pdfIf you were looking at an mRNA and saw the codon AUG, what would you .pdf
If you were looking at an mRNA and saw the codon AUG, what would you .pdf
 
If the proportion of an African population that is susceptible to Ma.pdf
If the proportion of an African population that is susceptible to Ma.pdfIf the proportion of an African population that is susceptible to Ma.pdf
If the proportion of an African population that is susceptible to Ma.pdf
 
Identify nursing diagnoses goal and outcome criteria and interven.pdf
Identify nursing diagnoses goal and outcome criteria and interven.pdfIdentify nursing diagnoses goal and outcome criteria and interven.pdf
Identify nursing diagnoses goal and outcome criteria and interven.pdf
 
How would you arrange these concepts in terms of concreteness (from .pdf
How would you arrange these concepts in terms of concreteness (from .pdfHow would you arrange these concepts in terms of concreteness (from .pdf
How would you arrange these concepts in terms of concreteness (from .pdf
 
For 12 pages randomly selected from War and Peace by Leo Tolstoy, th.pdf
For 12 pages randomly selected from War and Peace by Leo Tolstoy, th.pdfFor 12 pages randomly selected from War and Peace by Leo Tolstoy, th.pdf
For 12 pages randomly selected from War and Peace by Leo Tolstoy, th.pdf
 
Explain what an enumerated type isSolutionEnum is also called.pdf
Explain what an enumerated type isSolutionEnum is also called.pdfExplain what an enumerated type isSolutionEnum is also called.pdf
Explain what an enumerated type isSolutionEnum is also called.pdf
 
Explain how the three germ layers participate in the formation of ext.pdf
Explain how the three germ layers participate in the formation of ext.pdfExplain how the three germ layers participate in the formation of ext.pdf
Explain how the three germ layers participate in the formation of ext.pdf
 
Ethical issue What would you doTony, a data analyst for a major c.pdf
Ethical issue What would you doTony, a data analyst for a major c.pdfEthical issue What would you doTony, a data analyst for a major c.pdf
Ethical issue What would you doTony, a data analyst for a major c.pdf
 
Compare and contrast the structure of a nephron with that of an alve.pdf
Compare and contrast the structure of a nephron with that of an alve.pdfCompare and contrast the structure of a nephron with that of an alve.pdf
Compare and contrast the structure of a nephron with that of an alve.pdf
 
Classless addressing was introduced to extend the life of the IPv ad.pdf
Classless addressing was introduced to extend the life of the IPv ad.pdfClassless addressing was introduced to extend the life of the IPv ad.pdf
Classless addressing was introduced to extend the life of the IPv ad.pdf
 
C++ Complexity analysis Represent the time complexity of the follow.pdf
C++ Complexity analysis Represent the time complexity of the follow.pdfC++ Complexity analysis Represent the time complexity of the follow.pdf
C++ Complexity analysis Represent the time complexity of the follow.pdf
 
Can someone provide a solution for this assignmentPurpose of This.pdf
Can someone provide a solution for this assignmentPurpose of This.pdfCan someone provide a solution for this assignmentPurpose of This.pdf
Can someone provide a solution for this assignmentPurpose of This.pdf
 
Before first law of thermodynamics, there is zeroth law of thermodyn.pdf
Before first law of thermodynamics, there is zeroth law of thermodyn.pdfBefore first law of thermodynamics, there is zeroth law of thermodyn.pdf
Before first law of thermodynamics, there is zeroth law of thermodyn.pdf
 

Recently uploaded

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 

Recently uploaded (20)

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 

Write a Java program in which a method named digitSum that accepts a.pdf

  • 1. Write a Java program in which a method named digitSum that accepts an integer as a parameter and returns the sum of the digits of that number. For example, the call digitSum(29107) returns 2+9+1+0+7 or 19. For negative numbers, return the same value that would result if the number were positive. For example, digitSum(-456) returns 4+5+6 or 15.The call digitSum(0) returns 0 Solution DigitSumTest.java import java.util.Scanner; public class DigitSumTest { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter an integer: "); int n = scan.nextInt(); int sum = digitSum(n); System.out.println("The sum of digits is "+sum); } public static int digitSum (int n) { int sum = 0; if(n == 0){ return 0; } else if(n < 0){ n = -n; } do { sum += n % 10; } while ((n = n / 10) != 0); return sum; } }
  • 2. Output: Enter an integer: -456 The sum of digits is 15 Enter an integer: 29107 The sum of digits is 19