Java Programs
Java Programs
Java Programs
}
Calculate electricity bill
public class ElectricBill
{
public static void main(String args[])
{
int units = 123;
int bill = 0;
}
if (arg == sum)
{
System.out.println("Given number is armstrong number: " + arg);
}
else
{
System.out.println("Given number is not armstrong number: " + arg);
}
}
}
Program to print Armstrong number between 1 to 1000
public class ArmstrongNumbers
{
if (sum == i)
{
System.out.print(i + " ");
}
sum = 0;
}
}
Print given number in words
public class NumberToWords
{
String ten[] = { " ", " ", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", "Seventy", " Eighty",
" Ninety" };
if (n > 19)
{
System.out.print(ten[n / 10] + " " + one[n % 10]);
}
else
{
System.out.print(one[n]);
}
if (n > 0)
System.out.print(ch);
}
int n=28;
System.out.print(n);
if (n <= 0)
{
System.out.println("Enter numbers greater than 0");
}
else
{
NumberToWords a = new NumberToWords();
a.pw((n / 1000000000), " Hundred");
a.pw((n / 10000000) % 100, " crore");
a.pw(((n / 100000) % 100), " lakh");
a.pw(((n / 1000) % 100), " thousand");
a.pw(((n / 100) % 10), " hundred");
a.pw((n % 100), " ");
}
}
}
Program to check the given number is Palindrome or not
public class PalindromeNumberCheck
{
while (n > 0)
{
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
if (rev == pal)
{
System.out.println(" The given no is palindrome "+ rev);
}
else
{
System.out.println("The given no is not palindrome " + rev);
}
}
Program to print palindrome number upto N numbers
public class PalindromeUptoN
{
}
Program to print N prime numbers and find sum and average
public class PrimeNumberUptoN
{
*
**
***
****
*****
***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
***************
class FloydsTriangle
{
public static void main(String args[])
{
count++;
}
System.out.println();
}
}
}
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Print numbers in sequence way
public class PatternNumberSequence
{
public static void main(String[] args)
{
int a = 3;
int b = 4;
int n = 8;
}
}
}
347
7 5 12
12 6 18
18 7 25
25 8 33
33 9 42
42 10 52
52 11 63
Print numbers in triangle and pyramid vice
1
121
12321
1234321
123454321
import java.util.Scanner;
public class PatternNuberPyramidPrevRev
{
public static void main(String args[])
{
int s = 1;
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the N values");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
while (s <= i)
{
System.out.print(s);
s++;
}
s--;
while (s > 1)
{
System.out.print(--s);
}
System.out.println();
}
}
}
1
23
456
7 8 9 10
11 12 13 14 15
}
}
1
12
123
1234
12345
import java.util.Scanner;
int i, j, n;
}
}
}
}
a--;
}
System.out.println();
}
}
}
1
212
32123
4321234
543212345
Print different patterns using stars
*****
****
***
**
*
import java.util.Scanner;
public class Star1
{
public static void main(String args[])
{
int i, j, t;
System.out.println("How many row you want ");
Scanner sc = new Scanner(System.in);
t = sc.nextInt();
for (j = 0; j < t; j++)
{
for (i = t - 1; i >= j; i--)
{
System.out.print("*");
}
System.out.println("");
}
}
}
* *
** **
******
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++)
{
System.out.print("*");
}
for (j = (n - i); j >= 2; j--)
{
System.out.print(" ");
}
for (k = i; k >= 0; k--)
{
System.out.print("*");
}
System.out.println();
}
}
}
*
***
*****
*******
*********
*******
*****
***
*
Print pyramid triangle with star and numbers
public class Star10
{
int arr[] = { 14, 46, 47, 86, 92, 52, 48, 36, 66, 85 };
int largest = arr[0];
int secondLargest = arr[0];
}
}
}
}
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 };
for (int i = 1; i < a.length; i++) // iterate for loop from arrays 1st index (second
element)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
}
Program to find largest and second largest in an array
public class LargestAndSecondLargest
{
public static void main(String[] args)
{
int a[] = new int[] { 12, 44, 23, 56, 23, 78, 13 };
}
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 (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;
}
{
if (a[i] > a[j])
{
s = a[i];
a[i] = a[j];
a[j] = s;
}
}
if (a[i] % 2 != 0)
{
{
if (a[i] % 2 == 0)
{
}
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(setValue == true)
{
System.out.println("The Given Matrix is a Null Matrix");
}
else
{
System.out.println("The Given Matrix is not a Null Matrix");
}
}
}
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 < a.length;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;
}
}
}
if(setValue == true)
{
System.out.println("The Given Matrix is a Diagonal Matrix");
}
else
{
System.out.println("The Given Matrix is not a Diagonal Matrix");
}
}
}
Program for Linear search
import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{
int i, num, searchval, array[];
in.close();
for (i = 0; i < num; i++)
{
if (array[i] == searchval)
{
System.out.println(searchval + " is present at location " + (i + 1));
break;
}
}
if (i == num)
System.out.println(searchval + " is not exist in array.");
}
}
Program for Binary Search
import java.util.Scanner;
public class BinarySearch
{
public static void main(String args[])
{
int counter, num, item, array[], first, last, middle;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();
hcf = a;
lcm = (x * y) / hcf;
System.out.println("Number = "+no);
System.out.println("Reverse = "+rev);
}
}
int count = 1;
}
}
System.out.println("Number of words in a string = " + count);
}
}
for(int i=0;i<words.length;i++)
{
for(int j=i+1;j<words.length;j++)
{
if(words[i].equals(words[j]))
{
wrc=wrc+1;
words[j]="0";
}
}
if(words[i]!="0")
System.out.println(words[i]+"--"+wrc);
wrc=1;
}
Program to remove duplicate words in given string
public class RemoveDuplicate
{
for(int j=i+1;j<words.length;j++)
{
if(words[i].equals(words[j]))
{
words[j]=null;
}
}
}
}
for(int k=0;k<words.length;k++)
{
if(words[k]!=null)
{
System.out.println(words[k]);
}
}
}
}
Program to count each words and total number of words in given string
import java.io.IOException;
fr[i]++;
}
}
}
int total = 0;
System.out.println("Words and words count:");
for (int i = 0; i < words.length; i++)
{
if (words[i] != "")
{
System.out.println(words[i] + "=" + fr[i]);
total += fr[i];
}
}
System.out.println("Total words counted: " + total);
}
}
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 = inpstr.toCharArray();
char[] revArray = new char[inpArray.length];
int j=0;
for (int i = inpArray.length - 1; i >= 0; i--) {
revArray[j]=inpArray[i];
j++;
}
String revstr=String.valueOf(revArray);
if(inpstr.equals(revstr))
{
System.out.println("The given string is a Palindrome");
}
else
{
System.out.println("The given string is not a Palindrome");
}
}
}
Program to delete vowels in a given string
public class RemoveAllVovels {
result.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].substring(1)).append
(" ");
System.out.println(result);
}
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=input.split(",");
for(int k=0;k<words.length;k++)
System.out.println(words[k]);
}
}
}
Program to replace vowels with star
public class VowelswithStar
{
public static void main(String[] args)
{
String string = "Welcome to Candid Java Programming"; //Input String
System.out.println("Input String : "+string); //Displaying Input String
string = string.replaceAll("[AaEeIiOoUu]", "*"); //Replace vowels with star
System.out.println(string); //Display the word after replacement
}
Program to print character position count in a given string
public class LetterPositionCount
{
String s = "CANDIDJAVA";
char[] a = s.toCharArray();
int i = 1;
{
for (char output : a)
{
}
}
}
}
Program to print reversed string by word in given line
public class ReverseWord
{
for(int i=1;i<inputArray.length;i++)
{
if(inputArray[i]>maxValue)
{
maxValue=inputArray[i];
}
}
return maxValue;
}
int getMin(int[]inputArray)
{
int minValue=inputArray[0];
for(int i=1;i<inputArray.length;i++)
{
if(inputArray[i]<minValue)
{
minValue=inputArray[i];
}
}
return minValue;
}
}
class NoOfOccurenceOfCharacters
{
static final int MAX_CHAR = 256;
static void getOccuringChar(String str)
{
int count[] = new int[MAX_CHAR];
int len = str.length();
for (int i = 0; i < len; i++)
count[str.charAt(i)]++;
char ch[] = new char[str.length()];
for (int i = 0; i < len; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
if (str.charAt(i) == ch[j])
find++;
}
if (find == 1)
System.out.println("Number of Occurrence of " +
str.charAt(i) + " is:" + count[str.charAt(i)]);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = "geeksforgeeks";
getOccuringChar(str);
}
}