Unsolved Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

MEAN

import java.util.Scanner;

public class KboatMean


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 for Arithmetic mean");
System.out.println("Enter 2 for Geometric mean");
System.out.println("Enter 3 for Harmonic mean");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter b: ");
int b = in.nextInt();
double mean = 0.0;
switch (choice) {
case 1:
mean = (a + b) / 2.0;
System.out.println("Arithmetic mean = " + mean);
break;

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;

public class KboatCompany


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter No. of days: ");
int days = in.nextInt();
System.out.print("Enter Type('D' - Dealer & 'R' - Retailer): ");
char type = in.next().charAt(0);
System.out.print("Enter Purchase Amount: ");
double amt = in.nextDouble();
int discPercent = 0;

//Check if Customer Type is valid


if (type != 'D' && type != 'R') {
System.out.println("ERROR!!!Incorrect Customer Type.");
return;
}

if (days <= 30)


discPercent = type == 'D' ? 15 : 10;
else if (days <= 45)
discPercent = type == 'D' ? 12 : 8;
else if (days <= 60)
discPercent = type == 'D' ? 10 : 5;
else
discPercent = 0;

double disc = amt * discPercent / 100;


double billAmt = amt - disc;

System.out.println("No. of days for bill payment = " + days);


System.out.println("Customer Type = " + type);
System.out.println("Purchase Amount = " + amt);
System.out.println("Discount Amount = " + disc);
System.out.println("Bill Amount = " + billAmt);
}
}

FIBONACCI
import java.util.Scanner;

public class KboatFibonacciPrimeFactors


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 Prime Fibonacci Numbers");
System.out.println("Enter 2 Prime factors");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch (choice) {
case 1:
int a = 0, b = 1;
int term = a + b;
while (term <= 1000) {
/*
* Check if the this term of
* Fibonacci Series is Prime
* or not
*/
int c = 0;
for (int i = 1; i <= term; i++) {
if (term % i == 0) {
c++;
}
}

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;

public class KboatNumberCombinations


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
if (n < 100 || n > 999) {
System.out.println("Invalid Number");
return;
}

int digits[] = new int[3];


int t = n;
int idx = 2;
while (t != 0) {
int d = t % 10;
if (d == 0) {
System.out.println("Invalid Number");
return;
}
digits[idx--] = d;
t /= 10;
}

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


for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if (i != j && j != k && i != k)
System.out.println(digits[i]
+ "" + digits[j]
+ "" + digits[k]);
}
}
}
}
}

TRIANGULAR NUMBER
import java.util.Scanner;

public class KboatTriangularNumbers


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();

if (n < 3) {
System.out.println("Value of n should be greater than or
equal to 3");
return;
}

System.out.println("Triangular Numbers from 3 to "


+ n + ":");
int sum = 3; //First Triangular Number
for (int i = 3; sum <= n; i++) {
System.out.println(sum);
sum += i;
}
}
}

SMITH

import java.util.Scanner;

public class KboatSmithNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();

if (n <= 0) {
System.out.println(n + " is not a Smith Number.");
return;
}

boolean isComposite = false;


