Javaprograms
Javaprograms
Output:
The Sum Of 10 Numbers are:55
Output :
Sum of Digits of 251025 is 15
*
**
***
****
*****
import java.util.Scanner;
class FloydsTriangle{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of rows\n");
int rows = scan.nextInt();
System.out.println("Floyd's Triangle Generated\n"); int count = 1;
for ( int i = 1 ; i <= rows ; i++ ){
for ( int j = 1 ; j <= i ; j++ ){
System.out.print(count+" ");
count++;}
System.out.println();}}}
Output:
Enter the number of rows
9
Floyd's Triangle Generated
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
13.Print numbers in sequence way
public class PatternNumberSequence
{
public static void main(String[] args)
{
int a = 3;
int b = 4;
int n = 8;
Output:
347
7 5 12
12 6 18
18 7 25
25 8 33
33 9 42
42 10 52
52 11 63
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();}}}
Output:
Enter the N values
7
1
121
12321
1234321
123454321
12345654321
1234567654321
}
}
Output:
1
23
456
7 8 9 10
11 12 13 14 15
int i, j, n;
}
}
Output:
Enter the Row value n
5
1
12
123
1234
12345
import java.util.Scanner;
public class PatternNumberPyramidArrow
{
public static void main(String args[])
{
int i, j, n;
Scanner sc = new Scanner(System.in); System.out.println("Enter the values
"); n = sc.nextInt();
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
System.out.print(" " + j);
System.out.print("\n");
}
for (i = n - 1; i >= 1; i--)
{
for (j = 1; j <= i; j++)
System.out.print(" " + j);
System.out.print("\n");}}}
Output:
Enter the values
5
1
12
123
1234
12345
1234
123
12
1
18.PrintPatternNumberPyramidRev
import java.util.Scanner;
public class PatternNumberPyramidRev
{
public static void main(String args[])
{
int i, j, k, n, a;
}
System.out.println();
}}}
Output :
1
212
32123
4321234
543212345
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("");
}
}
}
Output:
How many row you want
5
*****
****
***
**
*
Output:
* *
** **
******
int arr[] = { 14, 46, 47, 86, 92, 52, 48, 36, 66, 85 };
int largest = arr[0];
int secondLargest = arr[0];
System.out.println("The given array is:" );
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
secondLargest = largest; largest = arr[i];}
else if (arr[i] > secondLargest) { secondLargest = arr[i]; }
}
Output:
The given array is:
14 46 47 86 92 52 48 36 66 85
Second largest number is:86
24.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<nums.length; i++)
{
if (maxOne < nums[i])
{
maxTwo = maxOne; maxOne = nums[i];
}
else if (maxTwo < nums[i])
{
maxTwo = nums[i];
}
}
System.out.println("Largest Number: " + maxOne);
System.out.println("Second Largest Number: " + maxTwo);
}
}
Output :
Largest Number: 99
Second Largest Number: 78
Output:
}
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;
}
}
}
System.out.print("\nOdd numbers :");
for (i = 0; i <= a.length - 1; i++)
{
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
else Matrix");
{
}
} 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
{
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);
}
}
}
}
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))
{
Palindrome");
}
else
{
System.out.println("The given string is not a
} Palindrome");
System.out.println("The given string is a
}
}
Program to delete vowels in a given string
public class RemoveAllVovels {
result.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].substring(1)).appen
d (" ");
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);
}
}