Unsolved Programs
Unsolved Programs
Unsolved Programs
import java.util.Scanner;
case 2:
mean = Math.sqrt(a * b);
System.out.println("Geometric mean = " + mean);
break;
case 3:
mean = 2.0 * a * b / (a + b);
System.out.println("Harmonic mean = " + mean);
break;
default:
System.out.println("Incorrect Choice!!!");
}
}
}
A COMPANY
import java.util.Scanner;
FIBONACCI
import java.util.Scanner;
if (c == 2)
System.out.print(term + " ");
a = b;
b = term;
term = a + b;
}
break;
case 2:
System.out.print("Enter Number: ");
int num = in.nextInt();
System.out.print("The prime factors of " + num + " are: ");
for(int i = 2; i < num; i++) {
while (num % i == 0) {
System.out.print(i + " ");
num = num / i;
}
}
if(num > 2) {
System.out.println(num);
}
break;
default:
System.out.println("Incorrect Choice!!!");
}
}
}
NATURAL NUMBER
import java.util.Scanner;
TRIANGULAR NUMBER
import java.util.Scanner;
if (n < 3) {
System.out.println("Value of n should be greater than or
equal to 3");
return;
}
SMITH
import java.util.Scanner;
if (n <= 0) {
System.out.println(n + " is not a Smith Number.");
return;
}
int sumPrimeDigits = 0;
t = n;
for(int i = 2; i < t; i++) {
while(t % i == 0) {
t /= i;
int temp = i;
while (temp != 0) {
int d = temp % 10;
sumPrimeDigits += d;
temp /= 10;
}
}
}
if(t > 2) {
while (t != 0) {
int d = t % 10;
sumPrimeDigits += d;
t /= 10;
}
}
if (sumPrimeDigits == sumDigits)
System.out.println(n + " is a Smith Number.");
else
System.out.println(n + " is not a Smith Number.");
}
else {
System.out.println(n + " is not a Smith Number.");
}
}
}
UNIQUE
import java.util.Scanner;
System.out.print("Enter n: ");
int n = in.nextInt();
if (m > n) {
System.out.println("Value of m should be less than n");
return;
}
while (num != 0) {
int d = num % 10;
if (visited[d]) {
isUnique = false;
break;
}
visited[d] = true;
num /= 10;
}
if (isUnique) {
count++;
System.out.print(i + " ");
}
}
System.out.println();
System.out.println("Frequency of unique-digit integers is: " +
count);
}
}
MAGIC
import java.util.Scanner;
System.out.print("Enter n: ");
int n = in.nextInt();
if (isComposite && i != 1) {
int num = i;
while (num > 9) {
int sum = 0;
while (num != 0) {
int d = num % 10;
num /= 10;
sum += d;
}
num = sum;
}
if (num == 1) {
count++;
System.out.print(i + " ");
}
}
}
System.out.println();
System.out.println("Frequency of composite magic numbers: " +
count);
}
}
POSITIVE NUMBER
import java.util.Scanner;
System.out.print("Enter m: ");
int m = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
if (sum == n) {
number = i;
break;
}
}
if (number == -1) {
System.out.println("Required number not found");
}
else {
System.out.println("The required number = " + number);
System.out.println("Total number of digits = " + count);
}
}
}
EVIL
import java.util.Scanner;
int count = 0;
int p = 0;
int binNum = 0;
while (n > 0) {
int d = n % 2;
if (d == 1)
count++;
binNum += (int)(d * Math.pow(10, p));
p++;
n /= 2;
}
if (count % 2 == 0)
System.out.println("Output: Evil Number");
else
System.out.println("Output: Not an Evil Number");
}
}
BOUNCY
import java.util.Scanner;
if (n < 100) {
System.out.println(n + " is not a Bouncy Number.");
return;
}
int t = n;
boolean isIncreasing = true, isDecreasing = true;
PRONIC
import java.util.Scanner;
if (isPronic)
System.out.println(num + " is a pronic number");
else
System.out.println(num + " is not a pronic number");
}
}
FASCINATING
import java.util.Scanner;
if (isFascinating)
System.out.println(num + " is a Fascinating Number");
else
System.out.println(num + " is not a Fascinating Number");
}
}
FACTORIAL
import java.util.Scanner;
TWIN PRIME
import java.util.Scanner;
return isPrime;
}
/*
* First check if the number itself is prime
* and forms a twin prime pair with a number
* at distance 2 on either side. For example,
* if n is 13 then (11, 13) forms a twin
* prime pair at least distance from 13
*/
boolean res1 = primeCheck(n);
if (res1) {
boolean res2 = primeCheck(n - 2);
if (res2) {
printResult(n, n - 2, n);
return;
}
/*
* After that check if the number falls between
* a twin prime pair. For example, if n is 12
* then twin prime pair at least distance is
* (11, 13)
*/
if (primeCheck(n - 1) && primeCheck(n + 1)) {
printResult(n, n - 1, n + 1);
return;
}
/*
* Find a twin prime pair towards left
*/
int l1 = 0, l2 = 0;
for (int i = n - 1; i >= 5; i--) {
if (primeCheck(i) && primeCheck(i - 2)) {
l1 = i - 2;
l2 = i;
break;
}
}
/*
* Find a twin prime pair towards right
*/
int r1 = 0, r2 = 0;
for (int i = n + 1; i < Integer.MAX_VALUE - 2; i++) {
if (primeCheck(i) && primeCheck(i + 2)) {
r1 = i;
r2 = i + 2;
break;
}
}
/*
* Find the twin prime pair with minimum distance
*/
if (l1 != 0 && l2 != 0 && r1 != 0 && r2 != 0) {
int lDist = Math.min(Math.abs(n - l1), Math.abs(n - l2));
int rDist = Math.min(Math.abs(n - r1), Math.abs(n - r2));
if (lDist < rDist)
printResult(n, l1, l2);
else
printResult(n, r1, r2);
}
else if (l1 != 0 && l2 != 0) {
printResult(n, l1, l2);
}
else if (r1 != 0 && r2 != 0) {
printResult(n, r1, r2);
}
}
}