JAVA PROGRAMS:
1. Find the sum of the digits of a number:
public class DigitsSum
{
public static void main(String[] args)
{
int num=251025, rem = 0, sum = 0, temp;
temp = num;
while (num > 0)
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
[Link]("Sum of Digits of " + temp + " is " + sum);
}
}
2. Calculate electricity bill:
public class ElectricBill
{
public static void main(String args[])
{
int units = 123;
int bill = 0;
if (units > 100)
{
if (units >= 200)
{
if (units > 300)
{
bill = units * 8;
}
else
bill = units * 6;
}
else
bill = units * 5;
}
[Link]("Units Consumed : " + units);
[Link]("Total Bill : " + bill);
}
}
3. Java program to find Armstrong number
public class ArmstrongNumber
{
public static void main(String args[])
{
int n, arg, sum = 0, r;
n = 153; // input value
arg = n;
for (int i = 1; i < n; i++)
{
while (n > 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
}
if (arg == sum)
{
[Link]("Given number is armstrong number: " + arg);
}
else
{
[Link]("Given number is not armstrong number: " + arg);
}
}
}
4. Program to print Armstrong number between 1 to 1000:
public class ArmstrongNumbers
{
public static void main(String[] args)
{
int num,rem,limit=1000, sum = 0;
[Link]("Armstrong numbers from 1 to N:");
for (int i = 1; i <= limit; i++)
{
num = i;
while (num > 0)
{
rem = num % 10;
sum = sum + (rem*rem*rem);
num = num / 10;
}
if (sum == i)
{
[Link](i + " ");
}
sum = 0;
}
}
}
5. Print given number in words:
public class NumberToWords
{
public void pw(int n, String ch)
{
String one[] = { " ", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", "
Nine", " Ten"," Eleven", " Twelve", " Thirteen", " Fourteen", "Fifteen", " Sixteen", " Seventeen",
" Eighteen"," Nineteen" };
String ten[] = { " ", " ", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", "Seventy", " Eighty",
" Ninety" };
if (n > 19)
{
[Link](ten[n / 10] + " " + one[n % 10]);
}
else
{
[Link](one[n]);
}
if (n > 0)
[Link](ch);
}
public static void main(String[] args)
{
int n=28;
[Link](n);
if (n <= 0)
{
[Link]("Enter numbers greater than 0");
}
else
{
NumberToWords a = new NumberToWords();
[Link]((n / 1000000000), " Hundred");
[Link]((n / 10000000) % 100, " crore");
[Link](((n / 100000) % 100), " lakh");
[Link](((n / 1000) % 100), " thousand");
[Link](((n / 100) % 10), " hundred");
[Link]((n % 100), " ");
}
}
}
6. Program to check the given number is Palindrome or not:
public class PalindromeNumberCheck
{
public static void main(String[] args)
{
int n=121,pal,r,rev=0;
pal = n;
while (n > 0)
{
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
if (rev == pal)
{
[Link](" The given no is palindrome "+ rev);
}
else
{
[Link]("The given no is not palindrome " + rev);
}
}
}
7. Program to print palindrome number upto N numbers:
public class PalindromeUptoN
{
public static void main(String[] args)
{
int n, b, rev = 0;
int limit=50;
[Link]("Palindrome numbers from 1 to N:");
for (int i = 1; i <= limit; i++)
{
n = i;
while (n > 0)
{
b = n % 10;
rev = rev * 10 + b;
n = n / 10;
}
if (rev == i)
{
[Link](i + " ");
}
rev = 0;
}
}
}
8. Program to print N prime numbers and find sum and average:
public class PrimeNumberUptoN
{
public static void main(String[] args)
{
int num =0, i =0;
[Link]("Prime numbers from 1 to 100 are :");
for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
[Link](i+" ");
}
}
}
9. Program to print patterns of numbers and stars:
*
**
***
****
public class PyramidPattern1
{
public static void main(String[] args)
{
int n=4;
for(int i=0;i<n;i++)
{
[Link]("\n");
for(int j=0;j<=i;j++)
{
[Link](" * ");
}
}
}
}
*
**
***
****
*****
public class PyramidPattern2
{
public static void main(String args[])
{
int i, j, k=8;
for(i=0; i<5; i++)
{
for(j=0; j<k; j++)
{
[Link](" ");
}
k = k - 2;
for(j=0; j<=i; j++)
{
[Link]("* ");
}
[Link]();
}
}
}
***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
**
**
** **
*** ***
**** ****
***** *****
****** ******
******* *******
***************
public class DifferentPatternPrograms1
{
public static void main(String args[])
{
int n,i,j,k,l,m,p,q,r,s;
Scanner sc=new Scanner([Link]);
[Link]("Enter the n values");
n=[Link]();
p=n;
q=n;
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
[Link]("*");
}
for(k=p*2;k<n*2-1;k++)
{
[Link](" ");
}
for(l=i;l!=0;l--)
{
if(l==n)
{
continue;
}
[Link]("*");
}
p--;
[Link]();
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link]("*");
}
for(k=q*2-2;k>1;k--)
{
[Link](" ");
}
for(m=i;m!=0;m--)
{
if(m==n)
{
continue;
}
[Link]("*");
}
[Link]();
q--;
}
}
}
10. Print Floyds triangle:
import [Link];
class FloydsTriangle
{
public static void main(String args[])
{
Scanner scan = new Scanner([Link]);
[Link]("Enter the number of rows\n");
int rows = [Link]();
[Link]("Floyd's Triangle Generated\n");
int count = 1;
for ( int i = 1 ; i <= rows ; i++ )
{
for ( int j = 1 ; j <= i ; j++ )
{
[Link](count+" ");
count++;
}
[Link]();
}
}
}
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
11. Print numbers in sequence way:
public class PatternNumberSequence
{
public static void main(String[] args)
{
int a = 3;
int b = 4;
int n = 8;
for (int i = 1; i <= n; i++)
{
int c = a + b;
[Link](a + " " + b + " " + c);
[Link](" ");
a = c;
b = b + 1;
}
}
}
347
7 5 12
12 6 18
18 7 25
25 8 33
33 9 42
42 10 52
52 11 63
12. Print numbers in triangle and pyramid vice:
1
121
12321
1234321
123454321
import [Link];
public class PatternNuberPyramidPrevRev
{
public static void main(String args[])
{
int s = 1;
int n;
Scanner sc = new Scanner([Link]);
[Link]("Enter the N values");
n = [Link]();
for (int i = 1; i <= n; i++)
{
while (s <= i)
{
[Link](s);
s++;
}
s--;
while (s > 1)
{
[Link](--s);
}
[Link]();
}
}
}
1
23
456
7 8 9 10
11 12 13 14 15
public class PatternNumberPyramidUptoN
{
public static void main(String args[])
{
{
int i, j, n = 1;
for (i = 0; i < 5; i++)
{
for (j = 0; j <= i; j++)
{
[Link](n + " ");
n++;
}
[Link]();
}
}
}
}
1
12
123
1234
12345
import [Link];
public class PatternNumberPyramid
{
public static void main(String args[])
{
int i, j, n;
Scanner sc = new Scanner([Link]);
[Link]("Enter the Row value n");
n = [Link]();
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
[Link](" " + j);
[Link]("\n");
}
}
}
13. Print numbers in pyramid vice:
import [Link];
public class PatternNumberPyramidArrow
{
public static void main(String args[])
{
int i, j, n;
Scanner sc = new Scanner([Link]);
[Link]("Enter the values ");
n = [Link]();
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
[Link](" " + j);
[Link]("\n");
}
for (i = n - 1; i >= 1; i--)
{
for (j = 1; j <= i; j++)
[Link](" " + j);
[Link]("\n");
}
}
}
1
12
123
1234
12345
1234
123
12
1
import [Link];
public class PatternNumberPyramidRev
{
public static void main(String args[])
{
int i, j, k, n, a;
Scanner sc = new Scanner([Link]);
[Link]("Enter the n values");
n = [Link]();
a = n;
for (i = 1; i <= n; i++)
{
for (j = a; j > 1; j--)
{
[Link](" ");
}
for (k = i; k != 0; k--)
{
[Link](k);
}
a--;
for (int l = 2; l <= i; l++)
{
[Link](l);
}
[Link]();
}
}
}
1
212
32123
4321234
543212345
14. Print different patterns using stars
*****
****
***
**
*
import [Link];
public class Star1
{
public static void main(String args[])
{
int i, j, t;
[Link]("How many row you want ");
Scanner sc = new Scanner([Link]);
t = [Link]();
for (j = 0; j < t; j++)
{
for (i = t - 1; i >= j; i--)
{
[Link]("*");
}
[Link]("");
}
}
}
**
** **
******
public class Star3
{
public static void main(String[] x)
{
int i, j, k, n = 3;
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{
[Link]("*");
}
for (j = (n - i); j >= 2; j--)
{
[Link](" ");
}
for (k = i; k >= 0; k--)
{
[Link]("*");
}
[Link]();
}
}
}
*
***
*****
*******
*********
*******
*****
***
*
15. Print pyramid triangle with star and numbers:
public class Star10
{
public static void main(String args[])
{
int i, j, k;
for (i = 1; i <= 5; i++)
{
for (j = i; j < 5; j++)
{
[Link](" ");
}
for (k = 1; k < (i * 2); k++)
{
[Link]("*");
}
[Link]("");
}
for (i = 4; i >= 1; i--)
{
for (j = 5; j > i; j--)
{
[Link](" ");
}
for (k = 1; k < (i * 2); k++)
{
[Link]("*");
}
[Link]("");
}
}
}
16. Program to find largest number in an array:
class LargestNumber
{
public static void main(String args[])
{
int[] a = new int[] { 20, 30, 50, 4, 71, 100};
int max = a[0];
for(int i = 1; i < [Link];i++)
{
if(a[i] > max)
{
max = a[i];
}
}
[Link]("The Given Array Element is:");
for(int i = 0; i < [Link];i++)
{
[Link](a[i]);
}
[Link]("From The Array Element Largest Number is:" + max);
}
}
17. Program to find second largest number in an array:
public class SecondLargest {
public static void main(String[] args) {
int arr[] = { 14, 46, 47, 86, 92, 52, 48, 36, 66, 85 };
int largest = arr[0];
int secondLargest = arr[0];
[Link]("The given array is:" );
for (int i = 0; i < [Link]; i++) {
[Link](arr[i]+"\t");
}
for (int i = 0; i < [Link]; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest) {
secondLargest = arr[i];
}
}
[Link]("\nSecond largest number is:" + secondLargest);
}
}
18. Find largest and smallest number in an array in java:
public class LargestSmallest
{
public static void main(String[] args)
{
int a[] = new int[] { 23, 34, 13, 64, 72, 90, 10, 15, 9, 27 };
int min = a[0]; // assume first elements as smallest number
int max = a[0]; // assume first elements as largest number
for (int i = 1; i < [Link]; i++) // iterate for loop from arrays 1st index (second
element)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
[Link]("Largest Number in a given array is : " + max);
[Link]("Smallest Number in a given array is : " + min);
}
}
19. Program to find largest and second largest in an array:
public class LargestAndSecondLargest
{
public static void main(String[] args)
{
int nums[] = { 5, 34, 78, 2, 45, 1, 99, 23 };
int maxOne = 0;
int maxTwo = 0;
for (int i=0;i<[Link]; i++)
{
if (maxOne < nums[i])
{
maxTwo = maxOne;
maxOne = nums[i];
}
else if (maxTwo < nums[i])
{
maxTwo = nums[i];
}
}
[Link]("Largest Number: " + maxOne);
[Link]("Second Largest Number: " + maxTwo);
}
}
20. Find the index of the largest number in an array:
public class LargestNumberIndex
{
public static void main(String[] args)
{
int a[] = new int[] { 12, 44, 23, 56, 23, 78, 13 };
int max = a[0];
int index = 0;
for (int i = 0; i < [Link]; i++)
{
if (max < a[i])
{
max = a[i];
index = i;
}
}
[Link]("Index position of Maximum value in an array is : " +index);
}}
21. Find the index of the smallest number in an array:
public class SmallestNumberIndex
{
public static void main(String[] args) {
int a[] = new int[]{12,44,23,56,9,23,78,13};
int min = a[0];
int index=0;
for(int i = 0; i < [Link]; i++)
{
if(min > a[i])
{
min = a[i];
index=i;
}
}
[Link]("Index position of Smallest value in a given array is :"+index);
}
}
22. Program to remove duplicate element in an array:
public class RemoveDuplicateElements
{
public static int[] removeDuplicates(int[] input)
{
int j = 0;
int i = 1;
// return if the array length is less than 2
if ([Link] < 2)
{
return input;
}
while (i < [Link])
{
if (input[i] == input[j])
{
i++;
}
else
{
input[++j] = input[i++];
}
}
int[] output = new int[j + 1];
for (int k = 0; k < [Link]; 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);
[Link]("Input Elements: \n");
for (int i : input1)
{
[Link](i + " ");
}
[Link]("\nOutput Elements: \n");
for (int i : output)
{
[Link](i + " ");
}
}
}
23. Program to print odd and even numbers from an array:
public class OddEvenArray
{
public static void main(String args[])
{
int s, i;
int[] a = { 33, 2, 4, 71, 88, 92, 9, 1 };
for (i = 0; i < [Link]; i++)
{
for (int j = i + 1; j < [Link]; j++)
{
if (a[i] > a[j])
{
s = a[i];
a[i] = a[j];
a[j] = s;
}
}
}
[Link]("Input numbers :");
for (i = 0; i < [Link]; i++)
{
[Link](" " + a[i]);
}
[Link]("\nOdd numbers :");
for (i = 0; i <= [Link] - 1; i++)
{
if (a[i] % 2 != 0)
{
[Link](" " + a[i]);
}
}
[Link]("\nEven numbers :");
for (i = 0; i < [Link]; i++)
{
if (a[i] % 2 == 0)
{
[Link](" " + a[i]);
}
}
}
}
24. Program to add two matrix:
class MatrixAddition
{
public static void main(String args[])
{
int[][] a = new int[][] { { 1, 2, 3},{ 4, 5, 6},{ 7, 8, 9} };
int[][] b = new int[][] { { 10, 11, 12},{ 13, 14, 15},{ 16, 17, 18} };
int[][] c = new int[3][3];
if([Link] == [Link] && a[0].length == b[0].length)
{
for(int i = 0;i < [Link];i++)
{
for(int j = 0;j < a[i].length;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
}
else
{
[Link]("'A' and 'B' Matrix are not SAME");
return;
}
[Link]("The Matrix 'A' Value:");
for(int i = 0;i < [Link];i++)
{
for(int j = 0;j < a[i].length;j++)
{
[Link](a[i][j] + " ");
}
[Link]();
}
[Link]("The Matrix 'B' Value:");
for(int i = 0;i < [Link];i++)
{
for(int j = 0;j < a[i].length;j++)
{
[Link](b[i][j]+ " ");
}
[Link]();
}
[Link]("The Addition Matrix of 'A' and 'B' Value:");
for(int i = 0;i < [Link];i++)
{
for(int j = 0;j < a[i].length;j++)
{
[Link](c[i][j] + " ");
}
[Link]();
}
}
}
25. Program to check given matrix is null matrix:
class NullMatrix
{
public static void main(String args[])
{
int[][] a = new int[][] { { 0, 0, 0},{ 0, 0, 1},{ 0, 0, 0} };
boolean setValue = true;
abc: for(int i = 0;i < [Link];i++)
{
for(int j = 0;j < a[i].length;j++)
{
if(a[i][j] != 0)
{
setValue = false;
break abc;
}
}
}
[Link]("The Given Matrix Value:");
for(int i = 0;i < [Link];i++)
{
for(int j = 0;j < a[i].length;j++)
{
[Link](a[i][j] + " ");
}
[Link]();
}
if(setValue == true)
{
[Link]("The Given Matrix is a Null Matrix");
}
else
{
[Link]("The Given Matrix is not a Null Matrix");
}
}
}
26. Program to check given matrix is diagonal matrix:
class DiagonalMatrix
{
public static void main(String args[])
{
int[][] a = new int[][] { { 1, 0, 1},{ 0, 3, 0},{ 0, 0, 3} };
boolean setValue = true;
abc: for(int i = 0;i < [Link];i++)
{
for(int j = 0;j < a[i].length;j++)
{
if(i == j)
{
if(a[i][j] == 0)
{
setValue = false;
break abc;
}
}
else if(a[i][j] != 0)
{
setValue = false;
break abc;
}
}
}
[Link]("The Given Matrix Value:");
for(int i = 0;i < [Link];i++)
{
for(int j = 0;j < a[i].length;j++)
{
[Link](a[i][j] + " ");
}
[Link]();
}
if(setValue == true)
{
[Link]("The Given Matrix is a Diagonal Matrix");
}
else
{
[Link]("The Given Matrix is not a Diagonal Matrix");
}
}
}
27. Program for Linear search:
import [Link];
class LinearSearch
{
public static void main(String args[])
{
int i, num, searchval, array[];
Scanner in = new Scanner([Link]);
[Link]("Enter number of elements");
num = [Link]();
array = new int[num];
[Link]("Enter " + num + " integers");
for (i = 0; i < num; i++)
array[i] = [Link]();
[Link]("Enter the search value:");
searchval = [Link]();
[Link]();
for (i = 0; i < num; i++)
{
if (array[i] == searchval)
{
[Link](searchval + " is present at location " + (i + 1));
break;
}
}
if (i == num)
[Link](searchval + " is not exist in array.");
}
}
28. Program for Binary Search:
import [Link];
public class BinarySearch
{
public static void main(String args[])
{
int counter, num, item, array[], first, last, middle;
Scanner input = new Scanner([Link]);
[Link]("Enter number of elements:");
num = [Link]();
array = new int[num];
[Link]("Enter " + num + " integers");
for (counter = 0; counter < num; counter++)
array[counter] = [Link]();
[Link]("Enter the search value:");
item = [Link]();
first = 0;
last = num - 1;
middle = (first + last) / 2;
while (first <= last)
{
if (array[middle] < item)
first = middle + 1;
else if (array[middle] == item)
{
[Link](item + " found at location " + (middle + 1) + ".");
break;
}
else
{
last = middle - 1;
}
middle = (first + last) / 2;
}
if (first > last)
[Link](item + " is not found.\n");
}
}
29. Program to calculate HCF and LCM:
public class FindHCFAndLCM
{
public static void main(String args[])
{
int a, b, x, y, t, hcf, lcm;
x = 6;
y = 10;
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
hcf = a;
lcm = (x * y) / hcf;
[Link]("HCF and LCM of : " + x + " and " + y + " is :\n");
[Link]("HCF = " + hcf);
[Link]("\nLCM = " + lcm);
}
}
30. Program to find volume of cube:
public class Cube {
public static void main(String arg[]) {
int side=5;
float volume=side * side * side;
[Link]("Volume of Cube :"+ volume);
}
}
31. program to print the reverse of a given number:
public class ReverseNum
{
public static void main(String[] args)
{
int rev = 0;
int num = 1234;
int no=num;
while (num > 0)
{
int rem = num % 10;
rev = rem + (rev * 10);
num = num / 10;
}
[Link]("Number = "+no);
[Link]("Reverse = "+rev);
}
}
32. Program to convert integer to roman letters:
import [Link];
import [Link];
public class IntegertoRoman
{
private static int[] bases = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
private static HashMap<Integer, String> map = new HashMap<Integer, String>();
private static void setup()
{
[Link](1, "I");
[Link](4, "IV");
[Link](5, "V");
[Link](9, "IX");
[Link](10, "X");
[Link](40, "XL");
[Link](50, "L");
[Link](90, "XC");
[Link](100, "C");
[Link](400, "CD");
[Link](500, "D");
[Link](900, "CM");
[Link](1000, "M");
}
public String intToRoman(int num)
{
setup();
String result = new String();
for (int i : bases)
{
while (num >= i)
{
result += [Link](i);
num -= i;
}
}
return result;
}
public static void main(String arg[])
{
[Link]("Enter the number : ");
Scanner sc = new Scanner([Link]);
int no = [Link]();
IntegertoRoman in = new IntegertoRoman();
int value=no;
String sd = [Link](value);
[Link](value+" ---> " + sd);
}
}
33. Program to count number of words in given string:
public class WordCount
{
public static void main(String args[])
{
String s = "welcome to candid java tutorial";
int count = 1;
for (int i = 0; i < [Link]() - 1; i++)
{
if (([Link](i) == ' ') && ([Link](i + 1) != ' '))
{
count++;
}
}
[Link]("Number of words in a string = " + count);
}
}
34. Program to count number of duplicate words in given string:
public class CountWords
{
public static void main(String[] args)
{
String input="Welcome to Java Session Session Session";
String[] words=[Link](" ");
int wrc=1;
for(int i=0;i<[Link];i++)
{
for(int j=i+1;j<[Link];j++)
{
if(words[i].equals(words[j]))
{
wrc=wrc+1;
words[j]="0";
}
}
if(words[i]!="0")
[Link](words[i]+"--"+wrc);
wrc=1;
}
}
}
35. Program to remove duplicate words in given string:
public class RemoveDuplicate
{
public static void main(String[] args)
{
String input="Welcome to Java Session Java Session Session Java";
String[] words=[Link](" ");
for(int i=0;i<[Link];i++)
{
if(words[i]!=null)
{
for(int j=i+1;j<[Link];j++)
{
if(words[i].equals(words[j]))
{
words[j]=null;
}
}
}
}
for(int k=0;k<[Link];k++)
{
if(words[k]!=null)
{
[Link](words[k]);
}
}
}
}
36. Program to count each words and total number of words in given string:
import [Link];
public class FindTtalCountWords
{
public static void main(String args[]) throws IOException
{
countWords("apple banna apple fruit fruit apple hello hi hi hello hi");
}
static void countWords(String st)
{
String[] words = [Link]("\\s");
int[] fr = new int[[Link]];
for (int i = 0; i < [Link]; i++)
fr[i] = 0;
for (int i = 0; i < [Link]; i++)
{
for (int j = 0; j < [Link]; j++)
{
if (words[i].equals(words[j]))
{
fr[i]++;
}
}
}
for (int i = 0; i < [Link]; i++)
{
for (int j = 0; j < [Link]; j++)
{
if (words[i].equals(words[j]))
{
if (i != j)
{
words[i] = "";
}
}
}
}
int total = 0;
[Link]("Words and words count:");
for (int i = 0; i < [Link]; i++)
{
if (words[i] != "")
{
[Link](words[i] + "=" + fr[i]);
total += fr[i];
}
}
[Link]("Total words counted: " + total);
}
}
37. Program to reverse the string and check whether it is palindrome or not:
public class PalindromeChecking
{
public static void main(String[] args)
{
String inpstr ="AMMA";
char[] inpArray = [Link]();
char[] revArray = new char[[Link]];
int j=0;
for (int i = [Link] - 1; i >= 0; i--) {
revArray[j]=inpArray[i];
j++;
}
String revstr=[Link](revArray);
if([Link](revstr))
{
[Link]("The given string is a Palindrome");
}
else
{
[Link]("The given string is not a Palindrome");
}
}
}
38. Program to delete vowels in a given string:
public class RemoveAllVovels {
public static void main(String[] args) {
String string = "Welcome to Candid Java Programming";
[Link]("Input String : "+string);
string = [Link]("[AaEeIiOoUu]", "");
[Link](string);
}
}
39. Program to capitalize first letter of each word in string:
public class StringCapital
{
public static void main(String[] args)
{
String str = "welcome to candid java program";
StringBuilder result = new StringBuilder([Link]());
String words[] = [Link]("\\ ");
for (int i = 0; i < [Link]; i++)
{
[Link]([Link](words[i].charAt(0))).append(words[i].substring(1)).append ("
");
}
[Link](result);
}
}
40. Program to split a comma-separated string:
public class CommaSeparated
{
public static void main(String[] args)
{
String input="Welcome,to,Java Session Session Session";
String[] words=[Link](",");
for(int k=0;k<[Link];k++)
{
[Link](words[k]);
}
}
}
41. Program to convert ASCI value to String:
public class AsciiToCharacter
{
public static void main(String[] args)
{
char c;
for(int i=65;i<=90;i++)
{
c =(char)i;
[Link](i+" = "+c);
}
}
}
42. Program to replace vowels with star:
public class VowelswithStar
{
public static void main(String[] args)
{
String string = "Welcome to Candid Java Programming"; //Input String
[Link]("Input String : "+string); //Displaying Input String
string = [Link]("[AaEeIiOoUu]", "*"); //Replace vowels with star
[Link](string); //Display the word after replacement
}
}
43. Program to print character position count in a given string:
public class LetterPositionCount
{
public static void main(String args[])
{
String s = "CANDIDJAVA";
char[] a = [Link]();
int i = 1;
{
for (char output : a)
{
[Link](output + " " + i + " ");
i++;
}
}
}
}
44. Program to print reversed string by word in given line:
public class ReverseWord
{
public static void main(String[] args)
{
String input="Welcome to Java Session";
String[] words=[Link](" ");
String[] revwords=new String[[Link]];
int j=0;
for(int i=[Link]-1;i>=0;i--)
{
revwords[j]=words[i];
[Link](revwords[j]+" ");
j++;
}
}
}
45. Program to returning a string as reverse text:
public class StringReverse {
public static void main(String args[])
{
String string = "Welcome to Java Programming and Dotnet Programming";
String[] wordsCount = [Link](" ");
[Link]("The Given String is:\n" + string + "\n");
[Link]("After Reverse String is:");
for(int i = [Link];i > 0;i--)
{
[Link](wordsCount[i - 1] + " ");
}
}
}
46. Program to find difference of minimum and maximum numbers of array in java:
import [Link];
class MinMaxInArray
{
int getMax(int[]inputArray)
{
int maxValue=inputArray[0];
for(int i=1;i<[Link];i++)
{
if(inputArray[i]>maxValue)
{
maxValue=inputArray[i];
}
}
return maxValue;
}
int getMin(int[]inputArray)
{
int minValue=inputArray[0];
for(int i=1;i<[Link];i++)
{
if(inputArray[i]<minValue)
{
minValue=inputArray[i];
}
}
return minValue;
}
}
public class ExArrayDifference
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements you wants to enter :" );
n=[Link]();
int arr[]=new int[n];
for(int i=0;i<[Link];i++)
{
[Link]("Enter ["+(i+1)+"] element :" );
arr[i]=[Link]();
}
MinMaxInArray mm=new MinMaxInArray();
[Link]("Maximum value is :" +[Link](arr));
[Link]("Minimum value is :" +[Link](arr));
int Difference=[Link](arr)-[Link](arr);
[Link]("Difference between Minnimum and Maximum in array is : "
+Difference );
}
}
47. Program to count the occurrences of each character:
class NoOfOccurenceOfCharacters
{
static final int MAX_CHAR = 256;
static void getOccuringChar(String str)
{
int count[] = new int[MAX_CHAR];
int len = [Link]();
for (int i = 0; i < len; i++)
count[[Link](i)]++;
char ch[] = new char[[Link]()];
for (int i = 0; i < len; i++) {
ch[i] = [Link](i);
int find = 0;
for (int j = 0; j <= i; j++) {
if ([Link](i) == ch[j])
find++;
}
if (find == 1)
[Link]("Number of Occurrence of " + [Link](i) + " is:" + count[[Link](i)]);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String str = "geeksforgeeks";
getOccuringChar(str);
}
}