Found 26504 Articles for Server Side Programming

Java program to calculate sum of squares of first n natural numbers

AmitDiwan
Updated on 11-Sep-2024 12:25:33

957 Views

In this article, you will learn to write a program in Java to calculate the sum of squares of first n natural numbers. This program efficiently computes the sum using a mathematical formula − (val * (val + 1) / 2) * (2 * val + 1) / 3 Problem Statement Write a program in Java to calculate the sum of squares of first n natural numbers, where n is a user-defined value. Input val = 8 Output The sum of squares of first 8 natural numbers is204 Steps to calculate sum of squares of first n natural numbers Following are ... Read More

Java Program for Smallest K digit number divisible by X

AmitDiwan
Updated on 08-Jul-2020 11:23:11

214 Views

To find the smallest K digit number divisible by X, the Java code is as follows −Example Live Demoimport java.io.*; import java.lang.*; public class Demo{    public static double smallest_k(double x_val, double k_val){       double val = 10;       double MIN = Math.pow(val, k_val - 1);       if (MIN % x_val == 0)       return (MIN);       else       return ((MIN + x_val) - ((MIN + x_val) % x_val));    }    public static void main(String[] args){       double x_val = 76;       double k_val ... Read More

Java program to get number of elements with odd factors in given range

AmitDiwan
Updated on 18-Nov-2024 22:29:39

289 Views

In this article, we will learn how to count the number of elements with odd factors (i.e., perfect squares) in a given range using Java. Perfect squares have odd divisors, and we can calculate them by counting the perfect squares in the specified range. Problem StatementGiven a range defined by a lower and upper bound, write a Java program to calculate how many numbers in this range have odd factors (i.e., perfect squares).Input Lower Range: 55Upper Range: 1000Output The number of elements with odd factors between 55 and 1000 is: 24 Steps to calculate the number of elements with ... Read More

Java program for nth multiple of a number in Fibonacci Series

AmitDiwan
Updated on 05-Nov-2024 22:02:55

324 Views

In this article, we will learn how to find the nth multiple of a given number in the Fibonacci series using Java. The Fibonacci series is a sequence where each number is the sum of the two preceding ones. We will use loops, conditional statements, and the modulus operator to find the position of the nth multiple of a specific number in the series.  Problem StatementGiven a number and its multiple positions, find the position of the nth multiple of a number in the Fibonacci series. Input A number whose multiple is to be found: 9The nth ... Read More

Replacing ‘public’ with ‘private’ in “main” in Java

AmitDiwan
Updated on 08-Jul-2020 10:33:09

397 Views

When ‘public’ is used in ‘main’ −Example Live Demopublic class Demo{    public static void main(String args[]){       System.out.println("This is a sample only");    } }OutputThis is a sample onlyA class named Demo contains the main function that is public. It has a print function, which successfully compiles, executes and prints the message on the console.When ‘public’ is replaced with ‘private’Example Live Demopublic class Demo{    private static void main(String args[]){       System.out.println("This is a sample only");    } }OutputError: Main method not found in class Demo, please define the main method as: public static void main(String[] args) ... Read More

Replace null values with default value in Java Map

AmitDiwan
Updated on 08-Jul-2020 10:29:55

1K+ Views

To replace null values with default value in Java Map, the code is as follows −Example Live Demoimport java.util.*; import java.util.stream.*; public class Demo{    public static Map null_vals(Map my_map, T def_val){       my_map = my_map.entrySet().stream().map(entry -> {          if (entry.getValue() == null)          entry.setValue(def_val);          return entry;       })       .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));       return my_map;    }    public static void main(String[] args){       Map my_map = new HashMap();       my_map.put(1, null);       my_map.put(2, 56);   ... Read More

Static and non static blank final variables in Java

AmitDiwan
Updated on 08-Jul-2020 10:28:41

1K+ Views

Static variable: Declared with the help of the keyword ‘static’, they are also known as class variables. They are defined within a constructor or outside a class function. When a variable is static, it is shared between all objects of the class, irrespective of the number of objects that are created.Demonstrating how the ‘static’ keyword, when used with variable works −Example Live Demopublic class Demo{    String name;    static String designation;    public void display_data(){       System.out.println("The name is: " + name);       System.out.println("The designation of this team members is : " + designation);    } ... Read More

Java program to find IP Address of the client

AmitDiwan
Updated on 08-Jul-2020 10:25:22

2K+ Views

To find the IP Address of the client, the Java code is as follows −Example Live Demoimport java.net.*; import java.io.*; import java.util.*; import java.net.InetAddress; public class Demo{    public static void main(String args[]) throws Exception{       InetAddress my_localhost = InetAddress.getLocalHost();       System.out.println("The IP Address of client is : " + (my_localhost.getHostAddress()).trim());       String my_system_address = "";       try{          URL my_url = new URL("http://bot.whatismyipaddress.com");          BufferedReader my_br = new BufferedReader(new          InputStreamReader(my_url.openStream()));          my_system_address = my_br.readLine().trim();       }   ... Read More

Java program to check order of characters in string

Shriansh Kumar
Updated on 30-Sep-2024 16:03:43

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

Java code to print common characters of two strings in alphabetical order

AmitDiwan
Updated on 15-Oct-2024 11:52:18

2K+ Views

In this article, we will learn to print common characters of two strings in alphabetical order using Java. The program uses arrays to count how often each letter appears in both strings and then compares these counts to identify the common characters. Problem Statement Write a Java program to print common characters of two strings in alphabetical order − Input my_str_1 = "itsasample" my_str_2 = "thisisasample" Output The common characters between the two strings in alphabetical order is :aaeilmpsst Steps to print common characters of two strings in alphabetical order Following are the steps to print common characters of two strings ... Read More

Advertisements