Write a segment of code that reads a sequence of integers from the keyboard unti
ID: 3786010 • Letter: W
Question
Write a segment of code that reads a sequence of integers from the keyboard until the user enters a negative number. It should then output a count of the number of even integers and the number of odd integers read (not including the final negative value in either count). Remember - 0 is an even number. For example, if the sequence is: 2 7 15 5 88 1243 104 -1 Then the output should be Number of even integers: 3 Number of odd integers: 4 Write a segment of code that reads a String from the keyboard and then outputs each letter in the String twice. For example, if the input String is "Welcome!" the code should output "WWeellccoommee!!" to the screen.Explanation / Answer
//OddEven.java
import java.util.Scanner;
public class OddEven
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sequence of numbers: ");
int number;
int evenCount = 0;
int oddCount = 0;
while(true)
{
number = sc.nextInt();
if(number < 0)
break;
else if(number%2 == 0)
evenCount++;
else if(number%2 != 0)
oddCount++;
}
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
}
}
/*
Output:
Enter the sequence of numbers:
2
7
15
5
88
1243
104
-2
Number of even integers: 3
Number of odd integers: 4
*/
//TwiceString.java
import java.util.Scanner;
public class TwiceString
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
for (int i = 0;i < str.length(); i++ )
{
System.out.print(str.charAt(i));
System.out.print(str.charAt(i));
}
System.out.println();
}
}
/*
Output:
Enter a string: Welcome
WWeellccoommee
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.