0% found this document useful (0 votes)
4 views12 pages

All Java Programs

The document contains multiple Java programs that perform various tasks such as finding the largest palindrome from the product of three-digit numbers, generating Fibonacci numbers, converting decimal to binary, checking for palindromes and perfect squares, summing prime numbers, and counting vowels and consonants in a string. Each program is self-contained with a main method and relevant helper methods. The programs demonstrate fundamental programming concepts including loops, conditionals, and array manipulation.

Uploaded by

swetharaghu4311
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)
4 views12 pages

All Java Programs

The document contains multiple Java programs that perform various tasks such as finding the largest palindrome from the product of three-digit numbers, generating Fibonacci numbers, converting decimal to binary, checking for palindromes and perfect squares, summing prime numbers, and counting vowels and consonants in a string. Each program is self-contained with a main method and relevant helper methods. The programs demonstrate fundamental programming concepts including loops, conditionals, and array manipulation.

Uploaded by

swetharaghu4311
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

LargestPalindrome.

java
public class LargestPalindrome {
public static void main(String[] args) {
int max = 0;

for (int i = 100; i <= 999; i++) {


for (int j = 100; j <= 999; j++) {
int num = i * j;
if (isPalindrome(num) && num > max) {
max = num;
}
}
}

[Link]("Largest Palindrome: " + max);


}

static boolean isPalindrome(int n) {


int temp = n, rev = 0;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
return n == rev;
}
}
[Link]
public class FibonacciReverse {
public static void main(String[] args) {
int n = 10;
int[] arr = new int[n];

int a = 0, b = 1, c;

for (int i = 0; i < n; i++) {


arr[i] = a;
c = a + b;
a = b;
b = c;
}

for (int i = n - 1; i >= 0; i--) {


[Link](arr[i] + " ");
}
}
}
[Link]
public class DecimalToBinaryAndCount {
public static void main(String[] args) {
int n = 19;
int[] bin = new int[32];
int i = 0, ones = 0;

while (n > 0) {
int bit = n % 2;
bin[i++] = bit;
if (bit == 1) ones++;
n /= 2;
}

[Link]("Binary: ");
for (int j = i - 1; j >= 0; j--) {
[Link](bin[j]);
}
[Link]("\n1s count: " + ones);
}
}
[Link]
public class PalindromeAndPerfectSquare {
public static void main(String[] args) {
int n = 121;

boolean isPal = isPalindrome(n);


boolean isSquare = isPerfectSquare(n);

if (isPal && isSquare) [Link]("Both");


else [Link]("Not both");
}

static boolean isPalindrome(int n) {


int temp = n, rev = 0;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
return n == rev;
}

static boolean isPerfectSquare(int n) {


for (int i = 1; i * i <= n; i++)
if (i * i == n) return true;
return false;
}
}
[Link]
public class PrimeSum {
public static void main(String[] args) {
int n = 50;
int sum = 0;

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


if (isPrime(i)) {
[Link](i + " ");
sum += i;
}
}

[Link]("\nSum = " + sum);


}

static boolean isPrime(int n) {


if (n < 2) return false;
for (int i = 2; i <= n / 2; i++)
if (n % i == 0) return false;
return true;
}
}
[Link]
public class CircularPrime {
public static void main(String[] args) {
int n = 197;

if (isCircularPrime(n)) [Link]("Circular Prime");


else [Link]("Not Circular Prime");
}

static boolean isCircularPrime(int n) {


int digits = countDigits(n);
int num = n;

for (int i = 0; i < digits; i++) {


if (!isPrime(num)) return false;
num = rotate(num, digits);
}
return true;
}

static int countDigits(int n) {


int c = 0;
while (n > 0) { n /= 10; c++; }
return c;
}

static int rotate(int n, int digits) {


int lastDigit = n % 10;
n /= 10;

int mul = 1;
for (int i = 1; i < digits; i++)
mul *= 10;

return lastDigit * mul + n;


}

static boolean isPrime(int n) {


if (n < 2) return false;
for (int i = 2; i <= n / 2; i++)
if (n % i == 0) return false;
return true;
}
}
[Link]
public class BalancedParentheses {
public static void main(String[] args) {
String s = "(()())";
int count = 0;
boolean balanced = true;

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


char ch = [Link](i);

if (ch == '(') count++;


else if (ch == ')') count--;

if (count < 0) {
balanced = false;
break;
}
}

if (count != 0) balanced = false;

[Link](balanced ? "Balanced" : "Not Balanced");


}
}
[Link]
public class UniqueCharacters {
public static void main(String[] args) {
String s = "banana";
int[] freq = new int[256];
int count = 0;

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


freq[[Link](i)]++;

for (int i = 0; i < 256; i++)


if (freq[i] == 1)
count++;

[Link]("Unique = " + count);


}
}
[Link]
public class NumberToWords {
public static void main(String[] args) {
int n = 1234;
convert(n);
}

static void convert(int n) {


String[] ones = {"","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
String[] tens = {"","Ten ","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","
String[] teens = {"Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ",
"Sixteen ","Seventeen ","Eighteen ","Nineteen "};

if (n >= 1000) {
[Link](ones[n / 1000] + "Thousand ");
n %= 1000;
}
if (n >= 100) {
[Link](ones[n / 100] + "Hundred ");
n %= 100;
}
if (n >= 20) {
[Link](tens[n / 10]);
n %= 10;
} else if (n >= 10) {
[Link](teens[n - 10]);
return;
}
if (n > 0) [Link](ones[n]);
}
}
[Link]
public class Sum10FibonacciFromN {
public static void main(String[] args) {
int n = 5;
int a = 0, b = 1, c;

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


c = a + b;
a = b;
b = c;
}

int sum = 0;

for (int i = 0; i < 10; i++) {


sum += a;
c = a + b;
a = b;
b = c;
}

[Link]("Sum = " + sum);


}
}
[Link]
public class PerfectSquareAndCube {
public static void main(String[] args) {
int n = 64;
boolean square = false, cube = false;

for (int i = 1; i * i <= n; i++)


if (i * i == n)
square = true;

for (int i = 1; i * i * i <= n; i++)


if (i * i * i == n)
cube = true;

[Link](square && cube ? "Both" : "No");


}
}
[Link]
public class VowelConsonantCount {
public static void main(String[] args) {
String s = "hello world";
int v = 0, c = 0;

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


char ch = [Link](i);

if (ch >= 'A' && ch <= 'Z') ch += 32;

if (ch >= 'a' && ch <= 'z') {


if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
v++;
else
c++;
}
}

[Link]("Vowels = " + v);


[Link]("Consonants = " + c);
}
}

You might also like