Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 2 of 450
Difference between sum of the squares of and square of sum first n natural numbers.
Problem StatementWith a given number n, write a program to find the difference between sum of the squares of and square of sum first n natural numbers.Examplen = 3 Squares of first three numbers = 3x3 + 2x2 + 1x1 = 9 + 4 + 1 = 14 Squares of sum of first three numbers = (3 + 2 + 1)x(3 + 2 + 1) = 6x6 = 36 Difference = 36 - 14 = 22Examplepublic class JavaTester { public static int difference(int n){ //sum of squares of n natural numbers ...
Read MoreDifference between sums of odd and even digits.
Problem StatementWith a given long integer n, write a program to find the difference between sum of the odd digits and even digits to be 0 or not. Index starts from 0.Examplen = 1212112 Sum of odd position elements = 2 + 2 + 1 = 5 Sum of even position elements = 1 + 1 + 1 + 2 = 5 Difference = 5 - 5 = 0 Output = YesExampleclass JavaTester { public static int difference(int n){ return (n % 11); } public static void main(String args[]){ int n = 1212112; System.out.println("Number: " + n); System.out.println(difference(n) == 0 ? "Yes" : "No"); n = 12121121; System.out.println("Number: " + n); System.out.println(difference(n) == 0 ? "Yes" : "No"); } }OutputNumber : 1212112 Output: Yes Number : 12121121 Output: No
Read MoreDifference between sums of odd level and even level nodes of a Binary Tree in Java
Problem StatementWith a given binary tree, write a program to find the difference between sum of nodes at odd level and even level. Assume root at level 1, left/right child of root at level 2 and so on.Example 5 / \ 2 6 / \ \ 1 4 8 / / \ 3 7 9 Sum of nodes at odd level = 5 + 1 + 4 + 8 = 18 Sum of ...
Read MoreDifference between the largest and the smallest primes in an array in Java
Problem StatementWith a given array of integers where all elements are less than 1000000. Find the difference between the largest and the smallest primes in an array.ExampleArray: [ 1, 2, 3, 4, 5 ] Largest Prime Number = 5 Smallest Prime Number = 2 Difference = 5 - 3 = 2.SolutionUse Sieve of Eratosthenes approach, which is an efficient way to find out all prime numbers smaller than a given number. Then we will figure out the largest and smallest prime number to get the required difference.Examplepublic class JavaTester { static int MAX = 1000000; ...
Read MoreDifference between Traits and Abstract Classes in Scala.
TraitsTraits are similar to interfaces in Java and are created using trait keyword.Abstract ClassAbstract Class is similar to abstract classes in Java and are created using abstract keyword.Exampletrait SampleTrait { // Abstract method def test // Non-Abstract method def tutorials() { println("Traits tutorials") } } abstract class SampleAbstractClass { // Abstract method def test // Non-abstract meythod def tutorials() { println("Abstract Class tutorial") } } class Tester extends SampleAbstractClass { def test() { println("Welcome to Tutorialspoint") ...
Read MoreIncremental Java infinite loop
ExampleFollowing is the required program −public class Tester { public static void main(String args[]) { int i = 0; do { i++; System.out.println(i); }while (true); } }The output will keep printing numbers in sequential order.
Read MoreIncremental Java infinite loop
ExampleFollowing is the required program −public class Tester { public static void main(String args[]) { int i = 0; do { i++; System.out.println(i); }while (true); } }The output will keep printing numbers in sequential order.
Read MoreHow is down-casting possible in Java?
Yes, a variable can be downcast to its lower range substitute by casting. It may lead to data loss although. See the example below −Examplepublic class Tester { public static void main(String[] args) { int a = 300; byte b = (byte)a; System.out.println(b); } }OutputIt will print output as −44
Read MoreHow is down-casting possible in Java?
Yes, a variable can be downcast to its lower range substitute by casting. It may lead to data loss although. See the example below −Examplepublic class Tester { public static void main(String[] args) { int a = 300; byte b = (byte)a; System.out.println(b); } }OutputIt will print output as −44
Read MoreHow to import java.lang.String class in Java?
To import any package in the current class you need to use the import keyword asimport packagename;Exampleimport java.lang.String; public class Sample { public static void main(String args[]) { String s = new String("Hello"); System.out.println(s); } }OutputHello
Read More