26 Dec 2024
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
There are different primitive data types -
2) Reference Data Types
Comments in Java
// 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
2) Run the Installer
3) Verify the Installation
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(); } } }
|
JAVA PROGRAM - INVERTED PYRAMID STAR PATTERN
## Write a Java Program to Print an INVERTED PYRAMID STAR PATTERN
* * * * * * * * *
* * * * *
* * *
*
public class InvertedPyramidStarPattern { public static void main(String[] args) { int rows = 5; for (int i = rows; i >= 1; i--) { for (int j = 1; j <= rows - i; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print("* "); } System.out.println(); } } }
|
JAVA PROGRAM - PYRAMID STAR PATTERN
## Write a Java program to print the PYRAMID STAR PATTERN?
JAVA PROGRAM - LEAP YEAR OR NOT
## Write a Java Program to check if the year is LEAP YEAR or NOT
public class CheckLeapYear { public static void main(String[] args) { int year = 2024; if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { System.out.println("The year " + year + " is a leap year."); } else { System.out.println("The year " year + " is not a leap year."); } } } |
JAVA PROGRAM - LARGEST VALUE AMONG THREE NUMBERS
## Write a Java program to find the largest value among three variables
public class LargestAmongThreeNumbers { public static void main(String[] args) { int num1 = 20; int num2 = 50; int num3 = 55; int largest = num1; if (num2 > largest) { largest = num2; } if (num3 > largest) { largest = num3; } System.out.println("The largest number among " + num1 + ", " + num2 + ", and " + num3 + " is: " + largest); } } |
JAVA PROGRAM - LARGEST VALUE AMONG TWO NUMBERS
## Write a Java Program to find the largest value among two numbers
public class LargestOfTwoNumbers { public static void main(String[] args) { int num1 = 10; int num2 = 20; if (num1 > num2) { System.out.println("The largest number is: " + num1); } else { System.out.println("The largest number is: " + num2); } } } |
8 Feb 2024
JAVA TUTORIAL PART 1 - INTRODUCTION TO JAVA
1) WHAT IS JAVA?
2) Key Features of Java
3) Common Applications of Java
1) Web Development
4) Download Java
JAVA - SUBTRACT TWO NUMBERS
Write a JAVA program to subtract two numbers?
public class SubtractTwoNumbers { public static void main(String[] args) { int num1 = 10; int num2 = 5; int difference = num1 - num2; System.out.println("Difference of " + num1 + " and " + num2 + " is: " + difference); } } |
7 Feb 2024
JAVA - ADD TWO NUMBERS
## Write a Java program to add two numbers
public class AddTwoNumbers { public static void main(String[] args) { int num1 = 5; int num2 = 10; int sum = num1 + num2; System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum); } } |
NODE JS - ASYNCHRONOUS PROGRAMMING
1) What is Asynchronous Programming?
2) Benefits of Asynchronous Programming?
3) Callbacks
const fs = require('fs'); fs.readFile('example.txt', 'utf8', (err, data) => { if (err) { console.error(err); return; } console.log(data); }); |
4) Promises
They allow chaining of asynchronous operations and handling of errors more elegantly.
const fs = require('fs').promises; fs.readFile('example.txt', 'utf8'). then((data) => { console.log(data); }) .catch((err) => { console.error(err); }); |
5) Async/ Await
const fs = require('fs').promises; async function testingAsync() { try { const data = await fs.readFile('example.txt', 'utf8'); console.log(data); } catch (err) { console.error(err); } } testingAsync(); |
6 Feb 2024
NODE JS - NPM
1) What is NPM?
- With npm, we can easily install, manage, and update packages (Libraries/ Modules) that you can use in your applications.
npm i express
NODE JS BUILT-IN MODULE - FS
1) What does FS module do?
2) Reading from a file
fs.readFile('example.txt', 'utf8', (err, data) => { if (err) { console.error(err); return; } console.log(data); }); |
3) Creating a new file
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) { if (err) throw err; console.log('Saved!'); }); |
fs.open('mynewfile2.txt', 'w', function (err, file) { if (err) throw err; console.log('Saved!'); }); |
fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) { if (err) throw err; console.log('Saved!'); }); |
4) Deleting a File
fs.unlink('myfile.txt', function (err) { if (err) throw err; console.log('File deleted!'); }); |
NODE JS BUILT IN MODULE - URL
1) What does the URL Module do?
2) URL Parse() method
Example:
const url = require('url'); const urlString = 'https://example.com:8080/path?query=value'; const parsedUrl = url.parse(urlString, true); // Pass true as the second argument to parse query parameters console.log(parsedUrl); |
3) URL Resolve() method
Example:
const url = require('url'); const baseUrl = 'https://example.com/users'; const relativePath = '/profile'; const resolvedUrl = url.resolve(baseUrl, relativePath); console.log(resolvedUrl); // returns https://example.com/profile |
5 Feb 2024
NODE JS BUILT IN MODULE - HTTP
1) Creating an HTTP Server
var http = require ('http');
http.createServer(function (req, res){
// Import the built-in 'http' module const http = require('http'); // Define the hostname and port number const hostname = '127.0.0.1'; const port = 3000; // Create an HTTP server const server = http.createServer((req, res) => { // Set the HTTP status code and content type in the response header res.writeHead(200, {'Content-Type': 'text/plain'}); // Send the response body res.end('Hello, world!\n'); }); // Start the server and listen on the specified port and hostname server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); }); |
2) GET REQUEST to query
// Import the built-in 'http' module const http = require('http'); // Define the options for the HTTP request const options = { hostname: 'jsonplaceholder.typicode.com', port: 80, path: '/posts', method: 'GET' }; // Create the HTTP request const req = http.request(options, (res) => { // Initialize a variable to store the response data let data = ''; // Concatenate chunks of data as they are received res.on('data', (chunk) => { data += chunk; }); // When the response is complete, log the data to the console res.on('end', () => { console.log(data); }); }); // Handle errors that occur during the request req.on('error', (error) => { console.error('An error occurred:', error); }); // End the request req.end(); |
MODULES IN NODE JS
1) Modules in Node Js
1.1) What are User-Defined Modules?
The same modules can be imported and used in other parts of the application using the "require()" function.
function greetWithPersonName(name) { console.log ( `Hello ${name}! `); //Here we have used the tilde operator } module.exports = greetWithPersonName; |
1.2) What are Predefined Modules/ Built-in Modules/ Core Modules?
const fs = require ('fs'); fs.readFile ('text.txt, 'utf8', (err, data) => { if (err) { console.error (err); return; } console.log (data); }); |
NODE JS INTRODUCTION
1) What is Node Js?
1) What is Node Js?
- Node Js is a runtime environment that allows you to run JavaScript code on the server side.
2) Why to use Node Js?
2) Why to use Node Js?
3) How to download Node Js?
3) How to download Node Js?
4) Writing the first Node Js program.
4) Writing the first Node Js program.
// Import the built-in 'http' module
const http = require('http'); // Create an HTTP server that listens on port 5000 const server = http.createServer((req, res) => { // Set the response HTTP header with HTTP status and content type res.writeHead(200, {'Content-Type': 'text/plain'}); // Write the response body ("Hello, world!") res.end('Hello World!!'); }); // Start the server and listen on port 5000 server.listen(5000, () => { console.log('Server is now running at http://localhost:5000/'); }); |
Click Here to Learn Modules in Node Js
21 Jan 2024
C program that uses a loop to calculate the factorial of a given number
// Factorial program using function
#include <stdio.h>
// Function prototype
long long calculateFactorial(int n);
int main() {
int number;
// Get input from the user
printf("Enter a non-negative integer: ");
scanf("%d", &number);
// Check if the input is non-negative
if (number < 0) {
printf("Please enter a non-negative integer.\n");
return 1; // Return with an error code
}
// Calculate and display the factorial using a loop
long long factorial = calculateFactorial(number);
printf("Factorial of %d = %lld\n", number, factorial);
return 0;
}
// Function to calculate factorial using a loop
long long calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
long long result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
}
14 Jan 2024
C program example that uses functions - This program calculates the sum of two numbers using separate functions for input, addition, and output.
#include <stdio.h>
// Function prototypes
int getInput();
int addNumbers(int num1, int num2);
void displayResult(int result);
int main() {
// Get input from the user
int number1 = getInput();
int number2 = getInput();
// Perform addition
int sum = addNumbers(number1, number2);
// Display the result
displayResult(sum);
return 0;
}
// Function to get input from the user
int getInput() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return num;
}
// Function to add two numbers
int addNumbers(int num1, int num2) {
return num1 + num2;
}
// Function to display the result
void displayResult(int result) {
printf("The sum is: %d\n", result);
}
1 Jan 2024
Node js program - IF ELSE IF example
# Node js program using if else if statement
var a = 'route 1';
if (a === 'route 1'){
console.log ('Route is 1');
}else if ( a === 'route 2') {
console.log ('Route is 2');
}else {
console.log ( 'No Route');
}