Assignment: Control Statements in Java

Additional Assignment: Control Statements in Java

Objective:

The objective of this assignment is to further deepen your understanding of control statements in Java, including if-else, switch, for, while, do-while, break, and continue statements. These exercises will challenge you with a variety of scenarios where control statements are crucial.


Part 1: Basic Coding Questions with Example Outputs

Question 1: Leap Year Checker

Write a Java program that checks if a given year is a leap year using if-else statements.

Example Output:

Enter a year: 2020
2020 is a leap year.

Question 2: Vowel or Consonant Checker

Write a Java program that checks whether a given character is a vowel or a consonant using a switch statement.

Example Output:

Enter a character: A
A is a vowel.

Question 3: Sum of Even Numbers Using for Loop

Write a Java program that calculates the sum of all even numbers between 1 and 100 using a for loop.

Example Output:

The sum of all even numbers between 1 and 100 is: 2550

Question 4: Counting Digits in a Number Using while Loop

Write a Java program that counts the number of digits in a given number using a while loop.

Example Output:

Enter a number: 12345
The number of digits in 12345 is 5.

Question 5: Sum of Digits Using do-while Loop

Write a Java program that calculates the sum of the digits of a given number using a do-while loop.

Example Output:

Enter a number: 1234
The sum of the digits of 1234 is 10.

Question 6: Using break in a Nested Loop

Write a Java program that finds the first pair of numbers (i, j) where i * j is greater than 50 using nested for loops and exits the loops using a break statement.

Example Output:

The first pair where i * j > 50 is: (6, 9)

Question 7: Print Odd Numbers Using continue

Write a Java program that prints all odd numbers between 1 and 20 using a for loop and the continue statement.

Example Output:

1
3
5
...
19

Question 8: Armstrong Number Between 1 and 1000

Write a Java program that finds and prints all Armstrong numbers between 1 and 1000 using nested loops.

Example Output:

Armstrong numbers between 1 and 1000 are: 153, 370, 371, 407

Question 9: Sum of Prime Numbers Using for Loop

Write a Java program that calculates the sum of all prime numbers between 1 and 100 using a for loop.

Example Output:

The sum of all prime numbers between 1 and 100 is: 1060

Question 10: Reverse Pyramid Pattern Using Nested Loops

Write a Java program that prints the following reverse pyramid pattern using nested for loops:

Example Output:

*****
****
***
**
*

Part 2: Advanced Coding Questions with Example Outputs

Question 11: Check Palindrome Using for Loop

Write a Java program that checks if a given string is a palindrome using a for loop.

Example Output:

Enter a string: radar
radar is a palindrome.

Question 12: Greatest Common Divisor (GCD) Using while Loop

Write a Java program that calculates the GCD of two numbers using the while loop.

Example Output:

Enter two numbers: 54 24
The GCD of 54 and 24 is: 6

Question 13: Floyd's Triangle Using Nested Loops

Write a Java program that prints Floyd's Triangle using nested loops. Floyd's Triangle is a right-angled triangular array of natural numbers.

Example Output:

1
2 3
4 5 6
7 8 9 10

Question 14: Display ASCII Values Using for Loop

Write a Java program that displays the ASCII values of characters from 'A' to 'Z' using a for loop.

Example Output:

A: 65
B: 66
C: 67
...
Z: 90

Question 15: Calculate Power of a Number Using while Loop

Write a Java program that calculates the power of a number (e.g., x^y) using a while loop.

Example Output:

Enter base: 3
Enter exponent: 4
3^4 = 81

Question 16: Display Prime Factors Using do-while Loop

Write a Java program that displays all the prime factors of a given number using a do-while loop.

Example Output:

Enter a number: 60
Prime factors of 60 are: 2, 2, 3, 5

Question 17: Generate Pascal's Triangle Using Nested Loops

Write a Java program that generates and prints Pascal's Triangle up to a given number of rows using nested loops.

Example Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Question 18: Count Vowels and Consonants Using for Loop

Write a Java program that counts the number of vowels and consonants in a given string using a for loop.

Example Output:

Enter a string: Hello World
Vowels: 3
Consonants: 7

Question 19: Convert Decimal to Binary Using while Loop

Write a Java program that converts a decimal number to its binary equivalent using a while loop.

Example Output:

Enter a decimal number: 10
Binary equivalent: 1010

Question 20: Prime Number Spiral Pattern Using Nested Loops

Write a Java program that prints a spiral pattern of prime numbers using nested loops.

Example Output:

2 3 5
7 11 13
17 19 23

These additional assignments will further enhance your understanding and application of control statements in Java. Each question includes an example output to help guide your implementation.

Last updated