Printing max and min value , check program - java

I've been trying to write a program to print the min and max value of 5 integer type variables, using no arrays and only swapping using this technique
if (x > y) {
int tmp = x;
x = y;
y = tmp;
}
now this is what I came up with ( although I know it's possible with only 6 swaps however I can't figure it out):
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
//storing max value in a all the way, and min value in b
if (a < b) {
int tmp = a;
a = b;
b = tmp;
}
if (a < c) {
int tmp = a;
a = c;
c = tmp;
}
if (a < d) {
int tmp = a;
a = d;
d = tmp;
}
if (a < e) {
int tmp = a;
a = e;
e = tmp;
}
if (c < b) {
int tmp = b;
b = c;
c = tmp;
}
if (d < b) {
int tmp = b;
b = d;
d = tmp;
}
if (e < b) {
int tmp = b;
b = e;
e = tmp;
}
System.out.println(b +"\n" + a);
Now this should work, but I also needed to write a code to make sure the program works fine, so I could check all the combinations with 0's and 1's in them.
i.e 2^5=32 combinations. and if one of them fails print it.
I've tried to do this :
for (int a = 0; a <= 1; a++) {
for(int b = 0; b <= 1; b++) {
for(int c = 0; c <= 1; c++) {
for(int d = 0; d <= 1; d++) {
for(int e = 0; e <= 1; e++) {
if (a < b){
int tmp = a;
a = b;
b = tmp;
}
if (a < c) {
int tmp = a;
a = c;
c = tmp;
}
if (a < d) {
int tmp = a;
a = d;
d = tmp;
}
if (a < e) {
int tmp = a;
a = e;
e = tmp;
}
if (c < b) {
int tmp = b;
b = c;
c = tmp;
}
if (d < b) {
int tmp = b;
b = d;
d = tmp;
}
if (e < b) {
int tmp = b;
b = e;
e = tmp;
}
System.out.println(b + "\n" + a);
}
}
}
}
}
but this doesn't work because once there is a "1" in any variable a gets it and then I'm not gonna be able to run the combinations properly..
****basically this is a tester for the code within the loop, so it should test 0000-11111 and print something like verified if all works well, or print the combination for which it got the wrong result.. however I can't see how it's gonna do that .****
any suggestions? no arrays , swapping only...
thanks ahead

I would use Math.min(int, int) and Math.max(int, int) and a long chain of invocations. Like,
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Math.min(Math.min(Math.min(Math.min(a, b), c), d), e);
int max = Math.max(Math.max(Math.max(Math.max(a, b), c), d), e);
Your "swapping piece of code" makes no sense. Here is another implementation
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
min = (a < min) ? a : min;
min = (b < min) ? b : min;
min = (c < min) ? c : min;
min = (d < min) ? d : min;
min = (e < min) ? e : min;
max = (a > max) ? a : max;
max = (b > max) ? b : max;
max = (c > max) ? c : max;
max = (d > max) ? d : max;
max = (e > max) ? e : max;

Why don't you store the a,b,c,d,e values in the innermost loop and reassign it back to the variables again after your swapping piece of code? Like so,
public static void main(String args[]) {
for (int a=0 ;a<=1 ;a++ ){
for(int b=0 ;b<=1 ;b++ ){
for(int c=0 ;c<=1 ;c++ ){
for(int d=0 ;d<=1 ;d++ ){
for(int e=0 ; e<=1 ;e++ ){
// Store the variable values
int ap = a;
int bp = b;
int cp =c;
int dp = d;
int ep = e;
// Swapping logic for finding min and max
// Due to swapping, the values of a,b,c,d,e may change
if ( a < b){
int tmp = a;
a=b;
b=tmp;
}
if ( a < c){
int tmp = a;
a=c;
c=tmp;
}
if ( a < d){
int tmp = a;
a=d;
d=tmp;
}
if ( a < e){
int tmp = a;
a=e;
e=tmp;
}
if ( c < b){
int tmp = b;
b=c;
c=tmp;
}
if ( d < b){
int tmp = b;
b=d;
d=tmp;
}
if ( e < b){
int tmp = b;
b=e;
e=tmp;
}
System.out.print(a+""+b+""+c+""+d+""+e+" ");
System.out.println(b + " " + a);
// Reassign the variable values
a = ap;
b = bp;
c=cp;
d=dp;
e=ep;
}
}
}
}
}
}

