Name:
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 [Link];
public class Assignment1 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
while (num > 9) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
[Link]("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 [Link];
public class Assignment2 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reversed = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reversed += [Link](i);
}
[Link]("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 [Link];
public class Assignment3 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter year: ");
int year = [Link]();
boolean isLeap = (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
[Link](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;
[Link](a + " " + b);
for (int i = 2; i < 10; i++) {
int c = a + b;
[Link](" " + 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 [Link];
public class Assignment5 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String password;
do {
[Link]("Enter password: ");
password = [Link]();
} while ( ||  ||
[Link]() < 8);
[Link]("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 [Link];
public class Assignment6 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter two numbers: ");
int a = [Link](), b = [Link]();
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
[Link]("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 [Link];
public class Assignment7 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter percentage: ");
double p = [Link]();
char grade = p >= 90 ? 'A' : p >= 80 ? 'B' : p >= 70 ? 'C' : p >= 60 ?
'D' : 'F';
[Link]("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 [Link];
public class Assignment8 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
for (int i = 1; i <= 10; i++) {
[Link](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 [Link];
public class Assignment9 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
double balance = 1000;
int choice;
do {
[Link]("\n1. Check Balance\n2. Deposit\n3. Withdraw\n4.
Exit");
choice = [Link]();
switch (choice) {
case 1: [Link]("Balance: " + balance); break;
case 2: balance += [Link](); break;
case 3: balance -= [Link](); 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 [Link];
public class Assignment10 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter string: ");
String s = [Link]().toLowerCase();
int vowels = 0;
for (char c : [Link]()) {
if ("aeiou".indexOf(c) != -1) vowels++;
}
[Link]("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 [Link];
public class Assignment11 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter limit: ");
int limit = [Link]();
for (int n = 2; n <= limit; n++) {
boolean prime = true;
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) {
prime = false;
break;
}
}
if (prime) [Link](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.
public class Assignment12 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Question 13:
Implement a do-while loop to calculate compound interest for a principal amount
until it doubles, given an annual interest rate.
import [Link];
public class Assignment13 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter principal and rate: ");
double p = [Link](), r = [Link]() / 100;
int years = 0;
while (p < 2 * p) {
p *= (1 + r);
years++;
}
[Link]("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 [Link];
public class Assignment14 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
long fact = 1;
while (n > 1) fact *= n--;
[Link]("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.
public class Assignment15 {
public static void main(String[] args) {
int[] evens = new int[50], odds = new int[50];
int e = 0, o = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) evens[e++] = i;
else odds[o++] = i;
}
[Link]("Evens: ");
for (int num : evens) [Link](num + " ");
[Link]("\nOdds: ");
for (int num : odds) [Link](num + " ");
}
}
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 < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 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) [Link](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 [Link];
public class Assignment17 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link](), original = n, reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
[Link](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 [Link];
public class Assignment18 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter decimal: ");
int n = [Link]();
String binary = "";
while (n > 0) {
binary = (n % 2) + binary;
n /= 2;
}
[Link]("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 [Link];
public class Assignment19 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter 3 sides: ");
double a = [Link](), b = [Link](), c =
[Link]();
if (a == b && b == c) [Link]("Equilateral");
else if (a == b || b == c || a == c) [Link]("Isosceles");
else [Link]("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.
public class Assignment20 {
public static void main(String[] args) {
for (int n = 1; n <= 1000; n++) {
int sum = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) sum += i;
}
if (sum == n) [Link](n + " ");
}
}
}