1.
Find the Factorial of the given number
Program:
public class Factorial {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int factorial=1;
for(int i=1;i<=n;i++)
{
factorial= factorial*i;
}
System.out.println(factorial);
}
}
Output:
Enter the number
5
120
Factorial of a Number
public class Factorial {
public static void main(String[] args) {
int num = 5, factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println(factorial);
}
}
Find the Factorial of a Number using Recursion
public class FactorialRecursion {
public static void main(String[] args) {
int num = 5;
System.out.println(factorial(num));
}
static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
}
2. Find the reverse of the number
Program:
public class ReverseTheNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(j*10)+i;
a=a/10;
}
System.out.println("The reverse number is "+j);
}
}
Output:
Enter the number
12345
The reverse number is 54321
---Reverse a Number
public class ReverseNumber {
public static void main(String[] args) {
int num = 12345, reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
System.out.println(reversed);
}
}
3. Check whether the number is palindrome or not
Program:
public class Palindrome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(j*10)+i;
a=a/10;
}
if(n==j) {
System.out.println("It is panlidrome");
}
else {
System.out.println("It is not a panlindrome");
}
}
}
Output:
Enter the number
11
It is panlidrome
4. Check whether the number is amstrong or not
Program:
public class Amstrong{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=(i*i*i)+j;
a=a/10;
}
if(n==j) {
System.out.println("It is amstrong");
}
else {
System.out.println("It is not a amstrong");
}
}
}
Output:
Enter the number
153
It is amstrong
Check if a Number is Armstrong
public class ArmstrongNumber {
public static void main(String[] args) {
int num = 153, sum = 0, temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, 3);
temp /= 10;
}
System.out.println(num == sum);
}
}
Number is amstrong easy way
int n=153,a,i=0,j=0;
a=n;
while(a>0){
i=a%10;
j=(i*i*i)+j;
a=a/10;
}
if(n==j)
System.out.println("number is amstrong");
else{
System.out.println("number is not amstrong");
}
5. Print the amstrong number available between 0 to 1000
Program:
public class Amstrong{
public static void main(String[] args) {
for (int n = 1; n <= 1000; n++) {
int a, i = 0, j = 0;
a = n;
while (a > 0) {
i = a % 10;
j = j + (i * i * i);
a = a / 10;
}
if (n == j) {
System.out.println(n);
}
}
}
}
Output:
1
153
370
371
407
6. To print the palindrome available between 0 to 100
Program:
public class Palindrome {
public static void main(String[] args) {
for (int n = 1; n <= 100; n++) {
int a, i = 0, j = 0;
a = n;
while (a > 0) {
i = a % 10;
j = (j * 10) + i;
a = a / 10;
}
if (n == j) {
System.out.println(n);
}
}
}
Output:
1
2
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
7. Print the count of the given number
Program:
public class CountOfNumber{
public static void main(String[] args) {
int n,i=0;
System.out.println("Enter a number");
Scanner get=new Scanner(System.in);
n=get.nextInt();
while(n>0)
{
n=n/10;
i++;
}
System.out.println("No of digits present: "+i);
}
}
Output:
Enter a number
12345
No of digits present: 5
Count the Number of Digits in a Number
public class CountDigits {
public static void main(String[] args) {
int num = 12345;
int count = String.valueOf(num).length();
System.out.println(count);
}
}
8. Find the Sum of the digit
Program:
public class SumOfDigits{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int a,i=0,j=0;
a=n;
while(a>0) {
i=a%10;
j=j+i;
a=a/10;
}
System.out.println("Sum of the digits "+j);
}
}
Output:
Enter the number
123
Sum of the digits 6
Find the Sum of Digits of a Number
public class SumOfDigits {
public static void main(String[] args) {
int num = 12345, sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println(sum);
}
}
9.Swap of two number using third variable
Program:
public class Swap{
public static void main(String[] args) {
int a, b, c;
Scanner sw = new Scanner(System.in);
System.out.println("The numbers are");
a = sw.nextInt();
b = sw.nextInt();
c = a;
a = b;
b = c;
System.out.println("Swapping numbers are");
System.out.println(a);
System.out.println(b);
}
}
Output:
The numbers are
12
24
Swapping numbers are
24
12
Swap Two Numbers
public class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a: " + a + ", b: " + b);
}
}
10.Swap of two variable without using third variable
Program:
public class SwapTwoNumber{
public static void main(String[] args) {
int a, b;
Scanner sw = new Scanner(System.in);
System.out.println("The numbers are");
a = sw.nextInt();
b = sw.nextInt();
a = a + b;
b = a - b;
a = a - b;
System.out.println("Swapping numbers are");
System.out.println(a);
System.out.println(b);
}
}
Output:
The numbers are
12
24
Swapping numbers are
24
12
11. To find even/odd number:
Program:
public class EvenOrOdd{
public static void main(String[] args) {
Scanner e = new Scanner(System.in);
System.out.println("Enter a Number");
int n = e.nextInt();
if (n % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}
Output:
Enter a Number
23
Odd
12. Count of even and odd count
Program:
public class OddEvenCount{
public static void main(String[] args) {
int evencount = 0, oddCount=0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
evencount++;
}
else {
oddCount++;
}
}
System.out.println("Even count is "+evencount);
System.out.println("Odd count is "+oddCount); }
}
Output:
Even count is 50
Odd count is 50
13. Fibonacci series:
Program:
public class Fibonacci {
public static void main(String[] args) {
int a = 0, b = 1;
System.out.println(a);
System.out.println(b);
for (int i = 2; i <= 10; i++) {
int c = a + b;
System.out.println(c);
a = b;
b = c;
}
}
}
Output:
0
1
1
2
3
5
8
13
21
34
55
Similar-----------------------------------------------------------------------------------------------------
Fibonacci Series
public class Fibonacci {
public static void main(String[] args) {
int n = 10, num1 = 0, num2 = 1;
System.out.print("Fibonacci Series: " + num1 + ", " + num2);
for (int i = 2; i < n; i++) {
int num3 = num1 + num2;
System.out.print(", " + num3);
num1 = num2;
num2 = num3;
}
}
}
14. Print the value in Fibonacci series up to 100
Program:
public class Fibonacci{
public static void main(String[] args) {
int a = 0, b = 1;
System.out.println(a);
System.out.println(b);
for (int i = 1; i <= 10; i++) {
int c = a + b;
if(c<=100)
a = b;
b = c;
System.out.println(c);
}
}
}
Output:
0
1
1
2
3
5
8
13
21
34
55
89
15. Reverse the String
Program:
public class ReverseString{
public static void main(String args[]) {
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: " + reverse);
}
}
Output:
Enter a string to reverse
Nishathi
Reverse of entered string is: ihtahsin
Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Automation";
StringBuilder reversed = new StringBuilder(str).reverse();
System.out.println(reversed);
}
}
Using built in reverse() method of the StringBuilder class:
String input = "Welcome To Jave Learning";
StringBuilder input1 = new StringBuilder();
input1.append(input); // append a string into StringBuilder input1
input1.reverse();
System.out.println(input1);
// 3. Using StringBuffer:
String strText = "Java Learning";
// conversion from String object to StringBuffer
StringBuffer sbr = new StringBuffer(strText);
sbr.reverse();
System.out.println(sbr);
Reverse the entire sentence
Input = "India is country My"
Output = "My country is India"
public static void main(String[] args) {
String str[] = "India is country My".split(" ");
String ans = "";
for (int i = str.length - 1; i >= 0; i--) {
ans = ans + str[i] + " ";
}
System.out.println(ans.substring(0, ans.length() - 1));
}
16.To Check the String is palindrome or not.
Program:
public class Palindrome {
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
Output:
Enter a string to check if it is a palindrome
madam
Entered string is a palindrome.
Check for Palindrome
public class Palindrome {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(reversed));
}
}
17.Count of each Character in the String
Program:
public class CountOfCharacter {
public static void main(String args[]) {
{
String s = "vengatram";
HashMap<Character, Integer> emp = new HashMap<Character,
Integer>();
char[] ch = s.toCharArray();
for (char c : ch) {
if (emp.containsKey(c)) {
int x = emp.get(c);
emp.put(c, x + 1);
} else {
emp.put(c, 1);
}
}
System.out.println(emp);
}
}
}
Output:
{a=2, r=1, t=1, e=1, v=1, g=1, m=1, n=1}
18.Count of each Word
Program:
public class CountOfWord{
public static void main(String args[]) {
{
String s = "vengat ram";
String[] s1 = s.split(" ");
HashMap<String, Integer> emp = new HashMap<String,
Integer>();
for (String c : s1) {
if (emp.containsKey(c)) {
int x = emp.get(c);
emp.put(c, x + 1);
} else {
emp.put(c, 1);
}
}
System.out.println(emp);
}}
}
Output:
{vengat=1, ram=1}
19. Print the numbers in ascending order
Program:
public class AscendingOrder {
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the numbers:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
Enter no. of elements you want in array: 10
Enter all the numbers:
20
30
40
50
60
70
80
90
100
120
Ascending Order:20,30,40,50,60,70,80,90,100,120
20.Print the numbers in descending order
Program:
public class DescendingOrder{
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order:");
for (int i = n - 1; i > 0; i--) {
System.out.print(a[i] + ",");
}
System.out.print(a[0]);
}
}
Output:
Enter no. of elements you want in array: 5
Enter all the elements:
90
50
35
48
12
Descending Order:90,50,48,35,12
21.Print Triangle with Stars
Program:
public class Triangle{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print("* ");
}
for (int k = 1; k <= i; k++) {
System.out.print("* ");
}
System.out.println(" ");}}}
Output:
*
**
***
****
*****
22. Assume the string is he,xa,wa,re and give the output as
hexaware
Program:
public class Replace {
public static void main(String[] args) {
String s="he,xa,wa,re";
String x = s.replace(",", "");
System.out.println(x);
}
}
Output:
Hexaware
23.Find the special character, uppercase, lowercase, Number of
digits in the given string
Program:
public class CharCount{
public static void main(String[] args) {
String s = "Hi Welcome To Java Classes Tommorow At 2.00
p.m!!";
int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
count++;
} else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
count1++;
} else if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
count2++;
} else {
count3++;
}
}
System.out.println("total no of small letters: " + count);
System.out.println("total no of capital letters: " + count1);
System.out.println("total no of digits: " + count2);
System.out.println("total no of special characters: " + count3);
}
}
Output:
total no of small letters: 27
total no of capital letters: 7
total no of digits : 3
total no of special characters: 12
24. Print Reverse triangle without Space
Program:
public class ReverseTriangle{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
*****
****
***
**
*
25 . Check Whether the given number is prime or not
Program:
public class Prime{
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number");
n = input.nextInt();
int count = 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
count = 1;
}
}
if (count == 0) {
System.out.println("It is a prime number");
} else {
System.out.println("It is not a prime number");
}
}
}
Output:
Enter the number
17
It is a prime number
---Prime Number Check
public class PrimeCheck {
public static void main(String[] args) {
int num = 11;
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(isPrime);
}
}
----Print the Prime Numbers in a Range
public class PrimeInRange {
public static void main(String[] args) {
int start = 10, end = 50;
for (int num = start; num <= end; num++) {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime && num > 1) {
System.out.print(num + " ");
}
}
}
}
26. Print the prime numbers counts available between 1 to 100
Program:
public class PrimeNumberCount{
public static void main(String[] args) {
int count, c = 0;
for (int i = 1; i <= 100; i++) {
count = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 0) {
c++;
}
}
System.out.println(c);
}
}
Output:
26
27. Multiplication of the given number
Program:
public class Multiplication{
public static void main(String[] args) {
int n, j;
Scanner mt = new Scanner(System.in);
System.out.println("Enter the Table");
n = mt.nextInt();
System.out.println("Table upto");
j = mt.nextInt();
for (int i = 1; i <= j; i++) {
int c = n * i;
System.out.println(i + "*" + n + "=" + c);
}
}}
Output:
Enter the Table
7
Table upto
10
1*7=7
2*7=14
3*7=21
4*7=28
5*7=35
6*7=42
7*7=49
8*7=56
9*7=63
10*7=70
28. Biggest of 4 number
Program:
public class BiggestNumber{
public static void main(String[] args) {
int a, b, c, d;
Scanner bn = new Scanner(System.in);
System.out.println("The four numbers are");
a = bn.nextInt();
b = bn.nextInt();
c = bn.nextInt();
d = bn.nextInt();
if (a > b && a > c && a > d) {
System.out.println("The biggest number is= " + a);
} else if (b > a && b > c && b > d) {
System.out.println("The biggest number is= " + b);
} else if (c > a && c > b && c > d) {
System.out.println("The biggest number is= " + c);
} else {
System.out.println("The biggest number is= " + d);
}
}
}
Output:
The four numbers are
10
20
30
40
The biggest number is=40
29. Find the 3
rd
maximum Number in an given array
Program:
public class ThirdMax{
public static void main(String[] args) {
int a[]={-12,45,-23,64,-100,24};
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
int temp=0;
if(a[i]<a[j]){
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
for(int k=0;k<a.length;k++){
System.out.println(a[k]);
}
System.out.println("The Third maximum number is " +
a[a.length4]);
}
}
Output:
64
45
24
-12
-23
-100
The Third maximum number is 24
30. Separate reverse of each word in the string
Program:
public class Reverse{
public static void main(String[] args)
{
String name = "Greens Tech";
String [] s =name.split(" ");
String res = "";
for(int i=0;i<s.length;i++)
{
String t = s[i];
for(int j=t.length()-1;j>=0;j--)
{
char ch=t.charAt(j);
res=res+ch;
}
res=res+ " ";
}
System.out.println(res);
}
}
Output:
sneerG hceT
31. Number triangle
Program:
public class Welcome {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}
Output:
1
22
333
4444
55555
32. Find the duplicate count in an array
Program:
public class ArrayDuplicate {
public static void main(String[] args)
{
int n, count=0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array: ");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the numbers: ");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if(a[i]==a[j]) {
count++;
}
}}
System.out.println(count);
}
}
Output:
Enter no. of elements you want in array: 5
Enter all the numbers:
10
20
10
30
10
3
33.Find the duplicate count in the string
Program:
public class ListDuplicate {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("b");
list.add("c");
list.add("a");
list.add("a");
list.add("a");
System.out.println("Count all with frequency");
Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet) {
System.out.println(temp + ": " +
Collections.frequency(list, temp));
}
}
}
Output:
Count all with frequency
a: 4
b: 2
c: 2
d: 1
34.Count of the palindrome number
Program:
public class PalindromeCount{
public static void main(String[] args) {
int c = 0;
for (int n = 1; n <= 1000; n++) {
int a, i = 0, j = 0;
a = n;
while (a > 0) {
i = a % 10;
j = (j * 10) + i;
a = a / 10;
}
if (n == j) {
c++;
}
}
System.out.println(c);
}
}
Output:
106
35. Count of the amstrong number
Program:
public class AmstrongCount {
public static void main(String[] args) {
int c = 0;
for (int n = 1; n <= 1000; n++) {
int a, i = 0, j = 0;
a = n;
while (a > 0) {
i = a % 10;
j = j + (i * i * i);
a = a / 10;
}
if (n == j) {
c++;
}
}
System.out.println(c);
}
}
Output:
5
36.Construct the triangle pyramid
Program:
public class TrianglePyramid{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How Many Rows You Want In Your Pyramid?");
int noOfRows = sc.nextInt();
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
for (int i = noOfRows; i >= 1; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j where j value will be from i to noOfRows
for (int j = i; j <= noOfRows; j++)
{
System.out.print(j+" ");
}
for (int j = noOfRows-1; j >= i; j--)
{
System.out.print(j+" ");}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
Output:
How Many Rows You Want In Your Pyramid?
5
Here Is Your Pyramid
5
454
34543
2345432
123454321
37. Count of vowels and non vowels
Program:
public class VowelsCount {
public static void main(String[] args) {
String a = "welcome";
int vowels = 0;
int nonVowels = 0;
for (int i = 0; i < a.length(); i++) {
char ch = a.charAt(i);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i'
|| ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u'
|| ch == 'U') {
vowels++;
} else {
nonVowels++;
}
}
System.out.println("Count of vowels is "+vowels);
System.out.println("Count of Non Vowels is "+nonVowels);
}
}
Output:
Count of vowels is 3
Count of Non Vowels is 4
Count Vowels and Consonants
public class VowelConsonantCount {
public static void main(String[] args) {
String str = "Automation";
int vowels = 0, consonants = 0;
for (char c : str.toCharArray()) {
if ("aeiouAEIOU".indexOf(c) != -1) {
vowels++;
} else if (Character.isLetter(c)) {
consonants++;
}
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
}
}
37.Remove duplicates from stored array
Program:
public class RemoveDuplicate {
public static int[] removeDuplicates(int[] input){
int j = 0;
int i = 1;
//return if the array length is less than 2
if(input.length < 2){
return input;
}
while(i < input.length){
if(input[i] == input[j]){
i++;
}else{
input[++j] = input[i++];
}
}
int[] output = new int[j+1];
for(int k=0; k<output.length; k++){
output[k] = input[k];
}
return output;
}
public static void main(String a[]){
int[] input1 = {2,3,6,6,8,9,10,10,10,12,12};
int[] output = removeDuplicates(input1);
for(int i:output){
System.out.print(i+" ");
}
}
}
Output:
{2 3 6 8 9 10 12}
-----Remove Duplicates from an Array
import java.util.HashSet;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4};
HashSet<Integer> set = new HashSet<>();
for (int num : arr) {
set.add(num);
}
System.out.println(set);
}
}
38.Sum of the odd and even number
Program:
public class SumOfOdd{
public static void main(String[] args) {
int oddCount = 0,evenCount=0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
oddCount= oddCount + i;
}
else {
evenCount=evenCount+i;
}
}
System.out.println("Count of odd number is "+oddCount);
System.out.println("Count of even number is "+evenCount);
}
}
Output:
Count of odd number is 2500
Count of even number is 2550
39.Count of Uppercase, lowercase, digits, special character
Program:
public class Test {
public static void main(String[] args) {
int lCaseCount = 0, uCaseCount = 0, numbersCount = 0,
sCharCount = 0;
String s = "Welcome To JAVA Clas @ 12345";
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isLowerCase(ch)) {
lCaseCount++;
} else if (Character.isUpperCase(ch)) {
uCaseCount++;
} else if (Character.isDigit(ch)) {
numbersCount++;
} else {
sCharCount++;
}
}
System.out.println("Upper Case Count: " + uCaseCount);
System.out.println("Lower Case Count: " + lCaseCount);
System.out.println("Numbers Count: " + numbersCount);
System.out.println("Special Characters Count: " + sCharCount);
}
}
Output:
Upper Case Count: 7
Lower Case Count: 10
Numbers Count: 5
Special Characters Count: 6
40. Sort an Array
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 3};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
41. Merge Two Arrays
import java.util.Arrays;
public class MergeArrays {
public static void main(String[] args) {
int[] arr1 = {1, 3, 5};
int[] arr2 = {2, 4, 6};
int[] merged = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, merged, 0, arr1.length);
System.arraycopy(arr2, 0, merged, arr1.length, arr2.length);
System.out.println(Arrays.toString(merged));
}
}
42. Find the Largest Element in an Array
public class LargestInArray {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9};
int largest = arr[0];
for (int num : arr) {
if (num > largest) {
largest = num;
}
}
System.out.println(largest);
}
}
43. Calculate GCD of Two Numbers
public class GCD {
public static void main(String[] args) {
int a = 60, b = 48;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
System.out.println(a);
}
}
44. Check for Anagram
import java.util.Arrays;
public class AnagramCheck {
public static void main(String[] args) {
String str1 = "listen", str2 = "silent";
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
System.out.println(Arrays.equals(arr1, arr2));
}
}
45. Find the Second Largest Element in an Array
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {12, 35, 1, 10, 34, 1};
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
System.out.println(second);
}
}
46. Print the Pascal's Triangle
public class PascalsTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
int num = 1;
System.out.format("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%4d", num);
num = num * (i - j) / (j + 1);
}
System.out.println();
}
}
}
47. Find the Missing Number in an Array
public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6};
int n = arr.length + 1;
int total = n * (n + 1) / 2;
for (int num : arr) {
total -= num;
}
System.out.println(total);
}
}
48. Convert Decimal to Binary
public class DecimalToBinary {
public static void main(String[] args) {
int num = 10;
String binary = Integer.toBinaryString(num);
System.out.println(binary);
}
}
49. Check for Perfect Number
public class PerfectNumber {
public static void main(String[] args) {
int num = 28, sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
System.out.println(num == sum);
}
}
50. Implementing a Simple Calculator
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter operation (+, -, *, /): ");
char operation = scanner.next().charAt(0);
double result;
switch (operation) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
default: throw new IllegalArgumentException("Invalid operation");
}
System.out.println("Result: " + result);
}
}
51. Find the Length of a String
public class StringLength {
public static void main(String[] args) {
String str = "Automation";
System.out.println(str.length());
}
}
52. Check if a String is Empty
public class CheckEmptyString {
public static void main(String[] args) {
String str = "";
System.out.println(str.isEmpty());
}
}
53. Count the Occurrences of a Character in a String
public class CountCharacter {
public static void main(String[] args) {
String str = "Automation";
char ch = 'a';
int count = 0;
for (char c : str.toCharArray()) {
if (c == ch) count++;
}
System.out.println(count);
}
}
54. Remove All Whitespaces from a String
public class RemoveWhitespaces {
public static void main(String[] args) {
String str = " A u t o m a t i o n ";
String result = str.replaceAll("\\s+", "");
System.out.println(result);
}
}
55. Find the Common Elements in Two Arrays
import java.util.HashSet;
public class CommonElements {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {3, 4, 5, 6};
HashSet<Integer> set = new HashSet<>();
for (int num : arr1) {
set.add(num);
}
for (int num : arr2) {
if (set.contains(num)) {
System.out.print(num + " ");
}
}
}
}
Finding Common Elements in Arrays
Input =
array1 = { 4, 2, 3, 1, 6 }; array2 = { 6, 7, 8, 4 };
Output = 6,4
// By using the for loop
Integer[] array1 = { 4, 2, 3, 1, 6 };
Integer[] array2 = { 6, 7, 8, 4 };
List<Integer> commonElements = new ArrayList<>();
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
commonElements.add(array1[i]);
} }}
System.out.println("Common Elements for given two array List is:" +
commonElements);
// by using ArrayList with retainAll method
ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(array1));
ArrayList<Integer> list2 = new ArrayList<>(Arrays.asList(array2));
list1.retainAll(list2);
System.out.println("Common Elements:" + list1);
// By using Java Stream
String[] array3 = { "Java", "JavaScript", "C", "C++" };
String[] array4 = { "Python", "C#", "Java", "C++" };
ArrayList<String> list3 = new ArrayList<>(Arrays.asList(array3));
ArrayList<String> list4 = new ArrayList<>(Arrays.asList(array4));
List<String> commonElements1 =
list3.stream().filter(list4::contains).collect(Collectors.toList());
System.out.println(commonElements1);
}}
56. Generate Random Numbers
import java.util.Random;
public class RandomNumbers {
public static void main(String[] args) {
Random random = new Random();
for (int i = 0; i < 5; i++) {
System.out.println(random.nextInt(100)); // Random number between 0-99
}
}
}
57. Check if a Year is Leap Year
public class LeapYear {
public static void main(String[] args) {
int year = 2024;
boolean isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
System.out.println(isLeap);
}
}
58. Find the Sum of First N Natural Numbers
public class SumOfNaturalNumbers {
public static void main(String[] args) {
int n = 10, sum = n * (n + 1) / 2;
System.out.println(sum);
}
}
59. Implement a Simple Login System
import java.util.Scanner;
public class SimpleLogin {
public static void main(String[] args) {
String username = "admin";
String password = "password";
Scanner scanner = new Scanner(System.in);
System.out.print("Enter username: ");
String inputUsername = scanner.nextLine();
System.out.print("Enter password: ");
String inputPassword = scanner.nextLine();
if (username.equals(inputUsername) && password.equals(inputPassword)) {
System.out.println("Login successful!");
} else {
System.out.println("Login failed!");
}
}
}
60. Java program to count the number of words in
a string
public class Main {
public static void main(String[] args) {
System.out.println("Enter the String");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int count = 1;
for (int i = 0; i < s.length() - 1; i++) {
if ((s.charAt(i) == ' ') && (s.charAt(i + 1) != ' ')) {
count++;
}
}
System.out.println("Number of words in a string: " +count); }
}
Enter the String: Welcome to Java World
Number of words in a string: 4
61-EvenOdd add number even to even odd to odd
public static void main(String[] args) {
// TODO Auto-generated method stub
int number=12345678;
int evenSum=0,oddSum=0;
while(number>0) {
int digit=number%10;
if(digit%2==0) {
evenSum+=digit;
} else {
oddSum+=digit;
}
number/=10;
}
System.out.println(evenSum);
System.out.println(oddSum);
}
62.Character count in the string
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="aaabbbcccc";
int countA=0,countB=0,countC=0;
for(int i=0;i<s.length();i++) {
char ch=s.charAt(i);
if(ch=='a') {
countA++;
}else if(ch=='b') {
countB++;
}else if(ch=='c') {
countC++;
}
}
System.out.println(countA);
System.out.println(countB);
System.out.println(countC);
}
63.Higher number print
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Integer> numbers = new HashSet<>();
Collections.addAll(numbers, 2, 4, 3, 15, 7, 1);
//numbers.add(2);
//numbers.add(4);
//numbers.add(3);
// numbers.add(5);
//numbers.add(7);
//numbers.add(1);
// Find the maximum value using Collections.max
int maxNumber = Collections.max(numbers);
System.out.println("The highest number is: " + maxNumber);
}
}
64. Find first and last element of ArrayList in java
Input = array1 = { 4, 2, 3, 1, 6 };
Output = First is:4, Last is: 6
ArrayList<Integer> list = new ArrayList<Integer>(5);
// find first element
int first = list.get(0);//First Element
// find last element
int last = list.get(list.size() - 1);//last Element
----------------------------------------------------------------------------------------------
65. Second Largest and Second Smallest Numbers:
// Code to find second largest and second smallest numbers in an array
int[] arrayList = { 4, 2, 3, 1,0, 6,12,15,20 };
int num=arrayList.length;
Arrays.sort(arrayList);
System.out.println("Second Largest element is "+arrayList[num-2]); //Display
Second
Smallest
System.out.println("Second Smallest element is "+arrayList[1]);
66. # Even and Odd Number Separation in Java
public class EvenOddSeparation {
public static void main(String[] args) {
String s = "1234567898765";
StringBuilder evenNumbers = new StringBuilder();
StringBuilder oddNumbers = new StringBuilder();
for (char c : s.toCharArray()) {
int digit = Character.getNumericValue(c);
if (digit % 2 == 0) {
evenNumbers.append(digit);
} else {
oddNumbers.append(digit);
}
}
System.out.println("Even Numbers: " + evenNumbers);
System.out.println("Odd Numbers: " + oddNumbers);
}
}
67. public class FloydsTriangle {
public static void main(String[] args) {
int rows = 5; // Number of rows in Floyd's triangle
int number = 1; // Starting number
// Loop through each row
for (int i = 1; i <= rows; i++) {
// Loop through each column in the current row
for (int j = 1; j <= i; j++) {
System.out.print(number + " "); // Print the current number
number++; // Increment the number
}
System.out.println(); // Move to the next line after each row
}
}
}
68. How to convert string to integer in java
program
public class StringToIntExample{
public static void main(String args[]){
String s="200";
int i=Integer.parseInt(s);
System.out.println(s+100);//200100 because + is string concatenation operator
System.out.println(i+100);//300 because + is binary plus operator
}}
69. How to convert integer to string in java
program
public class IntToStringExample1{
public static void main(String args[]){
int i=200;
String s=String.valueOf(i);
System.out.println(i+100);//300 because + is binary plus operator
System.out.println(s+100);//200100 because + is string concatenation operator
}}
70. How to convert string to long in java
public class StringToLongExample{
public static void main(String args[]){
String s="9990449935";
long l=Long.parseLong(s);
System.out.println(l);
}}
71. How to convert string to float in java
public class StringToFloatExample{
public static void main(String args[]){
String s="23.6";
float f=Float.parseFloat("23.6");
System.out.println(f);
}}
72. How to convert string to double in java
program
public class StringToDoubleExample{
public static void main(String args[]){
icientfutomation ProA
String s="23.6";
double d=Double.parseDouble("23.6");
System.out.println(d);
}}
73. Chose first higher second highest any type of number use this
int[] arrayList = { 4, 2, 3, 1,0, 6,12,15,20 };
int num=arrayList.length;
Arrays.sort(arrayList);
System.out.println("Second Largest element is "+arrayList[num-9]);
//Display Second
//Smallest
// System.out.println("Second Smallest element is "+arrayList[0]);
74. Reverse each word in a sentence
Input = "reverse each word in a string";
Output = "esrever hcae drow ni a gnirts"
public static void main(String[] args) {
String str = "reverse each word in a string";
String words[] = str.split("\\s");
String reverseWord = "";
for (String w : words) {
StringBuilder sb = new StringBuilder(w);
sb.reverse();
reverseWord = reverseWord + sb.toString() + " ";
}
System.out.println(reverseWord.trim());
75. Reverse the entire sentence
Input = "India is country My"
Output = "My country is India"
public static void main(String[] args) {
String str[] = "India is country My".split(" ");
String ans = "";
for (int i = str.length - 1; i >= 0; i--) {
ans = ans + str[i] + " ";
}
System.out.println(ans.substring(0, ans.length() - 1));
}
76. Find first and last element of ArrayList in java
int[] array1 = { 4, 2, 3, 1, 6 };
ArrayList<Integer> list = new ArrayList<Integer>(5);
for (int num : array1) {
list.add(num);
}
int first = list.get(0);//First Element
// find last element
int last = list.get(list.size() - 1);//last Element
System.out.println("First Element: " + first);
System.out.println("Last Element: " + last);