0% found this document useful (0 votes)
19 views

Assignment 4

The document contains a series of Java programming assignments that cover various concepts such as loops, conditionals, and algorithms. Each assignment includes a brief description of the task, followed by the corresponding Java code implementation. Topics range from printing even numbers and calculating factorials to generating patterns and performing matrix operations.

Uploaded by

seenu.80506
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assignment 4

The document contains a series of Java programming assignments that cover various concepts such as loops, conditionals, and algorithms. Each assignment includes a brief description of the task, followed by the corresponding Java code implementation. Topics range from printing even numbers and calculating factorials to generating patterns and performing matrix operations.

Uploaded by

seenu.80506
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Assignment 3

19/03/2025

1. .WJP to print even numbers between 1 to 10 using for loop and if else

public class EvenNumbers {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

if (i % 2 == 0) {

System.out.println(i);

}
}

2.WJP to print the number which are divisible by both 3 and 5

public class DivisibleBy3And5 {

public static void main(String[] args) {


for (int i = 1; i <= 100; 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

public class SumEvenNumbers {

public static void main(String[] args) {

int sum = 0;

for (int i = 2; i <= 10; i += 2) {

sum += i;

System.out.println("Sum of even numbers: " + sum);


}

4.WJP display your name 5 times with iteration part

public class PrintName {

public static void main(String[] args) {


for (int i = 1; i <= 5; 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;

public class MultiplicationTable {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

for (int i = 1; i <= 10; i++) {

System.out.println(num + " x " + i + " = " + (num * i));

}
}

}
6.WJP that calculates the factorial of a number using a for loop.

import java.util.Scanner;

public class Factorial {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = sc.nextInt();

int fact = 1;

for (int i = 1; i <= num; i++) {

fact *= i;

System.out.println("Factorial: " + fact);

}
}
7.WJP to count the number of digits in an integer using a while loop. Example: Input 12345
→ Output: 5

import java.util.Scanner;

public class CountDigits {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = sc.nextInt();

int count = 0;
while (num != 0) {

num /= 10;

count++;

System.out.println("Number of digits: " + count);


}

8.WJP to Reverse a given number using a do-while loop. Example: Input 123 → Output:
321

import java.util.Scanner;

public class ReverseNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = sc.nextInt();
int rev = 0;

do {
int digit = num % 10;

rev = rev * 10 + digit;

num /= 10;

} while (num > 0);


System.out.println("Reversed Number: " + rev);

9.WJP Use a do-while loop to calculate the factorial of a number. Example: Input 5 →
Output: 120

import java.util.Scanner;

public class FactorialDoWhile {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int fact = 1, i = 1;

do {

fact *= i;

i++;
} while (i <= num);

System.out.println("Factorial: " + fact);

}
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;

public class Fibonacci {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter N: ");

int n = sc.nextInt();

int a = 0, b = 1, c, i = 1;

while (i <= n) {

System.out.print(a + " ");

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) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();


int sum = 0;

while (num > 0) {

sum += num % 10;

num /= 10;

System.out.println("Sum of digits: " + sum);

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;

public class MultiplicationTableWhile {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int i = 1;
while (i <= 10) {

System.out.println(num + " x " + i + " = " + (num * i));


i++;
}

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;

public class PrintNto1 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter N: ");

int num = sc.nextInt();

do {
System.out.print(num + " ");

num--;

} while (num > 0);

}
}
14.WJP to find the sum of all prime numbers between two given numbers using a while
loop.

import java.util.Scanner;

public class SumOfPrimes {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter two numbers: ");

int start = sc.nextInt();


int end = sc.nextInt();

int sum = 0;

for (int i = start; i <= end; i++) {

if (isPrime(i)) sum += i;

System.out.println("Sum of primes: " + sum);

static boolean isPrime(int num) {


if (num < 2) return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) return false;

return true;

}
15.Pattern Matching (Right - Angled Stars)

**

***

****

*****

public class RightAngledStar {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

System.out.print("*");

System.out.println();
}

}
16.Inverted Right-Angled

*****

****

***
**

public class InvertedStar {

public static void main(String[] args) {

for (int i = 5; i >= 1; i--) {

for (int j = 1; j <= i; j++) {

System.out.print("*");
}

System.out.println();

17.Pyramid Pattern

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

public class PyramidPattern {

public static void main(String[] args) {


int n = 4;

for (int i = 1; i <= n; i++) {

for (int j = i; j < n; j++) System.out.print(" ");

for (int k = 1; k <= (2 * i - 1); k++) System.out.print("*");

System.out.println();

18.Hollow Square

public class HollowSquare {

public static void main(String[] args) {

int n = 5;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n; j++) {


if (i == 1 || i == n || j == 1 || j == n) System.out.print("*");
else System.out.print(" ");

System.out.println();

}
}

19.Floyd's Triangle.
1

23

456

7 8 9 10

public class FloydsTriangle {

public static void main(String[] args) {


int n = 5, num = 1;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(num++ + " ");

System.out.println();

}
}
}

20.Pascal's Triangle
1

11

121

1331

14641

public class PascalsTriangle {

public static void main(String[] args) {

int rows = 5;
for (int i = 0; i < rows; i++) {

for (int j = 0; j < rows - i; j++) System.out.print(" ");

int num = 1;

for (int j = 0; j <= i; j++) {

System.out.print(num + " ");

num = num * (i - j) / (j + 1);

}
System.out.println();

}
}
21.Butterfly Pattern

public class ButterflyPattern {

public static void main(String[] args) {

int n = 4;

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= i; j++) System.out.print("*");

for (int j = 1; j <= 2 * (n - i); j++) System.out.print(" ");

for (int j = 1; j <= i; j++) System.out.print("*");

System.out.println();

for (int i = n; i >= 1; i--) {

for (int j = 1; j <= i; j++) System.out.print("*");


for (int j = 1; j <= 2 * (n - i); j++) System.out.print(" ");

for (int j = 1; j <= i; j++) System.out.print("*");


System.out.println();
}

22.Hallow Diamond Pattern

public class HollowDiamond {

public static void main(String[] args) {


int n = 4;

for (int i = 1; i <= n; i++) {

for (int j = i; j < n; j++) System.out.print(" ");

System.out.print("*");

for (int j = 1; j < (i - 1) * 2; j++) System.out.print(" ");

if (i > 1) System.out.print("*");

System.out.println();
}
for (int i = n - 1; i >= 1; i--) {

for (int j = n; j > i; j--) System.out.print(" ");

System.out.print("*");

for (int j = 1; j < (i - 1) * 2; j++) System.out.print(" ");


if (i > 1) System.out.print("*");

System.out.println();

23.Hourglass Pattern

public class HourglassPattern {

public static void main(String[] args) {

int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = n; j > i; j--) System.out.print(" ");

for (int j = 1; j <= (2 * i - 1); j++) System.out.print("*");

System.out.println();

}
for (int i = 2; i <= n; i++) {

for (int j = n; j > i; j--) System.out.print(" ");

for (int j = 1; j <= (2 * i - 1); j++) System.out.print("*");

System.out.println();

24.Zig-Zag Pattern

public class ZigZagPattern {

public static void main(String[] args) {


int n = 7;
for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= n; j++) {

if ((i + j) % 4 == 0 || (i == 2 && j % 4 == 0)) System.out.print("*");

else System.out.print(" ");


}

System.out.println();

25. Write a java program to perform the addition of two matrices using nested for loops.

import java.util.Scanner;

public class A25 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int[][] A = new int[2][2], B = new int[2][2], sum = new int[2][2];

System.out.println("Enter elements of first matrix:");

for (int i = 0; i < 2; i++)


for (int j = 0; j < 2; j++) A[i][j] = sc.nextInt();

System.out.println("Enter elements of second matrix:");

for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++) B[i][j] = sc.nextInt();

for (int i = 0; i < 2; i++)


for (int j = 0; j < 2; j++) sum[i][j] = A[i][j] + B[i][j];
System.out.println("Resultant matrix:");

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) System.out.print(sum[i][j] + " ");

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;

public class PrimeNumbers {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter N: ");
int n = sc.nextInt();
for (int i = 2; i <= n; i++) {

if (isPrime(i)) System.out.print(i + " ");

}
static boolean isPrime(int num) {

if (num < 2) return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) return false;

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 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter N: ");

int n = sc.nextInt();

int a = 0, b = 1, c;

for (int i = 1; i <= n; i++) {


System.out.print(a + " ");
c = a + b;

a = b;

b = c;

}
}

28.WJP to calculate Profit & loss

import java.util.Scanner;

public class A28 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter Cost Price: ");


double cp = sc.nextDouble();

System.out.print("Enter Selling Price: ");

double sp = sc.nextDouble();

if (sp > cp) System.out.println("Profit: " + (sp - cp));

else if (cp > sp) System.out.println("Loss: " + (cp - sp));

else System.out.println("No Profit No Loss");

}
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 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter salary: ");

int salary = sc.nextInt();

System.out.print("Enter age: ");

int age = sc.nextInt();


if (salary >= 20000 || age <= 25) {

System.out.print("Enter required loan amount: ");

int loan = sc.nextInt();

if (loan <= 50000) System.out.println("You are eligible for the loan.");

else System.out.println("Maximum loan amount is 50000.");

} else {

System.out.println("You are not eligible for the loan.");

}
}

}
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;

public class ATM {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double balance = 1000.0;


while (true) {

System.out.println("\n1. Check Balance\n2. Deposit\n3. Withdraw\n4. Exit");

System.out.print("Choose an option: ");

int choice = sc.nextInt();

if (choice == 1) System.out.println("Balance: ₹" + balance);


else if (choice == 2) {
System.out.print("Enter deposit amount: ");

double amount = sc.nextDouble();

if (amount > 0) balance += amount;

else System.out.println("Invalid amount!");


} else if (choice == 3) {

System.out.print("Enter withdrawal amount: ");

double amount = sc.nextDouble();

if (amount > 0 && amount <= balance) balance -= amount;

else System.out.println("Invalid transaction!");

} else if (choice == 4) {

System.out.println("Thank you for using the ATM!");

break;
} else System.out.println("Invalid choice!");

You might also like