The steps in writing a Java program is illustrated as
follows:
Step 1: Write the source codes (.java) using a
programming text editor (such as Notepad++, Textpad, gEdit) or an IDE (such as
Eclipse or NetBeans).
Step 2: Compile the source codes (.java) into Java
portable bytecode (.class) using the JDK compiler ("javac").
Step 3: Run the compiled bytecode (.class) with the
input to produce the desired output, using the Java Runtime ("java").
Below is a simple Java program that demonstrates the three
basic programming constructs: sequential, loop, and conditional.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/*
* Sum the odd numbers and the even numbers from a lowerbound to an upperbound */ public class OddEvenSum { // Save as "OddEvenSum.java" public static void main(String[] args) { int lowerbound = 1, upperbound = 1000; int sumOdd = 0; // For accumulating odd numbers, init to 0 int sumEven = 0; // For accumulating even numbers, init to 0 int number = lowerbound; while (number <= upperbound) { if (number % 2 == 0) { // Even sumEven += number; // Same as sumEven = sumEven + number } else { // Odd sumOdd += number; // Same as sumOdd = sumOdd + number } ++number; // Next number } // Print the result System.out.println("The sum of odd numbers from " + lowerbound + " to " + upperbound + " is " + sumOdd); System.out.println("The sum of even numbers from " + lowerbound + " to " + upperbound + " is " + sumEven); System.out.println("The difference between the two sums is " + (sumOdd - sumEven)); }
}
|
The expected outputs are:
The sum of odd numbers from 1 to 1000 is 250000
The sum of even numbers from 1 to 1000 is 250500
The difference between the two sums is -500
No comments:
Post a Comment
Please write your view and suggestion....