Assignment 4
Assignment 4
19/03/2025
1. .WJP to print even numbers between 1 to 10 using for loop and if else
if (i % 2 == 0) {
System.out.println(i);
}
}
if (i % 3 == 0 && i % 5 == 0) {
System.out.println(i);
}
}
3.WJP to find sum of even numbers from 1 to 10 and 10 to 1 with iteration part
int sum = 0;
sum += i;
System.out.println("JAVA");
}
}
5. Algorithm: Start Create an instance of the Scanner class. Declare a number Ask the user
to initialize the number. Use a for loop to print the multiplication table of that number.
Display the result. Stop. WJP for above algorithm.
import java.util.Scanner;
}
}
}
6.WJP that calculates the factorial of a number using a for loop.
import java.util.Scanner;
int fact = 1;
fact *= i;
}
}
7.WJP to count the number of digits in an integer using a while loop. Example: Input 12345
→ Output: 5
import java.util.Scanner;
int count = 0;
while (num != 0) {
num /= 10;
count++;
8.WJP to Reverse a given number using a do-while loop. Example: Input 123 → Output:
321
import java.util.Scanner;
do {
int digit = num % 10;
num /= 10;
9.WJP Use a do-while loop to calculate the factorial of a number. Example: Input 5 →
Output: 120
import java.util.Scanner;
int fact = 1, i = 1;
do {
fact *= i;
i++;
} while (i <= num);
}
10.WJP Write a program to print the Fibonacci series using a while loop. Example: Input 5
→ Output: 0, 1, 1, 2, 3
import java.util.Scanner;
System.out.print("Enter N: ");
int n = sc.nextInt();
int a = 0, b = 1, c, i = 1;
while (i <= n) {
c = a + b;
a = b;
b = c;
i++;
11.WJP Write a program to calculate the sum of digits in a number using a while loop.
Example: Input 1234 → Output: 10
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
num /= 10;
12.WJP Use a while loop to print the multiplication table of a given number. Example:
Input 5 → Output: 5, 10, 15, ...
import java.util.Scanner;
int i = 1;
while (i <= 10) {
13.WJP Use a do-while loop to print numbers from N to 1. Example: Input 5 → Output: 5,
4, 3, 2, 1
import java.util.Scanner;
System.out.print("Enter N: ");
do {
System.out.print(num + " ");
num--;
}
}
14.WJP to find the sum of all prime numbers between two given numbers using a while
loop.
import java.util.Scanner;
int sum = 0;
if (isPrime(i)) sum += i;
return true;
}
15.Pattern Matching (Right - Angled Stars)
**
***
****
*****
System.out.print("*");
System.out.println();
}
}
16.Inverted Right-Angled
*****
****
***
**
System.out.print("*");
}
System.out.println();
17.Pyramid Pattern
***
*****
*******
System.out.println();
18.Hollow Square
int n = 5;
System.out.println();
}
}
19.Floyd's Triangle.
1
23
456
7 8 9 10
System.out.println();
}
}
}
20.Pascal's Triangle
1
11
121
1331
14641
int rows = 5;
for (int i = 0; i < rows; i++) {
int num = 1;
}
System.out.println();
}
}
21.Butterfly Pattern
int n = 4;
System.out.println();
System.out.print("*");
if (i > 1) System.out.print("*");
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
System.out.print("*");
System.out.println();
23.Hourglass Pattern
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = n; j > i; j--) System.out.print(" ");
System.out.println();
}
for (int i = 2; i <= n; i++) {
System.out.println();
24.Zig-Zag Pattern
System.out.println();
25. Write a java program to perform the addition of two matrices using nested for loops.
import java.util.Scanner;
System.out.println();
}
26.Write a java program to print all prime numbers between 1 and N using a for loop.
Example: Input N = 20 → Output: 2, 3, 5, 7, 11, 13, 17, 19
import java.util.Scanner;
System.out.print("Enter N: ");
int n = sc.nextInt();
for (int i = 2; i <= n; i++) {
}
static boolean isPrime(int num) {
return true;
27.Print the Fibonacci series up to N terms using a for loop. Example: Input N = 5 →
Output: 0, 1, 1, 2, 3
import java.util.Scanner;
public class FibonacciSeries {
System.out.print("Enter N: ");
int n = sc.nextInt();
int a = 0, b = 1, c;
a = b;
b = c;
}
}
import java.util.Scanner;
double sp = sc.nextDouble();
}
29.WJP to get input for salary and age. if salary greater than or equal to 20000 or age less
than or equal to 25 get input for required loan amount. If not print you are not eligible for
loan. if required loan amount is less than or equal to 50,000 print you are eligible for loan. if
it is greater than 50,000 print maximum loan amount is 50000.
import java.util.Scanner;
public class LoanEligibility {
} else {
}
}
}
30. Basic ATM Simulator Description: Simulate basic ATM operations like checking balance,
depositing money, and withdrawing money. Features: Use a loop to display a menu with
options (e.g., 1. Check Balance, 2. Deposit, 3. Withdraw, 4. Exit). Perform actions based on
user input. Validate withdrawal amounts to ensure sufficient balance. How It Works Initial
Balance: The user starts with a predefined balance ( 1000.0 in this case). ₹ Menu Options:
The menu offers four options: Check Balance: Displays the current balance. Deposit Money:
Adds a valid amount to the balance. Withdraw Money: Deducts a valid amount if sufficient
balance is available. Exit: Exits the ATM simulation. Validation: Deposits must be positive
numbers. Withdrawals must be positive and not exceed the available balance. Exit Loop:
The program exits when the user selects option 4.
import java.util.Scanner;
} else if (choice == 4) {
break;
} else System.out.println("Invalid choice!");