0% found this document useful (0 votes)
17 views10 pages

Java Programming Basics and Examples

The document contains multiple Java programs demonstrating basic programming concepts such as checking if a number is positive, negative, or zero, determining leap years, displaying days of the week, performing arithmetic operations, and printing patterns. It also includes examples for calculating factorials, summing digits, finding the smallest of three numbers, printing prime numbers, simulating an ATM, and generating multiplication tables. Each program is structured with input handling and output statements to illustrate the respective functionality.

Uploaded by

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

Java Programming Basics and Examples

The document contains multiple Java programs demonstrating basic programming concepts such as checking if a number is positive, negative, or zero, determining leap years, displaying days of the week, performing arithmetic operations, and printing patterns. It also includes examples for calculating factorials, summing digits, finding the smallest of three numbers, printing prime numbers, simulating an ATM, and generating multiplication tables. Each program is structured with input handling and output statements to illustrate the respective functionality.

Uploaded by

kimmykelv7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Check Positive, Negative, or Zero

import [Link];

public class CheckNumber {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter a number: ");

int num = [Link]();

if (num > 0) {

[Link](num + " is positive.");

} else if (num < 0) {

[Link](num + " is negative.");

} else {

[Link](num + " is zero.");

[Link](); // Close the scanner to prevent resource leaks

2. Determine Leap Year

import [Link];

public class LeapYear {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter a year: ");

int year = [Link]();

boolean isLeap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;


if (isLeap) {

[Link](year + " is a leap year.");

} else {

[Link](year + " is not a leap year.");

[Link]();

3. Display Day of the Week

import [Link];

public class DayOfWeek {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter a number (1-7): ");

int dayNum = [Link]();

String day;

switch (dayNum) {

case 1:

day = "Monday";

break;

case 2:

day = "Tuesday";

break;

case 3:

day = "Wednesday";

break;

case 4:
day = "Thursday";

break;

case 5:

day = "Friday";

break;

case 6:

day = "Saturday";

break;

case 7:

day = "Sunday";

break;

default:

day = "Invalid input.";

[Link](day);

[Link]();

4. Basic Arithmetic Operations

import [Link];

public class ArithmeticOperations {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter first number: ");

double num1 = [Link]();

[Link]("Enter second number: ");

double num2 = [Link]();

[Link]("Enter an operator (+, -, *, /): ");


char operator = [Link]().charAt(0);

double result;

switch (operator) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

result = num1 / num2;

break;

default:

result = [Link]; // Not a Number for invalid input

if ([Link](result)) {

[Link]("Invalid operator.");

} else {

[Link](num1 + " " + operator + " " + num2 + " = " + result);

[Link]();

5. Print Numbers from 1 to 100 (for loop)


public class PrintNumbersFor {

public static void main(String[] args) {

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

[Link](i + " ");

[Link](); // Add a newline at the end

6. Calculate Factorial of 9 (while loop)

public class FactorialWhile {

public static void main(String[] args) {

int num = 9;

long factorial = 1; // Use long to handle larger factorials

int i = 1;

while (i <= num) {

factorial *= i;

i++;

[Link]("Factorial of " + num + " is: " + factorial);

7. Sum Digits Between 23 and 67 (for loop)

public class SumDigits {

public static void main(String[] args) {

int sum = 0;

for (int i = 23; i <= 67; i++) {

sum += i;
}

[Link]("The sum of digits between 23 and 67 is: " + sum);

8. Find Smallest of Three Numbers

import [Link];

public class SmallestNumber {

public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter first number: ");

int num1 = [Link]();

[Link]("Enter second number: ");

int num2 = [Link]();

[Link]("Enter third number: ");

int num3 = [Link]();

int smallest = findSmallest(num1, num2, num3);

[Link]("The smallest number is: " + smallest);

[Link]();

public static int findSmallest(int a, int b, int c) {

return [Link](a, [Link](b, c)); // Using [Link] is the most efficient approach

9. Print Pattern 1
public class Pattern1 {

public static void main(String[] args) {

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

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

[Link]("*");

[Link]();

10. Print Pattern 2

public class Pattern2 {

public static void main(String[] args) {

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

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

[Link]("*");

[Link]();

11. Print Prime Numbers (do-while)

public class PrimeNumbers {

public static void main(String[] args) {

int num = 2;

do {

boolean isPrime = true;


int i = 2;

while (i * i <= num) {

if (num % i == 0) {

isPrime = false;

break;

i++;

if (isPrime && num > 1) {

[Link](num + " ");

num++;

} while (num <= 100);

[Link]();

12 . Simple ATM Simulation


import [Link];

public class ATM {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);
int balance = 1000; // Initial balance

simulateATM(scanner, balance);

[Link]();
}

static void simulateATM(Scanner scanner, int balance) {


int choice;

do {
[Link]("\nATM Menu:");
[Link]("1. Check Balance");
[Link]("2. Deposit Money");
[Link]("3. Withdraw Money");
[Link]("4. Exit");
[Link]("Enter your choice: ");
choice = [Link]();

switch (choice) {
case 1:
[Link]("Your balance is: $" + balance);
break;
case 2:
[Link]("Enter amount to deposit: ");
int deposit = [Link]();
balance += deposit;
[Link]("Deposit successful.");
break;
case 3:
[Link]("Enter amount to withdraw: ");
int withdraw = [Link]();
if (withdraw <= balance) {
balance -= withdraw;
[Link]("Withdrawal successful.");
} else {
[Link]("Insufficient balance.");
}
break;
case 4:
[Link]("Exiting ATM. Thank you!");
break;
default:
[Link]("Invalid choice. Please try again.");
}
} while (choice != 4);
}
}

13. Pattern Printing

import [Link];

public class Pattern1 {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

printPattern(scanner);

[Link]();

static void printPattern(Scanner scanner) {

[Link]("Enter the number of rows: ");

int rows = [Link]();


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

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

[Link](j + " ");

[Link]();

14. Multiplication Table

import [Link];

public class MultiplicationTable {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

printMultiplicationTable(scanner);

[Link]();

static void printMultiplicationTable(Scanner scanner) {

[Link]("Enter the limit for the table: ");

int limit = [Link]();

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

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

[Link]("%4d", i * j);

[Link]();

You might also like