Seu SlideShare está sendo baixado. ×

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

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio  Carregando em…3
×

1 de 2 Anúncio

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

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));
}
}
.

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));
}
}
.

Anúncio
Anúncio

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

1. 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. 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)); } }