Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions content/programs/add-two-integers.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Java Program to Add Two Integers
shortTitle: Add Two Integers
description: In this program, you'll learn to store and add two integer numbers in Java. After addition, the final sum is displayed on the screen.
---

The integer is stored in a variable using `System.in`, and is displayed on the screen using `System.out`.

To understand this example, you should have the knowledge of the following Java programming topics:

- [Java Operators](/docs/operators)
- [Java Basic Input and Output](/docs/basic-input-output)

## 1. Add Two Integers

A Java program that add two numbers predefined by the user is as follows:

### Example: Program to Add Two Integers

#### Input

```java
class Main {

public static void main(String[] args) {

System.out.println("Enter two numbers");
int first = 10;
int second = 20;

System.out.println(first + " " + second);

// add two numbers
int sum = first + second;
System.out.println("The sum is: " + sum);
}
}
```

#### Output

```plaintext
Enter two numbers
10 20
The sum is: 30
```
In this program, two integers `10` and `20` are stored in integer variables `first` and `second` respectively.

Then, `first` and `second` are added using the `+` operator, and its result is stored in another variable `sum`.

Finally, `sum` is printed on the screen using `println()` function.
78 changes: 78 additions & 0 deletions content/programs/calculate-compound-interest.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Java program To Calculate Compound Interest
shotTitle: Calculate Compound Interest
description: This code takes the principal amount, annual interest rate (as a percentage), number of years, and compounding frequency (how many times per year the interest is compounded) as input from the user.
---

The integer is stored in a variable using `System.in`, and is displayed on the screen using `System.out`.

To understand this example, you should have the knowledge of the following Java programming topics:

- [Java Operators](/docs/operators)
- [Java Basic Input and Output](/docs/basic-input-output)

## To calculate the Compound Interest

A java program to calculate Compound Interest


## Java Code

```java
import java.util.Scanner;

public class CompoundInterestCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input principal amount
System.out.print("Enter the principal amount: ");
double principal = scanner.nextDouble();

// Input annual interest rate (as a percentage)
System.out.print("Enter the annual interest rate (as a percentage): ");
double annualRate = scanner.nextDouble();

// Input number of years
System.out.print("Enter the number of years: ");
int years = scanner.nextInt();

// Input number of times interest is compounded per year
System.out.print("Enter the number of times interest is compounded per year: ");
int compoundingFrequency = scanner.nextInt();

// Convert annual rate to decimal and calculate compound interest
double rate = annualRate / 100;
double amount = principal * Math.pow(1 + (rate / compoundingFrequency), compoundingFrequency * years);

// Calculate compound interest
double compoundInterest = amount - principal;

// Display the result
System.out.println("The compound interest after " + years + " years is: " + compoundInterest);

// Close the scanner
scanner.close();
}
}
```

#### Output 1

```plaintext
Enter the principal amount: 5000
Enter the annual interest rate (as a percentage): 5
Enter the number of years: 3
Enter the number of times interest is compounded per year: 4
The compound interest after 3 years is: 797.1955807499652
```

#### Output 2

```plaintext
Enter the principal amount: 10000
Enter the annual interest rate (as a percentage): 3.5
Enter the number of years: 5
Enter the number of times interest is compounded per year: 12
The compound interest after 5 years is: 1938.8365362833173
```
40 changes: 40 additions & 0 deletions content/programs/calculate-power-of-a-number.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Java Program to calculate power of a number
shortTitle: Calculate power of a number
description: In this program you'll learn, How to calculate power of a number
---

To understand this example, you should have the knowledge of the following Java programming topics:

- [Java Operators](/docs/operators)
- [Java Basic Input and Output](/docs/basic-input-output)
- [Control Flow statement - For-Loop in Java](/docs/for-loop)
## Calculating power of a number
A java program to calculate the power of a number is as follows:

```java
public class power {
public static void main(String[] args){
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Base Number: ");
int baseNumber = input.nextInt();
System.out.print("Power of : ");
int powerOf = input.nextInt();
int output = baseNumber;
for(int i=1;i<powerOf;i++){
output*=baseNumber;
}
System.out.println(baseNumber + " power of " + powerOf + " = " + output );
}
}
```

#### Output:

```plaintext
Base Number: 2
Power of : 6
2 power of 6 = 64
```

Here we are taking two input from user and storing in a variable `baseNumber, powerOf`. To calculating the power of a number we initialize the output variable with base number and multiplying the baseNumber in a loop for powerOf-1 times inorder to get final result.
68 changes: 68 additions & 0 deletions content/programs/calculate-simple-interest.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: Java Program to Calculate Simple interest
shotTitle: Calculate Simple Interest
description: In this program, you'll learn to calculate the simple interest and display final value on screen/terminal
---

To understand this example, you should have the knowledge of the following Java programming topics:

