4 Aug 2024
12 Feb 2024
JAVA PROGRAM - ADDITION OF TWO MATRICES
## Write a Java Program to ADD TWO MATRICES
public class MatrixAddition {
public static void main(String[]
args) {
int[][] firstMatrix = { {1, 2, 3},
{4, 5, 6}, {7, 8, 9} };
int[][] secondMatrix = { {9, 8, 7},
{6, 5, 4}, {3, 2, 1} };
// Creating a matrix to store the
result
int[][] result = new
int[firstMatrix.length][firstMatrix[0].length];
// Adding corresponding elements of
two matrices
for (int i = 0; i <
firstMatrix.length; i++) {
for (int j = 0; j <
firstMatrix[i].length; j++) {
result[i][j] = firstMatrix[i][j] +
secondMatrix[i][j];
}
}
// Displaying the result matrix
System.out.println("Result of Matrix
Addition:");
for (int i = 0; i < result.length;
i++) {
for (int j = 0; j <
result[i].length; j++) {
System.out.print(result[i][j] + "
");
}
System.out.println();
}
}
}
|
NODE JS PROGRAM - PRINT NUMBERS FROM 1 TO 10
## Write a Node Js program to print numbers from 1 to 10
// Define the upper limit for the loop const limit = 10; // Print numbers from 1 to the limit using a for-loop console.log("Printing numbers using a for loop:"); for (let i = 1; i <= limit; i++) { console.log(i); } |
11 Feb 2024
NODE JS PROGRAM - FACTORIAL OF A NUMBER
## Write a Node Js program to print the Factorial of a number
function factorial(n) {
if (n === 0 || n === 1) {
return
1;
}
else {
return n *
factorial(n - 1);
}
}
const number = 10;
// Change this to calculate the factorial of a different number
const result = factorial(number); // Function
calling
console.log(`Factorial of ${number} is: ${result}`);
|
NODE JS PROGRAM - PRINT EACH NUMBER OF AN ARRAY USING LOOP
## Write a Node Js program to iterate over an array and print each number
const arrNumber = [1,2,3,4,5];
console.log ( "Printing numbers using a FOR Loop: ");
for (let i = 0 ; i < arrNumber.length ; i++) { console.log (arrNumber[i]);
}
|
JAVA TUTORIAL PART 3 - DATA TYPES AND COMMENTS
Data Types in Java
1) Primitive Data Types
- These are the most basic data types in Java. They represent single values and are not objects.
There are different primitive data types -
There are different primitive data types -
a) Integer Types-
- byte - 8-bit signed integer. Range varies from -128 to 127
- short - 16-bit signed integer. Range varies from -32,768 to 32,767
- int - 32-bit signed integer. Range varies from -2^31 to 2^31-1
Example - int num = 10;
- long- 64-bit signed integer. Range varies from -2^63 to 2^63-1
b) Floating-Point types:
- float - 32-bit floating-point number. It should be suffixed with 'f' or 'F'
- double - 64-bit floating-point number.
Example - double pi = 3.14;
c) Other Primitive types:
- char - 16-bit Unicode character.
Example - char letter = 'H';
- boolean - Represents the two values - true and false
Example - boolean flag = true;
2) Reference Data Types
- These are used to refer to objects. They do not store the actual data but store the reference (Memory Address) of the object.
There are different reference data types -
a) Class types-
- String - Represent a sequence of characters.
Example - String text = "Hello";
b) Array types-
- Arrays - Ordered collection of elements of the same type.
c) Interface types-
- Interfaces - Defines a set of methods that a class must implement.
Comments in Java
Single-Line comments start with //
Multi-Line comments starts with /* and ends with */
Example-
// This is a single-line comment /* * This is a multi-line comment * This is the second multi-line comment. */ |
10 Feb 2024
JAVA TUTORIAL PART 2 - JDK (Java Development Kit) INSTALLATION
1) Download the JDK
1) Visit the official Oracle website or OpenJDK website to download the JDK installer for your operating system.
2) Make sure to download the version that matches your operating system (Windows, macOS, or Linux)
2) Run the Installer
1) Once the JDK is installed, choose the installation directory for the JDK.
2) Set the Environment Variables-
- JAVA_HOME (example - C:\Program Files\ Java\jdk-16)
Add the JDK's bin Folder to the PATH environment variable.
- PATH (example - %JAVA_HOME%\bin)
3) Verify the Installation
Open the terminal or Command Prompt to check the Java version.
Example - java -version
Click Here to Learn About Java Data Types
JAVA PROGRAM - RIGHT TRIANGLE STAR PATTERN
## Write a Java Program to print the Right Triangle Star Pattern
*
* *
* * *
* * * *
* * * * *
public class RightTriangleStarPattern { public static void main(String[] args) { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("* "); } System.out.println(); } } }
|
Subscribe to:
Posts (Atom)