SlideShare une entreprise Scribd logo
1  sur  2
1. The following given statements:
import java.io.*;
import java.util.*;
2. The following given main() method:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer value: ");
long number = input.nextLong();
System.out.println("The number of odd digits in the number is " + countOdd(number));
}
3. A recursive method countOdd that will compute the number of odd digits in a number. The
method must handle inputs of both positive and negative numbers.
Solution
import java.util.Scanner;
public class Recursive {
public static int countOdd( long num){
int cnt = 0;
if (num == 0)
return 0;
long rem = num % 10;
cnt = countOdd (num/10);
if ((rem % 2) != 0)
cnt += 1;
return cnt;
}
public static void main(String[] args){
Scanner input = new Scanner(System. in );
System. out .println("Enter an integer value: ");
long number = input.nextLong();
System. out .println("The number of odd digits in the number is " + countOdd (number));
}
}

Contenu connexe

Similaire à 1- The following given statements- import java-io--- import java-util.docx

import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
optokunal1
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
angelsfashion1
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
Aiman Hud
 
import java.util.; public class DecimalToBinary { public stat.pdf
import java.util.; public class DecimalToBinary { public stat.pdfimport java.util.; public class DecimalToBinary { public stat.pdf
import java.util.; public class DecimalToBinary { public stat.pdf
anithareadymade
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
Katecate1
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
MaruMengesha
 

Similaire à 1- The following given statements- import java-io--- import java-util.docx (15)

Codeimport java.util.Random; import java.util.Scanner;public .pdf
Codeimport java.util.Random; import java.util.Scanner;public .pdfCodeimport java.util.Random; import java.util.Scanner;public .pdf
Codeimport java.util.Random; import java.util.Scanner;public .pdf
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
 
LAB1.docx
LAB1.docxLAB1.docx
LAB1.docx
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 
import java.util.; public class DecimalToBinary { public stat.pdf
import java.util.; public class DecimalToBinary { public stat.pdfimport java.util.; public class DecimalToBinary { public stat.pdf
import java.util.; public class DecimalToBinary { public stat.pdf
 
Java programs
Java programsJava programs
Java programs
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
 
An input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docxAn input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docx
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 

Plus de tjames442

1- What kind of contract did Lulene and Ellen have- What kinds of issu.docx
1- What kind of contract did Lulene and Ellen have- What kinds of issu.docx1- What kind of contract did Lulene and Ellen have- What kinds of issu.docx
1- What kind of contract did Lulene and Ellen have- What kinds of issu.docx
tjames442
 
1- What is a database- Give some examples- 2- What is a database manag.docx
1- What is a database- Give some examples- 2- What is a database manag.docx1- What is a database- Give some examples- 2- What is a database manag.docx
1- What is a database- Give some examples- 2- What is a database manag.docx
tjames442
 
1- What are phonons- What information can be obtained by studying the.docx
1- What are phonons- What information can be obtained by studying the.docx1- What are phonons- What information can be obtained by studying the.docx
1- What are phonons- What information can be obtained by studying the.docx
tjames442
 
1- Single convex lenses can be used to make images of far away objects.docx
1- Single convex lenses can be used to make images of far away objects.docx1- Single convex lenses can be used to make images of far away objects.docx
1- Single convex lenses can be used to make images of far away objects.docx
tjames442
 

Plus de tjames442 (20)

A star is 17-2 ly (light-years) from Earth- HINT (a) At what constant.docx
A star is 17-2 ly (light-years) from Earth- HINT (a) At what constant.docxA star is 17-2 ly (light-years) from Earth- HINT (a) At what constant.docx
A star is 17-2 ly (light-years) from Earth- HINT (a) At what constant.docx
 
A true statement about atoms is that they can emit radiation at freque.docx
A true statement about atoms is that they can emit radiation at freque.docxA true statement about atoms is that they can emit radiation at freque.docx
A true statement about atoms is that they can emit radiation at freque.docx
 
A TDR price of a length of 60 centimeters was installed vertically int.docx
A TDR price of a length of 60 centimeters was installed vertically int.docxA TDR price of a length of 60 centimeters was installed vertically int.docx
A TDR price of a length of 60 centimeters was installed vertically int.docx
 
1- The real interest rate is defined as- A- inflation minus the nomina.docx
1- The real interest rate is defined as- A- inflation minus the nomina.docx1- The real interest rate is defined as- A- inflation minus the nomina.docx
1- The real interest rate is defined as- A- inflation minus the nomina.docx
 
1- The intNumbers array is a one dimensional array- Write the statemen.docx
1- The intNumbers array is a one dimensional array- Write the statemen.docx1- The intNumbers array is a one dimensional array- Write the statemen.docx
1- The intNumbers array is a one dimensional array- Write the statemen.docx
 
1- The following data were reported by a corporation- Authorized share.docx
1- The following data were reported by a corporation- Authorized share.docx1- The following data were reported by a corporation- Authorized share.docx
1- The following data were reported by a corporation- Authorized share.docx
 
1- The following change is physical or chemical- Explain why- Water bo.docx
1- The following change is physical or chemical- Explain why- Water bo.docx1- The following change is physical or chemical- Explain why- Water bo.docx
1- The following change is physical or chemical- Explain why- Water bo.docx
 
1- Which of the following is a situation in which an involuntary trans.docx
1- Which of the following is a situation in which an involuntary trans.docx1- Which of the following is a situation in which an involuntary trans.docx
1- Which of the following is a situation in which an involuntary trans.docx
 
1- Which of the following is NOT a function of a firewall- a- Blocks o.docx
1- Which of the following is NOT a function of a firewall- a- Blocks o.docx1- Which of the following is NOT a function of a firewall- a- Blocks o.docx
1- Which of the following is NOT a function of a firewall- a- Blocks o.docx
 
1- What soil characteristics influence compressibility 2- Why is conso.docx
1- What soil characteristics influence compressibility 2- Why is conso.docx1- What soil characteristics influence compressibility 2- Why is conso.docx
1- What soil characteristics influence compressibility 2- Why is conso.docx
 
1- What type of encryption does PKI use- Symmetric Asymmetric DES AES.docx
1- What type of encryption does PKI use- Symmetric Asymmetric DES AES.docx1- What type of encryption does PKI use- Symmetric Asymmetric DES AES.docx
1- What type of encryption does PKI use- Symmetric Asymmetric DES AES.docx
 
1- What kind of contract did Lulene and Ellen have- What kinds of issu.docx
1- What kind of contract did Lulene and Ellen have- What kinds of issu.docx1- What kind of contract did Lulene and Ellen have- What kinds of issu.docx
1- What kind of contract did Lulene and Ellen have- What kinds of issu.docx
 
1- what is the net ionic equation for the neutralization of HBr with K.docx
1- what is the net ionic equation for the neutralization of HBr with K.docx1- what is the net ionic equation for the neutralization of HBr with K.docx
1- what is the net ionic equation for the neutralization of HBr with K.docx
 
1- What is outsourcing- What are advantages and disadvantages of outso.docx
1- What is outsourcing- What are advantages and disadvantages of outso.docx1- What is outsourcing- What are advantages and disadvantages of outso.docx
1- What is outsourcing- What are advantages and disadvantages of outso.docx
 
1- What is the difference between OSI and TCP-IP layered models-Soluti.docx
1- What is the difference between OSI and TCP-IP layered models-Soluti.docx1- What is the difference between OSI and TCP-IP layered models-Soluti.docx
1- What is the difference between OSI and TCP-IP layered models-Soluti.docx
 
1- What is a database- Give some examples- 2- What is a database manag.docx
1- What is a database- Give some examples- 2- What is a database manag.docx1- What is a database- Give some examples- 2- What is a database manag.docx
1- What is a database- Give some examples- 2- What is a database manag.docx
 
1- What are the functional sources of power- 2- Distinguish between pe.docx
1- What are the functional sources of power- 2- Distinguish between pe.docx1- What are the functional sources of power- 2- Distinguish between pe.docx
1- What are the functional sources of power- 2- Distinguish between pe.docx
 
1- What are phonons- What information can be obtained by studying the.docx
1- What are phonons- What information can be obtained by studying the.docx1- What are phonons- What information can be obtained by studying the.docx
1- What are phonons- What information can be obtained by studying the.docx
 
1- void fun(int &j) { j++- } int main() { int i - 20- fun(i)- printf(-.docx
1- void fun(int &j) { j++- } int main() { int i - 20- fun(i)- printf(-.docx1- void fun(int &j) { j++- } int main() { int i - 20- fun(i)- printf(-.docx
1- void fun(int &j) { j++- } int main() { int i - 20- fun(i)- printf(-.docx
 
1- Single convex lenses can be used to make images of far away objects.docx
1- Single convex lenses can be used to make images of far away objects.docx1- Single convex lenses can be used to make images of far away objects.docx
1- Single convex lenses can be used to make images of far away objects.docx
 

Dernier

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
 

Dernier (20)

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
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
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.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)
 
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
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
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_...
 
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Ữ Â...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
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
 
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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 

1- The following given statements- import java-io--- import java-util.docx

  • 1. 1. The following given statements: import java.io.*; import java.util.*; 2. The following given main() method: public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter an integer value: "); long number = input.nextLong(); System.out.println("The number of odd digits in the number is " + countOdd(number)); } 3. A recursive method countOdd that will compute the number of odd digits in a number. The method must handle inputs of both positive and negative numbers. Solution import java.util.Scanner; public class Recursive { public static int countOdd( long num){ int cnt = 0; if (num == 0) return 0; long rem = num % 10;
  • 2. cnt = countOdd (num/10); if ((rem % 2) != 0) cnt += 1; return cnt; } public static void main(String[] args){ Scanner input = new Scanner(System. in ); System. out .println("Enter an integer value: "); long number = input.nextLong(); System. out .println("The number of odd digits in the number is " + countOdd (number)); } }