Related

java code to find the biggest and smallest numbers out of 5 (not from array) using only 6 steps, each step you need to swap between 2 numbers

I want to use the swapping between 2 numbers format in order to find the biggest and smallest numbers out of the 5 given by user.
I want to use only 6 steps but I reached each time to more than that. any suggestions to how to fix my code?
Scanner myScanner = new Scanner(System.in);
int a = myScanner.nextInt();
int b = myScanner.nextInt();
int c = myScanner.nextInt();
int d = myScanner.nextInt();
int e = myScanner.nextInt();
int tmp;
if (a > b) {
tmp = b;
b = a;
a = tmp;
}
if (c > d) {
tmp = c;
c = d;
d = tmp;
}
if (a > c) {
tmp = a;
a = c;
c = tmp;
}
if (b > d) {
tmp = b;
b = d;
d = tmp;
}
if (d > e) {
tmp = d;
d = e;
e = tmp;
}
if (c > e) {
tmp = c;
c = e;
e = tmp;
}
if (b > e) {
tmp = b;
b = e;
e = tmp;
}
System.out.println(a);
System.out.print(e);
You have a duplicated code fragment. This will work if u remove the redundant code.
import java.util.Scanner;
public class UshtrimeRekursive {
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
int a = myScanner.nextInt();
int b = myScanner.nextInt();
int c = myScanner.nextInt();
int d = myScanner.nextInt();
int e = myScanner.nextInt();
int tmp;
if (a > b) {
tmp = b;
b = a;
a = tmp;
}
if (c > d) {
tmp = c;
c = d;
d = tmp;
}
if (a > c) {
tmp = a;
a = c;
c = tmp;
}
// if (b > d) {
// tmp = b;
// b = d;
// d = tmp;
// }
if (d > e) {
tmp = d;
d = e;
e = tmp;
}
if (c > e) {
tmp = c;
c = e;
e = tmp;
}
if (b > e) {
tmp = b;
b = e;
e = tmp;
}
System.out.println(a);
System.out.print(e);
}
}
I am not sure if the following code could be accepted as meeting the requirement to the number of "steps" because it contains 7 swap operations but only 3 or 4 swaps will actually be take place. Also, the swap is implemented without a temporary variable with XOR-based one-liner:
Random random = new Random();
// generate random values instead of user input
int a = random.nextInt(1000);
int b = random.nextInt(1000);
int c = random.nextInt(1000);
int d = random.nextInt(1000);
int e = random.nextInt(1000);
System.out.println(Arrays.asList(a, b, c, d, e));
if (e < a) {
a = (e ^= a ^= e) ^ a;
}
if (b < a) {
a = (b ^= a ^= b) ^ a;
} else if (b > e) {
b = (e ^= b ^= e) ^ b;
}
if (c < a) {
a = (c ^= a ^= c) ^ a;
} else if (c > e) {
c = (e ^= c ^= e) ^ c;
}
if (d < a) {
a = (d ^= a ^= d) ^ a;
} else if (d > e) {
d = (e ^= d ^= e) ^ d;
}
System.out.println("min = " + a);
System.out.println("max = " + e);
System.out.println(Arrays.asList(a, b, c, d, e));
Example output
[629, 941, 339, 496, 366]
min = 339
max = 941
[339, 629, 366, 496, 941]

How to assign a long variable to an output in the previous command in Java?

