[Go to site: main page, start]

0% found this document useful (0 votes)
13 views21 pages

Basic Programs Java

The document contains a collection of basic Java programs demonstrating fundamental programming concepts, including input/output, arithmetic operations, conditional statements, and loops. It features examples such as printing 'Hello World', calculating the area of shapes, checking even or odd numbers, and generating multiplication tables. Additionally, it includes variations using Scanner for user input and different control structures like if-else, switch-case, while, and do-while loops.
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)
13 views21 pages

Basic Programs Java

The document contains a collection of basic Java programs demonstrating fundamental programming concepts, including input/output, arithmetic operations, conditional statements, and loops. It features examples such as printing 'Hello World', calculating the area of shapes, checking even or odd numbers, and generating multiplication tables. Additionally, it includes variations using Scanner for user input and different control structures like if-else, switch-case, while, and do-while loops.
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

🔹 BASIC JAVA PROGRAMS (1–10)

1️⃣ Hello World


class Hello {
public static void main(String[] args) {
[Link]("Hello World");
}
}

2️⃣ Print Your Name


class Name {
public static void main(String[] args) {
[Link]("Richa");
}
}

3️⃣ Add Two Numbers


class Add {
public static void main(String[] args) {
int a = 4, b = 6;
[Link](a + b);
}
}

4️⃣ Subtract Two Numbers


class Subtract {
public static void main(String[] args) {
int a = 10, b = 5;
[Link](a - b);
}
}

5️⃣ Multiply Two Numbers


class Multiply {
public static void main(String[] args) {
int a = 3, b = 4;
[Link](a * b);
}
}

6️⃣ Divide Two Numbers


class Divide {
public static void main(String[] args) {
int a = 20, b = 5;
[Link](a / b);
}
}

7️⃣ Area of Rectangle


class Rectangle {
public static void main(String[] args) {
int l = 5, b = 4;
[Link](l * b);
}
}

8️⃣ Area of Circle


class Circle {
public static void main(String[] args) {
double r = 7;
[Link](3.14 * r * r);
}
}

9️⃣ Simple Interest


class Interest {
public static void main(String[] args) {
int p = 1000, r = 5, t = 2;
[Link]((p * r * t) / 100);
}
}

🔟 Celsius to Fahrenheit
class Temp {
public static void main(String[] args) {
int c = 25;
[Link]((c * 9 / 5) + 32);
}
}

🔹 IF–ELSE PROGRAMS (11–20)


1️⃣1️⃣ Even or Odd
class EvenOdd {
public static void main(String[] args) {
int n = 6;
if (n % 2 == 0)
[Link]("Even");
else
[Link]("Odd");
}
}
1️⃣2️⃣ Positive or Negative
class Check {
public static void main(String[] args) {
int n = -3;
if (n > 0)
[Link]("Positive");
else
[Link]("Negative");
}
}

1️⃣3️⃣ Largest of Two Numbers


class Largest2 {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
[Link](a);
else
[Link](b);
}
}

1️⃣4️⃣ Largest of Three Numbers


class Largest3 {
public static void main(String[] args) {
int a = 3, b = 9, c = 6;
if (a > b && a > c)
[Link](a);
else if (b > c)
[Link](b);
else
[Link](c);
}
}

1️⃣5️⃣ Check Leap Year


class Leap {
public static void main(String[] args) {
int y = 2024;
if (y % 4 == 0)
[Link]("Leap Year");
else
[Link]("Not Leap Year");
}
}

1️⃣6️⃣ Check Vowel or Consonant


class Vowel {
public static void main(String[] args) {
char ch = 'a';
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
[Link]("Vowel");
else
[Link]("Consonant");
}
}

1️⃣7️⃣ Check Eligibility to Vote


class Vote {
public static void main(String[] args) {
int age = 18;
if (age >= 18)
[Link]("Eligible");
else
[Link]("Not Eligible");
}
}

1️⃣8️⃣ Grade System


class Grade {
public static void main(String[] args) {
int m = 75;
if (m >= 90)
[Link]("A");
else if (m >= 60)
[Link]("B");
else
[Link]("C");
}
}

1️⃣9️⃣ Divisible by 5
class Div5 {
public static void main(String[] args) {
int n = 25;
if (n % 5 == 0)
[Link]("Divisible");
else
[Link]("Not Divisible");
}
}

2️⃣0️⃣ Check Equal Numbers


class Equal {
public static void main(String[] args) {
int a = 5, b = 5;
if (a == b)
[Link]("Equal");
else
[Link]("Not Equal");
}
}
🔹 LOOP PROGRAMS (21–30)
2️⃣1️⃣ Print 1 to 10
class OneToTen {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++)
[Link](i);
}
}

2️⃣2️⃣ Sum of First 10 Numbers


class Sum {
public static void main(String[] args) {
int s = 0;
for (int i = 1; i <= 10; i++)
s += i;
[Link](s);
}
}

2️⃣3️⃣ Multiplication Table


class Table {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= 10; i++)
[Link](n * i);
}
}

2️⃣4️⃣ Factorial
class Fact {
public static void main(String[] args) {
int f = 1, n = 5;
for (int i = 1; i <= n; i++)
f *= i;
[Link](f);
}
}

2️⃣5️⃣ Reverse a Number


