Mr. Felix Assignment Java
Mr. Felix Assignment Java
Index Number:
Course
Group D.
Question 1:
Write a Java program using a do-while loop to find the sum of digits of a number
until the sum becomes a single digit.
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
System.out.println("Single digit sum: " + num);
}
}
Question 2:
Create a program using a while loop to reverse a string without using any built-in
reverse methods.
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println("Reversed: " + reversed);
}
}
Question 3:
Use if/else statements to determine if a given year is a leap year, considering all
edge cases (e.g., century years).
import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter year: ");
int year = scanner.nextInt();
boolean isLeap = (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
System.out.println(isLeap ? "Leap year" : "Not a leap year");
}
}
Question 4:
Write a Java program with a for loop to generate the first 10 numbers in the
Fibonacci sequence.
public class Assignment4 {
public static void main(String[] args) {
int a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 2; i < 10; i++) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}
}
}
Question 5:
Implement a do-while loop to validate user input for a password that must contain
at least 8 characters, one uppercase letter, and one digit.
import java.util.Scanner;
public class Assignment5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String password;
do {
System.out.print("Enter password: ");
password = scanner.nextLine();
} while (!password.matches(".*[A-Z].*") || !password.matches(".*\\d.*") ||
password.length() < 8);
System.out.println("Password accepted");
}
}
Question 6:
Using a while loop, write a program to find the greatest common divisor (GCD) of
two numbers using the Euclidean algorithm.
import java.util.Scanner;
public class Assignment6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = scanner.nextInt(), b = scanner.nextInt();
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
System.out.println("GCD: " + a);
}
}
Question 7:
Create a program with nested if/else statements to categorize a student's grade
into A, B, C, D, or F based on a percentage score.
import java.util.Scanner;
public class Assignment7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter percentage: ");
double p = scanner.nextDouble();
char grade = p >= 90 ? 'A' : p >= 80 ? 'B' : p >= 70 ? 'C' : p >= 60 ?
'D' : 'F';
System.out.println("Grade: " + grade);
}
}
Question 8:
Write a Java program using a for loop to print a multiplication table (1 to 10) for
a user-specified number.
import java.util.Scanner;
public class Assignment8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number: ");
int n = scanner.nextInt();
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}
}
Question 9:
Use a do-while loop to simulate a simple ATM menu that allows users to check
balance, deposit, withdraw, or exit, with input validation.
import java.util.Scanner;
public class Assignment9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double balance = 1000;
int choice;
do {
System.out.println("\n1. Check Balance\n2. Deposit\n3. Withdraw\n4.
Exit");
choice = scanner.nextInt();
switch (choice) {
case 1: System.out.println("Balance: " + balance); break;
case 2: balance += scanner.nextDouble(); break;
case 3: balance -= scanner.nextDouble(); break;
}
} while (choice != 4);
}
}
Question 10:
Write a program with a while loop to count the number of vowels in a given string,
ignoring case sensitivity.
import java.util.Scanner;
public class Assignment10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter string: ");
String s = scanner.nextLine().toLowerCase();
int vowels = 0;
for (char c : s.toCharArray()) {
if ("aeiou".indexOf(c) != -1) vowels++;
}
System.out.println("Vowels: " + vowels);
}
}
Question 11:
Using if/else and a for loop, write a program to check if a number is prime and
print all prime numbers up to a given limit.
import java.util.Scanner;
public class Assignment11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter limit: ");
int limit = scanner.nextInt();
for (int n = 2; n <= limit; n++) {
boolean prime = true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
prime = false;
break;
}
}
if (prime) System.out.print(n + " ");
}
}
}
Question 12:
Create a Java program with a nested for loop to print a pattern of stars in a
right-angled triangle with 5 rows.
import java.util.Scanner;
public class Assignment13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter principal and rate: ");
double p = scanner.nextDouble(), r = scanner.nextDouble() / 100;
int years = 0;
while (p < 2 * p) {
p *= (1 + r);
years++;
}
System.out.println("Years to double: " + years);
}
}
Question 14:
Write a program using a while loop to find the factorial of a number, handling
cases where the input is negative or zero.
import java.util.Scanner;
public class Assignment14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number: ");
int n = scanner.nextInt();
long fact = 1;
while (n > 1) fact *= n--;
System.out.println("Factorial: " + fact);
}
}
Question 15:
Use if/else statements within a for loop to separate even and odd numbers from 1 to
100 and store them in two separate arrays.
Question 16:
Write a Java program with a for loop to implement bubble sort for an array of
integers in ascending order.
public class Assignment16 {
public static void main(String[] args) {
int[] arr = {5, 3, 8, 6, 2};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int num : arr) System.out.print(num + " ");
}
}
Question 17:
Create a program using a do-while loop to repeatedly prompt the user for a number
and check if it is a palindrome until they choose to exit.
import java.util.Scanner;
public class Assignment17 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number: ");
int n = scanner.nextInt(), original = n, reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
System.out.println(original == reversed ? "Palindrome" : "Not palindrome");
}
}
Question 18:
Using a while loop, write a program to convert a decimal number to its binary
representation without using built-in methods.
import java.util.Scanner;
public class Assignment18 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter decimal: ");
int n = scanner.nextInt();
String binary = "";
while (n > 0) {
binary = (n % 2) + binary;
n /= 2;
}
System.out.println("Binary: " + binary);
}
}
Question 19:
Implement a nested if/else structure to determine the type of triangle
(equilateral, isosceles, scalene) based on three side lengths, with input
validation.
import java.util.Scanner;
public class Assignment19 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter 3 sides: ");
double a = scanner.nextDouble(), b = scanner.nextDouble(), c =
scanner.nextDouble();
if (a == b && b == c) System.out.println("Equilateral");
else if (a == b || b == c || a == c) System.out.println("Isosceles");
else System.out.println("Scalene");
}
}
Question 20:
Write a Java program using a for loop to find all perfect numbers (where the sum of
proper divisors equals the number) between 1 and 1000.