I started the code like this and
public static long lcm_of_array_elements(int[] element_array) {
long lcm_of_array_elements = 1;
....
if (counter == element_array.length) {
return lcm_of_array_elements;
In my driver code:
int[] element_array = { a, b, c, d };
System.out.println(lcm_of_array_elements(element_array));
Here, I was getting the right value but I want to assign some variable to this value to perform some operations. I tried the following:
long val=lcm_of_array_elements(element_array) ;
However this only returned the initially defined value : 1.
So I tried, this but it displayed an error:
long val=lcm_of_array_elements(element_array).Last.val ;
Could I please get a command to store the value in some variable?
Let us take values: a=2,b=3,c=4,d=5
System.out.println(lcm_of_array_elements(element_array));
System.out.println(lcm_of_array_elements(element_array));
long k=lcm_of_array_elements(element_array) ;
System.out.println(k);
My output is:
60
1
But I need it to be:
60
60
Here is the full code:
// To check if the LCM of 4 numbers is divisible by their sum
public static long lcm_of_array_elements(int[] element_array)
{
long lcm_of_array_elements = 1;
int divisor = 2;
while (true) {
int counter = 0;
boolean divisible = false;
for (int i = 0; i < element_array.length; i++) {
if (element_array[i] == 0) {
return 0;
}
else if (element_array[i] < 0) {
element_array[i] = element_array[i] * (-1);
}
if (element_array[i] == 1) {
counter++;
}
if (element_array[i] % divisor == 0) {
divisible = true;
element_array[i] = element_array[i] / divisor;
}
}
if (divisible) {
lcm_of_array_elements = lcm_of_array_elements * divisor;
}
else {
divisor ++;
}
if (counter == element_array.length) {
return lcm_of_array_elements;
}
}
}
// Driver Code
public static void main(String[] args)
{
Random rand = new Random() ;
int f = 4;
while(f == 4){
int a = rand.nextInt(100) ;
int b = rand.nextInt(100) ;
int c = rand.nextInt(100) ;
int d = rand.nextInt(100) ;
System.out.println(a) ;
System.out.println(b) ;
System.out.println(c) ;
System.out.println(d) ;
int e = (a + b + c + d) ;
int[] element_array = { a, b, c, d };
System.out.println(e) ;
System.out.println(lcm_of_array_elements(element_array));
long k = lcm_of_array_elements(element_array) ;
System.out.println(k) ;
Scanner sc = new Scanner(System.in) ;
int y = sc.nextInt();
if(y%e==0){
System.out.println("SUCCESS");
f = f + 1 ;
}
else{
System.out.println("FAIL");
}
}
}
}
Try
import java.util.Random;
import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Random rand = new Random ();
int f = 4;
while (f == 4)
{
int a = rand.nextInt (100);
int b = rand.nextInt (100);
int c = rand.nextInt (100);
int d = rand.nextInt (100);
System.out.println (a);
System.out.println (b);
System.out.println (c);
System.out.println (d);
int e = (a + b + c + d);
int[] element_array = { a, b, c, d };
System.out.println (e);
//System.out.println (lcm_of_array_elements (element_array));
long k = lcm_of_array_elements (element_array);
System.out.println ("K: "+k);
Scanner sc = new Scanner (System.in);
int y = sc.nextInt ();
if (y % e == 0)
{
System.out.println ("SUCCESS");
f = f + 1;
}
else
{
System.out.println ("FAIL");
}
}
}
public static long lcm_of_array_elements (int[]element_array)
{
long lcm_of_array_elements = 1;
int divisor = 2;
long c=1;
while (true)
{
int counter = 0;
boolean divisible = false;
for (int i = 0; i < element_array.length; i++)
{
if (element_array[i] == 0)
{
return 0;
}
else if (element_array[i] < 0)
{
element_array[i] = element_array[i] * (-1);
}
if (element_array[i] == 1)
{
counter++;
}
if (element_array[i] % divisor == 0)
{
divisible = true;
element_array[i] = element_array[i] / divisor;
}
}
if (divisible)
{
c = c * divisor;
}
else
{
divisor++;
}
if (counter == element_array.length)
{
return c;
}
}
}
}

Customized diamond shape using while loop & nested for-loop (java)

My program compiles and runs but my spacing for "Enter a size..." is incorrect. Would like some help with spacing, and System.out.println formatting in the loop for my program to have proper spacing as intended.
import java.util.Scanner;
public class PrintCustomizedDiamond {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter a letter:");
char ch = s.next().charAt(0);
int a = 0, b, c, d;
d = a - 1;
while (a < 6 || a % 2 == 1) {
System.out.print("Enter a size (even number no less than 6): ");
a = s.nextInt();
if (a % 2 == 0) {
System.out.print("");
}
else {
System.out.println("");
}
}
d = (a / 2) - 1;
for (b = 2; b <= a; b = b + 2) {
for (c = 0; c < d; c++) {
System.out.print(" ");
}
for (c = 0; c < b; c++) {
System.out.print(ch);
}
d = d - 1;
System.out.println();
}
d = 0;
for (b = a; b >= 2; b = b -2) {
for (c = d; c > 0; c--)
{
System.out.print(" ");
}
for (c = 0; c < b; c++)
{
System.out.print(ch);
}
d = d + 1;
System.out.println();
}
}
}