class Reverse {
public static void main(String[] args) {
int n = 123, r = 0;
while (n != 0) {
r = r * 10 + n % 10;
n /= 10;
}
[Link](r);
}
}
2️⃣6️⃣ Palindrome Number
class Palindrome {
public static void main(String[] args) {
int n = 121, r = 0, t = n;
while (n != 0) {
r = r * 10 + n % 10;
n /= 10;
}
if (r == t)
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}

2️⃣7️⃣ Count Digits


class Count {
public static void main(String[] args) {
int n = 1234, c = 0;
while (n != 0) {
c++;
n /= 10;
}
[Link](c);
}
}

2️⃣8️⃣ Print Even Numbers


class Even {
public static void main(String[] args) {
for (int i = 2; i <= 10; i += 2)
[Link](i);
}
}

2️⃣9️⃣ Sum of Digits


class SumDigits {
public static void main(String[] args) {
int n = 123, s = 0;
while (n != 0) {
s += n % 10;
n /= 10;
}
[Link](s);
}
}

3️⃣0️⃣ Fibonacci Series


class Fibonacci {
public static void main(String[] args) {
int a = 0, b = 1;
[Link](a);
[Link](b);
for (int i = 1; i <= 5; i++) {
int c = a + b;
[Link](c);
a = b;
b = c;
}
}
}

## Hello Name

```java

Import [Link];

Class HelloName {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link](“Enter your name: “);

String name = [Link]();

[Link](“Hello “ + name);

```

## Add Two Numbers


```java

Import [Link];

Class Add {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int a = [Link]();

Int b = [Link]();

[Link](“Sum = “ + (a + b));

```

## Area of Rectangle

```java

Import [Link];

Class Rectangle {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int l = [Link]();

Int b = [Link]();

[Link](“Area = “ + (l * b));
}

```

## Even or Odd

```java

Import [Link];

Class EvenOdd {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int n = [Link]();

If (n % 2 == 0)

[Link](“Even”);

Else

[Link](“Odd”);

```

## Largest of Two Numbers


```java

Import [Link];

Class Largest {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int a = [Link]();

Int b = [Link]();

If (a > b)

[Link](a);

Else

[Link](b);

```

## Simple Interest

```java

Import [Link];

Class SimpleInterest {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


Int p = [Link]();

Int r = [Link]();

Int t = [Link]();

[Link](“SI = “ + (p * r * t) / 100);

```

## Factorial of a Number

```java

Import [Link];

Class Factorial {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int n = [Link]();

Int fact = 1;

For (int I = 1; I <= n; i++)

Fact *= I;

[Link](fact);

```
## Reverse a Number

```java

Import [Link];

Class Reverse {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int n = [Link](), r = 0;

While (n != 0) {

R = r * 10 + n % 10;

N /= 10;

[Link]®;

```

## Multiplication Table

```java

Import [Link];
Class Table {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int n = [Link]();

For (int I = 1; I <= 10; i++)

[Link](n + “ x “ + I + “ = “ + (n * i));

```

## Check Positive or Negative

```java

Import [Link];

Class PosNeg {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int n = [Link]();

If (n >= 0)

[Link](“Positive”);

Else

[Link](“Negative”);
}

## Switch Case Example (Menu Program)

```java

Import [Link];

Class SwitchDemo {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link](“1. Add”);

[Link](“2. Subtract”);

[Link](“3. Multiply”);

[Link](“4. Divide”);

[Link](“Enter your choice: “);

Int ch = [Link]();

[Link](“Enter two numbers: “);

Int a = [Link]();

Int b = [Link]();
Switch (ch) {

Case 1:

[Link](“Sum = “ + (a + b));

Break;

Case 2:

[Link](“Difference = “ + (a – b));

Break;

Case 3:

[Link](“Product = “ + (a * b));

Break;

Case 4:

[Link](“Quotient = “ + (a / b));

Break;

Default:

[Link](“Invalid choice”);

```

## Even or Odd Using Switch

```java
Import [Link];

Class EvenOddSwitch {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int n = [Link]();

Switch (n % 2) {

Case 0:

[Link](“Even”);

Break;

Case 1:

[Link](“Odd”);

Break;

```

## Day Name Using Switch

```java

Import [Link];
Class Day {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int d = [Link]();

Switch (d) {

Case 1: [Link](“Monday”); break;

Case 2: [Link](“Tuesday”); break;

Case 3: [Link](“Wednesday”); break;

Case 4: [Link](“Thursday”); break;

Case 5: [Link](“Friday”); break;

Case 6: [Link](“Saturday”); break;

Case 7: [Link](“Sunday”); break;

Default: [Link](“Invalid Day”);

```

## `while` Loop Example (Print 1 to 5)


```java

Class WhileDemo {

Public static void main(String[] args) {

Int I = 1;

While (I <= 5) {

[Link](i);

I++;

```

## `do-while` Loop Example (Print 1 to 5)

```java

Class DoWhileDemo {

Public static void main(String[] args) {

Int I = 1;

Do {

[Link](i);

I++;

} while (I <= 5);

```
## Multiplication Table Using `while`

```java

Import [Link];

Class TableWhile {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int n = [Link]();

Int I = 1;

While (I <= 10) {

[Link](n + “ x “ + I + “ = “ + (n * i));

I++;

```

## Multiplication Table Using `do-while`

```java

Import [Link];
Class TableDoWhile {

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Int n = [Link]();

Int I = 1;

Do {

[Link](n + “ x “ + I + “ = “ + (n * i));

I++;

} while (I <= 10);

```

## Even or Odd Using `while`

```java

Class EvenOddWhile {

Public static void main(String[] args) {

Int I = 1;

While (I <= 10) {

If (I % 2 == 0)

[Link](I + “ Even”);

Else
[Link](I + “ Odd”);

I++;

You might also like