These all number related programs is important for interview . All Question are important.
1). find sum of first 10 natural numbers in java 8 stream | Java Program to Find Sum of Natural Numbers | How do you find the sum of 10 numbers in Java? | How do you find the sum of the first 10 natural numbers?
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Naturel_Number_Example{
public static void main(String[] args) {
// find sum of first 10 natural numbers
int sumofNatualNumbers = IntStream.range(1, 11).sum();
System.out.println(“Sum of first 10 Natural numbers: “ + sumofNatualNumbers);
}
}
2). Program to print first 10 even numbers | How to print 10 even numbers in Java? | Java Program to Print First 10 Even Natural Numbers
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Naturel_Number_Example{
public static void main(String[] args) {
IntStream.rangeClosed(1, 10).map(i -> i * 2).forEach(System.out::println)
}
}
3). How to write a Java program to print Fibonacci series? | How do you write a program for Fibonacci series? | How to generate Fibonacci numbers using Stream | Java Fibonacci examples .
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Naturel_Number_Example{
public static void main(String[] args) {
Stream.iterate(new int[] { 0, 1 }, f -> new int[] { f[1], f[0] + f[1] }).limit(10).map(f -> f[0])
.forEach(i -> System.out.print(i + ” “));
}
}
4). palindrome number in java 8 | Palindrome Program In Java | How to check if a String is a Palindrome using Java 8 | Java 8 Streams – Checking Whether String is Palindrome | Java Programs to Check Palindrome
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Naturel_Number_Example{
public static void main(String[] args) {
String str = “ROTATOR”;
boolean isItPalindrome = IntStream.range(0, str.length() / 2)
.noneMatch(i -> str.charAt(i) != str.charAt(str.length() – i – 1));
if (isItPalindrome) {
System.out.println(str + ” is a palindrome”);
} else {
System.out.println(str + ” is not a palindrome”);
}
}
}
5). How to find prime number in java 8 ? | Prime number in java 8 | get prime numbers from List using java stream | get prime numbers from List using java stream | How to check prime number in java8? | How do you find the prime of a number in Java?
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Naturel_Number_Example{
public static void main(String[] args) {
int number = 17; // Replace with the number you want to check
boolean isPrime = isNumberPrime(number);
if (isPrime) {
System.out.println(number + ” is a prime number.”);
} else {
System.out.println(number + ” is not a prime number.”);
}
}
public static boolean isNumberPrime(int number) {
return number > 1 && IntStream.range(2, number).noneMatch(i -> number % i == 0);
}
}