Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given n integers. Write a program that checks whether the product of the odd elements
- * is equal to the product of the even elements. Elements are counted from 1 to n,
- * so the first element is odd, the second is even, etc. */
- import java.util.Scanner;
- public class _10_OddAndEvenProduct {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter all Integer numbers of the Sequence on single line, separated by a space:");
- String inputLineStr = scan.nextLine();
- scan.close();
- String[] numStr = inputLineStr.split(" ");
- if (numStr.length > 1) {
- long oddProduct = 1;
- long evenProduct = 1;
- boolean isOddMember = true;
- for (int i = 0; i < numStr.length; i++) {
- int number = Integer.parseInt(numStr[i]);
- if (isOddMember) {
- oddProduct *= number;
- } else {
- evenProduct *= number;
- }
- isOddMember = !isOddMember;
- }
- if (oddProduct == evenProduct) {
- System.out.printf("yes\nproduct = %d !\n", oddProduct);
- } else {
- System.out.printf("no\nodd_product = %d\neven_product = %d !\n",
- oddProduct, evenProduct);
- }
- } else if (inputLineStr.isEmpty()) {
- System.out.println("Empty Sequence of numbers!!!");
- } else {
- System.out.printf("no\nodd_product = %s\neven_product = !\n", numStr[0]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement