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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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