- [Java Operators](/docs/operators)
- [Java Basic Input and Output](/docs/basic-input-output)

## Calculate Simple Interest

A java program that calculate the simple interest by getting user input for priniple amount, Rate of interest, Time Period.

```java
import java.util.Scanner;
public class simpleInterest {
public static void main(String[] args){
int principleAmount,time;
float interestRatePerAnnum;
// instance of Scanner class
Scanner input = new Scanner(System.in);
// taking user input
System.out.print("Enter the principle amount : ");
principleAmount = input.nextInt();
System.out.print("Enter the rate of interest(per annum): ");
interestRatePerAnnum = input.nextFloat();
System.out.print("Enter the time period(y): ");
time = input.nextInt();

float simpleInterest = (principleAmount * interestRatePerAnnum * time)/100;

System.out.println("Simple Interest: " + simpleInterest);

float totalAmountToReceive = principleAmount + simpleInterest;
System.out.println("Total amount person receive after " + time + " year" + ": " + totalAmountToReceive);
}
}
```

#### Output

```plaintext
Enter the principle amount : 1500
Enter the rate of interest(per annum): 8
Enter the time period(y): 5
Simple Interest: 600.0
Total amount person receive after 5 year: 2100.
```

To Calculate Simple interest ,
`SimpleInterest = (P*R*T)/100`


here, P = principleAmount
R = Rate of interest (%)
T = Time Period (per annum),

We need to take user input for following (P,R,T) and then apply the formulae using `*,/` operator. And then, print the output to user's terminal using `println()` funciton.

<Callout>

Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input)

Here two input numbers are taken from user one after another with space in between them which distinguish between two different inputs, this useful behavior is because of the default settings of Scanner called as Delimiter, [learn more here](https://www.javatpoint.com/post/java-scanner-delimiter-method).

</Callout>
44 changes: 44 additions & 0 deletions content/programs/check-even-or-odd.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: Java Program to check Even or Odd number
shortTitle: Check Even or Odd number
description: In this program, you'll learn to check if an integer is even or odd in Java.
---

```java
import java.util.Scanner;

class CheckEvenOdd
{
public static void main(String args[])
{
int num;
System.out.println("Enter an Integer number:");

//The input provided by user is stored in num
Scanner input = new Scanner(System.in);
num = input.nextInt();

/* If number is divisible by 2 then it's an even number
* else odd number*/
if ( num % 2 == 0 )
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
}
}
```
#### Output 1:

```plaintext
Enter an Integer number:
78
Entered number is even
```

#### Output 2:

```plaintext
Enter an Integer number:
77
Entered number is odd
```
20 changes: 20 additions & 0 deletions content/programs/contents.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Contents
description: A collection of basic Java programs categorized by topic.
---

## List of Programs Available

1. [Print an Integer](/programs/print-an-integer)
2. [Add Two Integers](/programs/add-two-integers)
3. [Check Even or Odd Number](/programs/check-even-or-odd)
4. [Add Two Binary](/programs/java-program-to-add-two-binary-numbers)
5. [Add Two Complex Numbers](/programs/java-program-to-add-two-complex-numbers)
6. [Multiply Two Numbers](/programs/multiply-two-numbers)
7. [Check Leap Year](/programs/java-program-to-check-Leap-year)
8. [Calculate Simple interest](/programs/calculate-simple-interest)
9. [Check Divisibility](/programs/java-program-to-check-divisbility)
10. [Calculate Quotient and Reminder](/programs/find-quotient-and-reminder)
11. [Calculate Power of a Number](/programs/calculate-power-of-a-number)
12. [Calculate Compound Interest](/programs/calculate-compound-interest)
13. [Calculate Factorial of a Number](/programs/factorial-in-java)
49 changes: 49 additions & 0 deletions content/programs/factorial-in-java.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Java Program to Calculate Factorial of a number
shotTitle: Calculate Simple Factorial
description: In this program, you'll learn to calculate the factorial and display final value on screen/terminal
---

To understand this example, you should have the knowledge of the following Java programming topics:

- [Java Loops](/docs/for-loop)
- [Java Basic Input and Output](/docs/basic-input-output)

## Calculate Factorial

A java program that calculate the factorial of a number by getting user input of the number.

```java
import java.util.Scanner;
class FactorialExample{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int i,fact=1;
int number = in.nextInt();
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
```

#### Output

```plaintext
Enter the number : 5
The factorial of 5 is : 120.
```

To Calculate Factorial ,
`Factorial(5) = (5*4*3*2*1)`



<Callout>

Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input)

Here two input numbers are taken from user one after another with space in between them which distinguish between two different inputs, this useful behavior is because of the default settings of Scanner called as Delimiter, [learn more here](https://www.javatpoint.com/post/java-scanner-delimiter-method).

</Callout>
Loading