design a method in java receives 2 D array and manipulates each column in it

I am trying to design a method in java which receive 2 D array and manipulate each column in it. then, save this manipulation whereas each column will be represented as a one value in the return array. My problem is that, I do not know, how can I read every column individuall. Could you suggest me please.
int[] ibraMethod(int[][] InputArray){
int[] Array_After_ibraMethod = new int[24];
int APoints = 0;
int BPoints = 0;
int CPoints = 0;
for(int h = 1; h < 24 ; h++){
System.out.println("\n Hour"+ h);
for(int i = 0; i<InputArray.length; i++){
for(int j = 0; j<InputArray[h].length; j++){
if(InputArray[i][j]==1){
APoints = APoints +3;
}
else if(InputArray[i][j]==-1){
BPoints = BPoints +2;
}
else if(InputArray[i][j]==0){
CPoints = CPoints +1;
}
}
}
Array_After_ibraMethod[h] = Compare(APoints, BPoints, CPoints);
}
/// to print////
System.out.println("\n After ibraMethod");
for (Integer i :Array_After_ibraMethod) {
System.out.print(i.intValue() + " ");
}
return Array_After_ibraMethod;
}
and this is the compare function code
Compare(int a, int b, int c) {
int r;
if ((a > b) && (a > c)) {
r = 1;
System.out.println("\n A ");
} else if ((b > a) && (b > c)) {
r = -1;
System.out.println("\n B ");
} else {
r = 0;
System.out.println("\n C");
}
return r;
}
Use j<InputArray[i].length; instead of j<InputArray[h].length; as j<InputArray[h].length; may give you java.lang.ArrayIndexOutOfBoundsException

How to find the GCD of three numbers within a single method

I've got to ensure that the GCD between 3 numbers is no greater than 1.
Here's the code I have so far for the method:
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
if()
}
return 1;
}
the return 1 was already there when I started working on the lab. How can I make sure that the GCD is no more than 1? And return all three integers?
Here's the remainder of the code if it helps in figuring out what needs to be done:
import static java.lang.System.*;
public class Triples
{
private int number;
public Triples()
{
this(0);
}
public Triples(int num)
{
number = num;
}
public void setNum(int num)
{
number = num;
}
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
if()
}
return 1;
}
public String toString()
{
String output="";
int max = number;
for(a = 1; a <= max; a++)
{
for(b = a +1; b <= max; b++)
{
for(c = b + 1; c <= max; c++)
{
if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
{
if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
}
}
}
}
return output+"\n";
}
}
UPDATE
Here is my new coding for the same lab:
import static java.lang.System.*;
public class Triples
{
private int number;
public Triples()
{
this(0);
}
public Triples(int num)
{
number = num;
}
public void setNum(int num)
{
number = num;
}
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
int max = number;
for(a = 1; a <= max; a++)
{
a = n;
for(b = a +1; b <= max; b++)
{
b =n;
for(c = b + 1; c <= max; c++)
{
c = n;
if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
{
if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
{
if(a%2<=1 && b%2<=1 && c%2<=1)
{
return 1;
}
}
}
}
}
}
}
return 1;
}
public String toString()
{
String output="";
output = greatestCommonFactor(a, b, c);
return output+"\n";
}
}
You can use Euclid's algorithm to calculate the GCD of a and b. Call the result d. Then the GCD of a, b, and c is the GCD of c and d; for that, you can use Euclid's algorithm again.
Here's a brute-force way if you don't care about efficiency:
private int greatestCommonFactor(int a, int b, int c)
{
limit = Math.min(a, b);
limit = Math.min(limit, c);
for(int n = limit; n >= 2; n--)
{
if ( (a % n == 0) && (b % n == 0) && (c % n == 0) ) {
return n;
}
}
return 1;
}
Explanation:
You can save some work by only checking up to the minimum of (a, b, c). Any number greater than that definitely won't be a GCD of all 3.
You need to start your loop at n = limit instead of n = 0 and count backwards.
As soon as we come across a number that produces zero remainder for (a, b, c), that must be the GCD.
If nothing is found within the loop, GCD defaults to 1.

Categories

Resources