Shriansh Kumar

Shriansh Kumar

211 Articles Published

Articles by Shriansh Kumar

Page 8 of 22

JavaScript program to retrieve clients IP address

Shriansh Kumar
Shriansh Kumar
Updated on 08-Oct-2024 1K+ Views

Internet Protocol address, in short, IP address, is a unique address represented by a string of numbers that identifies a device on the internet or a local network. In JavaScript, we can retrieve a client's IP addresses using a 3rd parties API services such as ipify or ipapi. We can then further use this information to track user locations, personalize content based on the location, or implement security measures. Using "ipify" API To obtain the client's IP address, we will use a third−party API service (https://www.ipify.org/). We'll send a GET call to the "ipify" API using the $.getJSON() function to ...

Read More

JavaScript how to get an alert to appear when I click on a button in a class?

Shriansh Kumar
Shriansh Kumar
Updated on 04-Oct-2024 2K+ Views

An alert in JavaScript is a dialog box that is displayed to the user when some important information or warnings needs to be notified. It may contain a message along with OK button. When you click on the OK button, it will dismiss the dialog. Clicking a button triggers an event handler which invoke a function that instructs the browser to display a dialog with a message. In this article, we will explore a few ways to get an alert to appear when user click on a button. For example, click on the button given below to get an ...

Read More

Java Program to display upper triangular matrix

Shriansh Kumar
Shriansh Kumar
Updated on 30-Sep-2024 1K+ Views

In this article, we will understand how to display an upper triangular matrix of a given matrix in Java. A matrix has row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix. And, the term upper triangular matrix refers to a matrix whose all elements below the main diagonal are 0. Example Scenario: Input: matrix = { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } Output: upptri_mat = 2 1 4 0 2 3 0 0 2 Using ...

Read More

JavaScript: How to check if a number is even without using the modulo operator?

Shriansh Kumar
Shriansh Kumar
Updated on 30-Sep-2024 1K+ Views

For a given integer number, write a program in JavaScript to check whether a number is odd or even and return the same to the user. It’s pretty easy to check whether a number is even by using the modulo operator. But, in this article, we will be checking whether a number is even or not without using the modulo operator. Using the for loop In this approach, we are going to use the for loop to check whether a number is even or not. The idea is to take a Boolean flag variable as true and check it up ...

Read More

Java program to check order of characters in string

Shriansh Kumar
Shriansh Kumar
Updated on 30-Sep-2024 2K+ Views

You are given a String, your task is to write a program in Java that checks the order of its characters. If the order is previously defined, you have to check if the characters are in the given order otherwise check for alphabetical order. Let's understand the problem statement with an example − Example Scenario: Input: str = "abcmnqxz"; Output: res = TRUE The given string is in alphabetical order. Using Iteration In this approach, use a for loop to iterate over the string and check if the value of character at the current place and the previous ...

Read More

JavaScript function to take a number n and generate an array with first n prime numbers

Shriansh Kumar
Shriansh Kumar
Updated on 30-Sep-2024 2K+ Views

We are required to write a JavaScript function that takes in a number n and returns an array that contains first n prime numbers. We know that prime numbers are those numbers that are only divisible by 1 and themselves like 2, 3, 19, 37, 73 etc. Let's understand the problem with an example − Input: n = 6; Output: prime_numbers = [ 2, 3, 5, 7, 11, 13 ] Using Iteration We will first write a function that checks whether a given number is prime or not and then run a loop till the given number n ...

Read More

Java program to check if a Substring is present in a given String

Shriansh Kumar
Shriansh Kumar
Updated on 25-Sep-2024 1K+ Views

Suppose you are given two strings, your task is to write a Java program that checks whether the second string is the sub-string of the first one. String in Java is an immutable sequence of characters and sub-string is its small portion. Example Scenario: − Input 1: str = "The sunset is beautiful"; Input 2: sub_str = "sunset"; Output: res = found!! Using Iteration In this approach, the idea is to use a nested for loop and an if block. The outer for loop will iterate over the characters of the main string. For each starting position i, ...

Read More

Java program to reverse each word in a sentence

Shriansh Kumar
Shriansh Kumar
Updated on 25-Sep-2024 4K+ Views

Given a sentence or string, write a Java program to reverse each word in that sentence. Each word in that sentence should be reversed and at the same time, they should be in the same order as before. Let's understand the problem with an example − Example Scenario: Input: orgnl_sen = an apple is red Output: modfd_sen = na elppa si der Using Iteration First, split the given sentence into an array of words using the space character as the delimiter. Use a for-each loop to iterate over each word. Then, iterate through the characters of each ...

Read More

Java Program to Sort 2D Array Across Left Diagonal

Shriansh Kumar
Shriansh Kumar
Updated on 18-Sep-2024 959 Views

For a given 2D array, our task is to write a Java program to sort its element present across the left diagonal. For this problem, we need to arrange the elements of the array in such a way that the values are sorted when read from the top left to the bottom right, which is the left diagonal of the array. Example Scenario: Input: matrix = {3, 8, 2}, {6, 5, 4}, {3, 2, 1}; Output: new_matrix = {1, 4, 2}, {2, 3, 8}, {3, 6, 5} Use the below sorting tool to understand the problem: Sorting Tool ...

Read More

Java program to check whether a number is positive or negative

Shriansh Kumar
Shriansh Kumar
Updated on 13-Sep-2024 1K+ Views

In this article, we will learn to check whether the number is positive or negative in Java. To check whether the specified number is positive or negative can be determined with respect to 0. A number greater than 0 is considered a positive number whereas a number less than 0 is considered a negative number, we can use Java conditional statements like if-else-if blocks or ternary operators. Before that let's discuss the problem statement with the help of examples − Problem Statement Given an integer number as an input, write a Java program to check whether the number is positive ...

Read More
Showing 71–80 of 211 articles
« Prev 1 6 7 8 9 10 22 Next »
Advertisements