for (int i = 2; i < n; i++) {
if (n % i == 0) {
isComposite = true;
break;
}
}
if (isComposite && n != 1) {
int sumDigits = 0;
int t = n;
while (t != 0) {
int d = t % 10;
sumDigits += d;
t /= 10;
}

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;

public class KboatUniqueIntegers


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter m: ");
int m = in.nextInt();

if (m < 1 || m > 30000) {


System.out.println("Value of m should be between 1 and
30000");
return;
}

System.out.print("Enter n: ");
int n = in.nextInt();

if (n < 1 || n > 30000) {


System.out.println("Value of n should be between 1 and
30000");
return;
}

if (m > n) {
System.out.println("Value of m should be less than n");
return;
}

System.out.println("The Unique-Digit integers are:");


int count = 0;
for (int i = m; i <= n; i++) {
int num = i;
boolean visited[] = new boolean[10];
boolean isUnique = true;

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;

public class KboatCompositeMagicNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter m: ");
int m = in.nextInt();

System.out.print("Enter n: ");
int n = in.nextInt();

if (m < 1 || n < 1 || m > n) {


System.out.println("Invalid input");
return;
}

System.out.println("The composite magic numbers are:");


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

boolean isComposite = false;


for (int j = 2; j < i; j++) {
if (i % j == 0) {
isComposite = true;
break;
}
}

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;

public class KboatNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);

System.out.print("Enter m: ");
int m = in.nextInt();

System.out.print("Enter n: ");
int n = in.nextInt();

if (m < 100 || m > 10000 || n < 1 || n >= 100) {


System.out.println("Invalid Input");
return;
}

int number = -1, count = 0;


for (int i = m + 1; i < Integer.MAX_VALUE; i++) {
int sum = 0;
count = 0;
int t = i;
while (t != 0) {
int d = t % 10;
sum += d;
t /= 10;
count++;
}

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;

public class KboatEvilNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n = in.nextInt();
if (n < 0) {
System.out.println("Invalid Input");
return;
}

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;
}

System.out.println("Binary Equivalent: " + binNum);


System.out.println("No. of 1's: " + count);

if (count % 2 == 0)
System.out.println("Output: Evil Number");
else
System.out.println("Output: Not an Evil Number");
}
}

BOUNCY

import java.util.Scanner;

public class KboatBouncyNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();

if (n < 100) {
System.out.println(n + " is not a Bouncy Number.");
return;
}

int t = n;
boolean isIncreasing = true, isDecreasing = true;

int prev = t % 10;


while (t != 0) {
int d = t % 10;
if (d > prev) {
isIncreasing = false;
break;
}
prev = d;
t /= 10;
}
t = n;
prev = t % 10;
while (t != 0) {
int d = t % 10;
if (d < prev) {
isDecreasing = false;
break;
}
prev = d;
t /= 10;
}

if (!isIncreasing && !isDecreasing)


System.out.println(n + " is a Bouncy Number.");
else
System.out.println(n + " is not a Bouncy Number.");
}
}

PRONIC

import java.util.Scanner;

public class KboatPronicNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();

boolean isPronic = false;

for (int i = 1; i <= num - 1; i++) {


if (i * (i + 1) == num) {
isPronic = true;
break;
}
}

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;

public class KboatFascinatingNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();

if (num < 100) {


System.out.println(num + " is not a Fascinating Number");
return;
}

int num2 = num * 2;


int num3 = num * 3;

boolean isFascinating = true;


String str = "" + num + num2 + num3;
for (char i = '1'; i <= '9'; i++) {
int idx1 = str.indexOf(i);
int idx2 = str.lastIndexOf(i);
if (idx1 == -1 || idx1 != idx2) {
isFascinating = false;
break;
}
}

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;

public class KboatPermutationCombination


{
static long factorial(int num) {
int f = 1;
for (int i = 1; i <= num; i++) {
f *= i;
}
return f;
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = in.nextInt();
System.out.print("Enter the value of r: ");
int r = in.nextInt();
int p = (int)(factorial(n) / factorial(n - r));
int c = (int)(factorial(n)
/ (factorial(n - r) * factorial(r)));
System.out.println("Permutation = " + p);
System.out.println("Combination = " + c);
}
}

TWIN PRIME

import java.util.Scanner;

public class KboatTwinPrime


{
static boolean primeCheck(int num) {
boolean isPrime = true;
if (num <= 1)
return false;

for (int i = 2; i <= num / 2; i++) {


if (num % i == 0) {
isPrime = false;
break;
}
}

return isPrime;
}

static void printResult(int n, int p1, int p2) {


System.out.println("Number read in is "
+ n + "\tp1 = "
+ p1 + ", p2 = "
+ p2);
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();

/*
* 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;
}

boolean res3 = primeCheck(n + 2);


if (res3) {
printResult(n, n, n + 2);
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);
}
}
}

You might also like