0% found this document useful (0 votes)
2 views3 pages

Java Essential Programs

The document contains Java programs that cover essential algorithms and data structures, including calculating factorials, checking palindromes, anagrams, GCD, Fibonacci series, prime numbers, reversing numbers, summing digits, checking Armstrong numbers, and finding the last non-zero digit of a factorial. Each program is implemented in a simple and straightforward manner, utilizing basic Java constructs such as loops and conditionals. These examples serve as a practical guide for beginners to understand fundamental programming concepts in Java.

Uploaded by

Shreyas S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java Essential Programs

The document contains Java programs that cover essential algorithms and data structures, including calculating factorials, checking palindromes, anagrams, GCD, Fibonacci series, prime numbers, reversing numbers, summing digits, checking Armstrong numbers, and finding the last non-zero digit of a factorial. Each program is implemented in a simple and straightforward manner, utilizing basic Java constructs such as loops and conditionals. These examples serve as a practical guide for beginners to understand fundamental programming concepts in Java.

Uploaded by

Shreyas S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Essential Programs (Easy to Moderate)

1. Factorial of a number
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int fact = 1;
for (int i = 1; i <= n; i++) fact *= i;
[Link](fact);
}
}
2. Palindrome check (string)
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String s = [Link]();
String rev = new StringBuilder(s).reverse().toString();
if ([Link](rev)) [Link]("Palindrome");
else [Link]("Not Palindrome");
}
}
3. Anagram check
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String s1 = [Link]();
String s2 = [Link]();
char[] a = [Link](), b = [Link]();
[Link](a); [Link](b);
if ([Link](a, b)) [Link]("Anagram");
else [Link]("Not Anagram");
}
}
4. GCD (HCF) of two numbers
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link](), b = [Link]();
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
[Link](a);
}
}
5. Fibonacci series
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int a = 0, b = 1;
[Link](a + " " + b + " ");
for (int i = 2; i < n; i++) {
int c = a + b;
[Link](c + " ");
a = b;
b = c;
}
}
}
6. Prime number check
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
boolean prime = n > 1;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) prime = false;
[Link](prime ? "Prime" : "Not Prime");
}
}
7. Reverse a number
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), rev = 0;
while (n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
[Link](rev);
}
}
8. Sum of digits of a number
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
[Link](sum);
}
}
9. Check Armstrong number
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link](), temp = n, sum = 0;
int digits = [Link](n).length();
while (n > 0) {
int r = n % 10;
sum += [Link](r, digits);
n /= 10;
}
[Link](sum == temp ? "Armstrong" : "Not Armstrong");
}
}
10. Factorial last non-zero digit
import [Link].*;
public class Main {
static int lastNonZeroDigit(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
while (fact % 10 == 0) fact /= 10;
fact %= 100000;
}
return fact % 10;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
[Link](lastNonZeroDigit(n));
}
}

You might also like