Advertisement
acappelwest

Untitled

Oct 13th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class WhileLoops
  3. {
  4.     public static void main(String [] args)
  5.     {
  6.         Scanner keyboard = new Scanner(System.in);
  7.         int firstnum;
  8.         int secondnum;
  9.        
  10.         System.out.println("Enter an integer: ");
  11.         firstnum = keyboard.nextInt();
  12.        
  13.         System.out.println("Enter another, larger integer: ");
  14.         secondnum = keyboard.nextInt();
  15.        
  16.         System.out.println(" ");
  17.        
  18.         int sumE = 0;
  19.         int sumO = 0;
  20.         int countE = 1;
  21.         int countO = 1;
  22.        
  23.         System.out.print("Even numbers: " );
  24.         if (firstnum % 2 == 1)
  25.             countE = firstnum + 1;
  26.         else
  27.             countE = firstnum;
  28.         while (countE <= secondnum)
  29.         {
  30.             System.out.print(countE + " ");
  31.             sumE = sumE + countE;
  32.             countE = countE + 2;
  33.         }
  34.         System.out.println();
  35.         System.out.println("Sum of even numbers = " + sumE);
  36.        
  37.         System.out.print("Odd numbers: " );
  38.         if (firstnum % 2 == 0)
  39.             countO = firstnum + 1;
  40.         else
  41.             countO = firstnum;
  42.         while (countO <= secondnum)
  43.         {
  44.             System.out.print(countO + " ");
  45.             sumO = sumO + countO;
  46.             countO = countO + 2;
  47.         }
  48.         System.out.println();
  49.         System.out.println("Sum of odd numbers = " + sumO);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement