0% found this document useful (0 votes)
2 views

Comp

The document contains 10 simple Java programs using BlueJ, demonstrating various control structures. It includes four if-else examples, two for-loop examples, two while-loop examples, and two do-while loop examples. Each program is designed to illustrate a specific concept such as checking number properties, printing sequences, and calculating sums or factorials.

Uploaded by

5avihaan189
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Comp

The document contains 10 simple Java programs using BlueJ, demonstrating various control structures. It includes four if-else examples, two for-loop examples, two while-loop examples, and two do-while loop examples. Each program is designed to illustrate a specific concept such as checking number properties, printing sequences, and calculating sums or factorials.

Uploaded by

5avihaan189
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Here are 10 simple Java programs using BlueJ, based on the different types of control

structures you requested.

a. if-else (4 programs)

1. Check if a number is positive or negative

java
Copy code
public class IfElseExample1 {
public static void main(String[] args) {
int number = 5;

if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is negative.");
}
}
}

2. Check if a number is even or odd

java
Copy code
public class IfElseExample2 {
public static void main(String[] args) {
int number = 8;

if (number % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}
}
}

3. Find the largest of two numbers

java
Copy code
public class IfElseExample3 {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;

if (num1 > num2) {


System.out.println("num1 is the largest.");
} else {
System.out.println("num2 is the largest.");
}
}
}

4. Check if a character is a vowel or consonant

java
Copy code
public class IfElseExample4 {
public static void main(String[] args) {
char ch = 'a';

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


{
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is a consonant.");
}
}
}

b. for…loop (2 programs)

1. Print numbers from 1 to 10

java
Copy code
public class ForLoopExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}

2. Print the sum of numbers from 1 to 5

java
Copy code
public class ForLoopExample2 {
public static void main(String[] args) {
int sum = 0;

for (int i = 1; i <= 5; i++) {


sum += i;
}

System.out.println("The sum is: " + sum);


}
}

c. while loop (2 programs)

1. Print numbers from 1 to 5 using while loop

java
Copy code
public class WhileLoopExample1 {
public static void main(String[] args) {
int i = 1;

while (i <= 5) {
System.out.println(i);
i++;
}
}
}

2. Print the factorial of 5 using while loop

java
Copy code
public class WhileLoopExample2 {
public static void main(String[] args) {
int number = 5;
int factorial = 1;
int i = 1;

while (i <= number) {


factorial *= i;
i++;
}

System.out.println("The factorial of " + number + " is: " +


factorial);
}
}

d. do…while loop (2 programs)

1. Print numbers from 1 to 5 using do-while loop

java
Copy code
public class DoWhileLoopExample1 {
public static void main(String[] args) {
int i = 1;

do {
System.out.println(i);
i++;
} while (i <= 5);
}
}

2. Print the sum of numbers from 1 to 5 using do-while loop

java
Copy code
public class DoWhileLoopExample2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;

do {
sum += i;
i++;
} while (i <= 5);
System.out.println("The sum is: " + sum);
}
}

You might also like