Instagram - Java Programs
Instagram - Java Programs
publicclass OddEven {
public static void main(String[] args) {
if(number % 2 == 0) {
[Link](number + " is even.");
}else {
[Link](number + " is odd.");
}
}
}
publicstaticvoidmain(String[]args){
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
if (isPrime(number)) {
[Link](number+"isaprimenumber.");
} else {
[Link](number+"isnotaprime number.");
}
}
By Achal Singh
TheAutomationEngineer
Output: 0 1 1 2 3 5 8
publicclassSwapNumbers {
publicstaticvoid main(String[] args) {
Scannerscanner=new Scanner([Link]);
[Link]("Enter the first number: ");
int a = 5,
[Link]("Enter the second number: ");
int b = 10;
[Link]("Before swapping: a = " + a + ", b = " + b);
a=a+b;
b=a-b;
a=a-b;
[Link]("After swapping: a = " + a + ", b = " + b);
}
}
By Achal Singh
TheAutomationEngineer
}
}
Input: 5!
Output:5!=5*4*3*2*1 = 120
}
}
Input:15786
Output: 68751
By Achal Singh
TheAutomationEngineer
intno = 0, a = 0;
Scanner scanner = new Scanner([Link]);
[Link]("Enter any number : ");
no = [Link]();
if(no<0)
{
no = no * -1;
} else if (no==0) {
no=1;
}
while(no>0)
{
no=no/10;
a++;}
[Link]("Number of digits in given number is :" +a);}
By Achal Singh
TheAutomationEngineer
import [Link];
if (isPalindrome(number)) {
[Link](number+"isapalindrome.");
} else {
[Link](number+"isnotapalindrome.");
}
}
}
}
1001 is a palindrome.
By Achal Singh
TheAutomationEngineer
[Link]("Sumofdigitsof"+number+"is: " +
sumOfDigits);
}
Output:
Sumofdigitsof12345 is: 15
By Achal Singh
TheAutomationEngineer
Strings
1.) Javaprogram to reverse a string
import [Link];
public class Test {
publicstaticvoidmain(String[] args) {
Scannerscanner=newScanner([Link]);
[Link]("Enterastring: ");
Stringinput=[Link]();
char ch;
String nstr = "";
for(inti=0;i<[Link](); i++) {
ch = [Link](i);
nstr = ch + nstr;
}
[Link]("ReversedString is : " + nstr);
[Link](inputString);
[Link](reverseString);
}
By Achal Singh
TheAutomationEngineer
HashMap<Character,Integer>charCountMap=newHashMap<>();
char[] strArray = [Link]();
for (char c : strArray) {
if ([Link](c)) {
[Link](c, [Link](c) + 1);
} else {
[Link](c, 1);
}
}
a : 4 g : 2 m : 2 n : 2 r : 3
By Achal Singh
TheAutomationEngineer
[Link](s,1);
}
[Link]("Count of Characters in a given string:"+
charCountMap);
}
}
CountofCharacters in a given string : {Java=1, Automation=2,Test=1}
By Achal Singh
TheAutomationEngineer
import [Link];
staticvoidpermute(Stringstr,String prefix) {
if ([Link]() == 0) {
[Link](prefix);
} else {
for(inti=0;i<[Link](); i++) {
Stringrem=[Link](0,i) + [Link](i+1);
permute(rem,prefix+[Link](i));
}
}
}
}
abc
acb
bac
bca
cab
cba
By Achal Singh
TheAutomationEngineer
[Link];
staticbooleanisPalindrome(String str) {
int start = 0;
intend=[Link]() - 1;
while(start<end) {
if([Link](start) != [Link](end)){
returnfalse;
}
start++;
end--;
}
return true;
}
}
By Achal Singh
TheAutomationEngineer
publicstaticvoidmain(String[] args) {
String str1 = "listen";
String str2 = "silent";
[Link](areAnagrams(str1,str2));
}
charCount[[Link](i)]++;
charCount[[Link](i)]--;
}
for(intcount:charCount)
{
if ( count !=0 )
{ }
return false;
}
return true;
}
}
By Achal Singh
TheAutomationEngineer
Vowels : 3
Consonants : 7
By Achal Singh
TheAutomationEngineer
import [Link];
publicstaticvoidprintUniqueCharacters(String str) {
//AssumeASCIIcharacters(0-127),useboolean array to track
character occurrences
boolean[] unique = new boolean[128];
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (!unique[ch]) {
unique[ch] = true;
[Link](ch + " ");
}
}
}
}
Enterastring:JavaAutomation
Uniquecharactersin"Java Automation":
Jav Automin
By Achal Singh
TheAutomationEngineer
[Link];
publicstaticvoidprintEvenIndexedCharacters(String str){
for(inti=0;i<[Link](); i++) {
if(i%2==0){
[Link]([Link](i));
}
}
}
}
Evenindexedcharactersin"Automation":
Atmto
By Achal Singh
TheAutomationEngineer
import [Link];
StringstringWithoutSpaces=removeSpaces(input);
[Link]("String without spaces: " +
stringWithoutSpaces);
}
public static String removeSpaces(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < [Link](); i++) {
if ([Link](i) != ' ') {
[Link]([Link](i));
}
}
return [Link]();
}
}
By Achal Singh
TheAutomationEngineer
import [Link];
publicstaticvoidmain(String[]args) {
Scannerscanner=newScanner([Link]);
[Link]("Enterastring: ");
Stringinput=[Link]();
StringdoubledString=doubleCharacters(input);
[Link]("Doubledcharacters: " + doubledString);
}
publicstaticStringdoubleCharacters(String str) {
StringBuilderdoubled=newStringBuilder();
for(inti=0;i<[Link](); i++) {
char ch = [Link](i);
[Link](ch).append(ch); // Append each character
twice
}
return [Link]();
}
}
By Achal Singh
TheAutomationEngineer
By Achal Singh
TheAutomationEngineer
import [Link];
return [Link]();
}
}
Output: a2b2c3d2
By Achal Singh
TheAutomationEngineer
import [Link];
[Link](ch);
}
[Link]("Outputinlowercase:"+lowerCase);
[Link]("Outputinuppercase"+upperCase);
}
By Achal Singh
TheAutomationEngineer
import [Link];
[Link](ch);
[Link]("OutputinAlpha:"+[Link]());
[Link]("Output in Numeric:
"+[Link]());
}
By Achal Singh
TheAutomationEngineer
By Achal Singh
TheAutomationEngineer
import [Link];
publicstaticintlengthOfLongestSubstring(String s) {
HashSet<Character>set=newHashSet<>();
int maxLength = 0;
int start = 0;
int end = 0;
return maxLength;
}
}
By Achal Singh
TheAutomationEngineer
Arrays
1.) Find common elements between
two arrays
import [Link];
import [Link];
publicclassCommonElements {
publicstaticvoid main(String[] args) {
int[]array1 = {1, 2, 3, 4, 5};
int[]array2 = {4, 5, 6, 7, 8};
Set<Integer> commonElements = findCommonElements(array1,
array2);
By Achal Singh
TheAutomationEngineer
[Link];
publicclass Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Date");
[Link]("Elderberry");
if(![Link]()) {
Output:
First element: Apple
Last element: Elderberry
By Achal Singh
TheAutomationEngineer
[Link]("Sorted array:");
for (int num : array) {
[Link](num + " ");
}
}
Output:
Sorted array:
12569
By Achal Singh
TheAutomationEngineer
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 6, 2, 5};
int[]uniqueArray=removeDuplicates(array);
[Link]("Arraywithduplicates removed:");
for (int num : uniqueArray) {
[Link](num + " ");
}
}
publicstaticint[]removeDuplicates(int[]array) {
Set<Integer> set = new HashSet<>();
for (int num : array) {
[Link](num);
}
return result;
}
}
Output:
Array with duplicates removed:
12569
By Achal Singh
TheAutomationEngineer
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
ArrayList<Integer>arrayList=newArrayList<>();
[Link](5);
[Link](2);
[Link](9);
[Link](1);
[Link](6);
[Link](2);
[Link](5);
ArrayList<Integer> uniqueList =
removeDuplicates(arrayList);
[Link]("ArrayListwithduplicates
removed:");
for (int num : uniqueList) {
[Link](num + " ");
}
}
public static ArrayList<Integer>
removeDuplicates(ArrayList<Integer> list) {
Set<Integer>set=newHashSet<>(list);
return new ArrayList<>(set);
}
}
Output:
ArrayList with duplicates removed:
12569
By Achal Singh
TheAutomationEngineer
publicstaticintfindMissingNumber(int[] array) {
intn=[Link]+1;//Since one number is missing, the length
should be n+1
inttotalSum=n*(n+1)/2; // Sum of first n natural numbers
int arraySum = 0;
for (int num : array) {
arraySum += num;
}
return totalSum - arraySum;
}
}
Output:
The missing number is: 3
By Achal Singh
TheAutomationEngineer
int[]result=findLargestAndSmallest(array);
publicstaticint[]findLargestAndSmallest(int[] array) {
if(array==null||[Link] == 0) {
thrownewIllegalArgumentException("Array must not be null or
empty");
}
Output:
Smallest element: 1
Largest element: 9
By Achal Singh
TheAutomationEngineer
intindex=linearSearch(array, target);
if (index != -1) {
[Link]("Element " + target + " found at index:"+
index);
} else {
[Link]("Element " + target + " not found in the
array.");
}
}
publicstaticintlinearSearch(int[] array, int target) {
for(inti=0;i<[Link]; i++) {
if(array[i]==target) {
returni;//Element found, return index
}
}
return-1;//Elementnot found
}
}
Output:
Element 6 found at index: 4
Element 10 not found in the array
By Achal Singh
TheAutomationEngineer
Output:
Sum of integers in the array: 26
By Achal Singh
TheAutomationEngineer
Output:
Minimum value in the array: 1
Maximum value in the array: 9
By Achal Singh
TheAutomationEngineer
[Link]("Evennumberscount:"+count[1]);
[Link]("Oddnumberscount:"+count[0]);
}
Output:
Even numbers count:4
By Achal Singh
TheAutomationEngineer
array) {
repeated)
List<Integer> nonRepeatedElements = new ArrayList<>();
for ([Link]<Integer, Integer> entry :
[Link]()) {
if ([Link]() == 1) {
[Link]([Link]());
}
}
return nonRepeatedElements;
}
}
Output :
Non-repeated elements: [3, 4]
By Achal Singh
TheAutomationEngineer
// Constructor
publicStudent(intid,String name) {
[Link] = id;
[Link] = name;
}
// hashCode method
@Override
public int hashCode() {
[Link](id, name);
}
// equals method
@Override
publicbooleanequals(Object obj) {
if (this == obj)
return true;
if(obj==null||getClass() != [Link]())
return false;
Studentstudent=(Student) obj;
returnid==[Link] && [Link](name, [Link]);
}
publicstaticvoidmain(String[] args) {
//Creatingobjectsof Student class
Studentstudent1=new Student(1, "Alice");
Studentstudent2=new Student(2, "Bob");
Studentstudent3=new Student(1, "Alice");
//Testingequalsmethod
[Link]("[Link](student2): " +
[Link](student2));// Output: false
[Link]("[Link](student3): " +
[Link](student3));// Output: true
//TestinghashCodemethod
[Link]("Hashcode of student1: " + [Link]());
[Link]("Hashcode of student2: " + [Link]());
[Link]("Hashcode of student3: " + [Link]());
}
}
By Achal Singh