[Go to site: main page, start]

0% found this document useful (0 votes)
3 views6 pages

Java Coding Problem Code

The document contains a Java program that demonstrates various operations such as reversing a string and an integer, sorting an array in both ascending and descending order, checking for palindromes, calculating factorials, swapping strings without a third variable, removing duplicates from a string, counting character frequencies, and checking if a number is prime or even/odd. Each operation is clearly printed to the console with appropriate messages. The program serves as a comprehensive example of basic Java programming techniques.

Uploaded by

overleaf44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Java Coding Problem Code

The document contains a Java program that demonstrates various operations such as reversing a string and an integer, sorting an array in both ascending and descending order, checking for palindromes, calculating factorials, swapping strings without a third variable, removing duplicates from a string, counting character frequencies, and checking if a number is prime or even/odd. Each operation is clearly printed to the console with appropriate messages. The program serves as a comprehensive example of basic Java programming techniques.

Uploaded by

overleaf44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Online Java Compiler

// Use this editor to write, compile and run your Java code online

import [Link].*;

class Main {

public static void main(String[] args) {

// Reverse a string
String name = "nitiny";

String rev = "";

for(int i = [Link]() - 1; i >=0; i--)

rev = rev + [Link](i);

[Link]("Reverse string of " + name + " is -> " + rev);

[Link]("------------------------------------------------------------");

// Reverse a integer
int ogInt = 569622;

int ogInt1 = 569622;

int revInt = 0;

while(ogInt != 0){

int dig = ogInt % 10;

revInt = revInt * 10 + dig;

ogInt = ogInt / 10;

[Link]("Reverse a Integer number " + ogInt1 + " is -> " + revInt);

[Link]("------------------------------------------------------------");
// Array in Ascending order
int[] num = {2 ,1 ,9 ,4 ,5 ,7 ,6};

for(int i = 0; i < [Link] - 1; i++){

for(int j = 0; j < [Link] - i - 1; j++){

if(num[j] > num[j+1]){

int temp = num[j+1];

num[j+1] = num[j];

num[j] = temp;

[Link]("Ascending Order -> " + [Link](num));

[Link]("------------------------------------------------------------");

// Array in Descending order


int[] num1 = {2 ,1 ,9 ,4 ,5 ,7 ,6};

for(int i = 0; i < [Link] - 1; i++){

for(int j = 0; j < [Link] - i - 1; j++){

if(num1[j] < num1[j+1])

int temp1 = num1[j];

num1[j] = num1[j+1];

num1[j+1] = temp1;

}
}

[Link]("Descending Order -> " + [Link](num1));

[Link]("------------------------------------------------------------");

// Palindrome
if([Link](name)){

[Link]("String name " + name + " is a palindrome");

else{

[Link]("String name " + name + " is not a palindrome");

[Link]("------------------------------------------------------------");

// Factorial code
int number = 11;

int fact = 1;

for(int i = 1; i <= number; i++ ){

fact = fact * i;

[Link]("Factorial of number " + number + " is " + fact);

[Link]("------------------------------------------------------------");
// Swap the string without third variable
String var1 = "Online";

String var2 = "Java";

[Link]("Swap details:");

[Link]("Initial Data: ");

[Link]("Varaible1 -> " +var1);

[Link]("Varaible2 -> " +var2);

var1 = var1 + var2;

var2 = [Link](0, [Link]() - [Link]());

var1 = [Link]([Link]());

[Link]("\t");

[Link]("After Swap details:");

[Link]("New Data: ");

[Link]("Varaible1 -> " +var1);

[Link]("Varaible2 -> " +var2);

[Link]("------------------------------------------------------------");

// Remove Duplicate from string


String str = "programming";

String result = "";

for (int i = 0; i < [Link](); i++) {

char c = [Link](i);

if ([Link](c) == -1) { // if c is not already in result

result += c;

}
[Link]("Original String: " + str);

[Link]("After Removing Duplicates: " + result);

[Link]("------------------------------------------------------------");

(Menthod 2)
String a = "programizpro";

List<Character> emptyList = new ArrayList<>();

for(int i = 0; i < [Link]() - 1; i++){

char b = [Link](i);

if(![Link](b)){

[Link](b);

for(char n : emptyList){

[Link](n);

[Link]("------------------------------------------------------------");

// Print the frequency of a char in string


Map<Character, Integer> freqMap = new LinkedHashMap<>(); // preserves order

for (char c : [Link]()) {

[Link](c, [Link](c, 0) + 1);

[Link]("\nCharacter Frequencies:");

for ([Link]<Character, Integer> entry : [Link]()) {

[Link]([Link]() + " -> " + [Link]());

}
[Link]("------------------------------------------------------------");

// Prime number
boolean flag = false;

for(int i = 2; i < number; i++){

if(number % i == 0)

flag = true;

break;

if(flag)

[Link]("Number " + number + " is not a prime number");

else

[Link]("Number " + number + " is a prime number");

[Link]("------------------------------------------------------------");

// Even n odd print


if(number % 2 == 0)

[Link]("Number " + number + " is Even number");

else

[Link]("Number " + number + " is Odd number");

You might also like