Write a segment of code that reads a sequence of integers fr

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.

Solution

//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

*/

 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 numbe
 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 numbe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site