Advertisement
dimipan80

Odd and Even Product

Aug 8th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. /* You are given n integers. Write a program that checks whether the product of the odd elements
  2.  * is equal to the product of the even elements. Elements are counted from 1 to n,
  3.  * so the first element is odd, the second is even, etc. */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _10_OddAndEvenProduct {
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Scanner scan = new Scanner(System.in);
  12.         System.out.println("Enter all Integer numbers of the Sequence on single line, separated by a space:");
  13.         String inputLineStr = scan.nextLine();
  14.         scan.close();
  15.  
  16.         String[] numStr = inputLineStr.split(" ");
  17.         if (numStr.length > 1) {
  18.             long oddProduct = 1;
  19.             long evenProduct = 1;
  20.             boolean isOddMember = true;
  21.             for (int i = 0; i < numStr.length; i++) {
  22.                 int number = Integer.parseInt(numStr[i]);
  23.                 if (isOddMember) {
  24.                     oddProduct *= number;
  25.                 } else {
  26.                     evenProduct *= number;
  27.                 }
  28.  
  29.                 isOddMember = !isOddMember;
  30.             }
  31.  
  32.             if (oddProduct == evenProduct) {
  33.                 System.out.printf("yes\nproduct = %d !\n", oddProduct);
  34.             } else {
  35.                 System.out.printf("no\nodd_product = %d\neven_product = %d !\n",
  36.                         oddProduct, evenProduct);
  37.             }
  38.         } else if (inputLineStr.isEmpty()) {
  39.             System.out.println("Empty Sequence of numbers!!!");
  40.         } else {
  41.             System.out.printf("no\nodd_product = %s\neven_product = !\n", numStr[0]);
  42.         }
  43.     }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement