Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class WhileLoops
- {
- public static void main(String [] args)
- {
- Scanner keyboard = new Scanner(System.in);
- int firstnum;
- int secondnum;
- System.out.println("Enter an integer: ");
- firstnum = keyboard.nextInt();
- System.out.println("Enter another, larger integer: ");
- secondnum = keyboard.nextInt();
- System.out.println(" ");
- int sumE = 0;
- int sumO = 0;
- int countE = 1;
- int countO = 1;
- System.out.print("Even numbers: " );
- if (firstnum % 2 == 1)
- countE = firstnum + 1;
- else
- countE = firstnum;
- while (countE <= secondnum)
- {
- System.out.print(countE + " ");
- sumE = sumE + countE;
- countE = countE + 2;
- }
- System.out.println();
- System.out.println("Sum of even numbers = " + sumE);
- System.out.print("Odd numbers: " );
- if (firstnum % 2 == 0)
- countO = firstnum + 1;
- else
- countO = firstnum;
- while (countO <= secondnum)
- {
- System.out.print(countO + " ");
- sumO = sumO + countO;
- countO = countO + 2;
- }
- System.out.println();
- System.out.println("Sum of odd numbers = " + sumO);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement