Showing the whole process of GCD in Java - java

I'm not that new with Java (studied it years ago but stopped for 5-6 years because of work) and I'm trying to get in touch with it again. A friend of mine gave me practice problems I could work with to practice, but it is quite hard for me. It is basically showing the steps in finding GCD using Euclidean Algorithm. I did everything, but the last detail needed for the GCD is missing once I input a big number.
Here is the code I've done so far:
import java.util.Scanner;
class mpONE {
public static void main (String[] args)
{
System.out.println("Finding GCD Using Euclid's Algorithm");
Scanner num = new Scanner(System.in);
System.out.println("Enter your first (higher) number: ");
int n1 = num.nextInt();
System.out.println("Enter your second (lower) number: ");
int n2 = num.nextInt();
System.out.println("Numbers for finding GCD are: " + n1 + " " + n2);
System.out.println("Computing for GCD... ");
for (int i = 0; i <= n2; i++)
{
int g = n1/n2;
int f = (g * n2);
int h = n1 - f;
System.out.print(n1 + " = " + "(" + n2 + " * " + g + ") + " + h);
n1 = n2;
n2 = h;
System.out.println();
if (h == 0)
{
break;
}
}
for (int i = 1; i <= n1 && i <= n2; ++i) {
if (n1 % i == 0 && n2 % i == 0)
{
int ans = i;
System.out.println("Your GCD is " + ans);
}
}
}
}
With smaller numbers, it does work (the "+ h" should be 0), but with big numbers, it stops just before it shows the 0 remainder. Thanks for your help!

Just change the location of the if(h==0) block:
import java.util.Scanner;
class mpONE {
public static void main (String[] args)
{
System.out.println("Finding GCD Using Euclid's Algorithm");
Scanner num = new Scanner(System.in);
System.out.println("Enter your first (higher) number: ");
int n1 = num.nextInt();
System.out.println("Enter your second (lower) number: ");
int n2 = num.nextInt();
System.out.println("Numbers for finding GCD are: " + n1 + " " + n2);
System.out.println("Computing for GCD... ");
for (int i = 0; i <= n2; i++)
{
int g = n1/n2;
int f = (g * n2);
int h = n1 - f;
System.out.print(n1 + " = " + "(" + n2 + " * " + g + ") + " + h);
System.out.println();
if (h == 0)
{
break;
}
n1 = n2;
n2 = h;
}
for (int i = 1; i <= n1 && i <= n2; ++i) {
if (n1 % i == 0 && n2 % i == 0)
{
int ans = i;
System.out.println("Your GCD is " + ans);
}
}
}
}

Related

Perfect Number Method w/ Range

I can't get the "its not a perfect number" to display. I need help with last part. It doesn't allow me to change it to else.
package editmess;
import java.util.Scanner;
public class Editmess {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("\nPerfect Number Finder Program");
System.out.print("\nEnter the start value: ");
int starval = scanner.nextInt();
System.out.print("Enter the end value:");
int endval = scanner.nextInt();
for (int n1 = starval; n1 < endval; n1++) {
int sum = 0;
for (int n2 = 1; n2 < n1; n2++) {
if (n1 % n2 == 0) {
sum = sum + n2;
}
}
if (sum == n1) {
System.out.println(n1 + " is a perfect number");
if (sum != n1) {
System.out.println("There is no perfect number between " + starval + " and " + endval);
break;
}
}
}
}
}
You can't put an else statement inside and if statement unless there is another if statement there.
Here is the whole code. Also you weren't reading in all the numbers entered. The first for loop should be n1 <= endval; and not n1 < endval; so that it also checks the enval entered.
Scanner scanner = new Scanner(System.in);
int counter = 0;
System.out.println("\nPerfect Number Finder Program");
System.out.print("\nEnter the start value: ");
int starval = scanner.nextInt();
System.out.print("Enter the end value:");
int endval = scanner.nextInt();
for (int n1 = starval; n1 <= endval; n1++) {
int sum = 0;
for (int n2 = 1; n2 < n1; n2++) {
if (n1 % n2 == 0) {
sum = sum + n2;
}
}
if (sum == n1) {
System.out.println(n1 + " is a perfect number");
counter ++; //This will add one to the counter if this loop is enterd
}
if(n1 == endval){
System.out.println("FINISHED!");
break;
}
}
//If the counter is 0 then it will display the message
if(counter == 0){
System.out.println("THERE IS NO PERFECT NUMBERS");
}
}

How would I get this output?

import java.util.Scanner;
public class EuclidGCD {
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
System.out.print ("Enter First Number: ");
int n1 = kbd.nextInt();
System.out.print ("Enter Second Number: ");
int n2 = kbd.nextInt();
int gcd = 1;
int k = 2;
while (k <= n1 && k <= n2){
if (n1 % k == 0 && n2 % k == 0)
gcd = k ;
k ++;
}
System.out.println("The GCD of " + n1 + " and " + n2 + " is " + gcd);
}
}
so everything works but I realized that I do not want a neg number if a negative number is entered I want the output POSITIVE NUMBERS ONLY so would I need another while loop?
Just add a if statement checking if n1 and n2 are positive, and only excuted the rest of your code if they are. You shouldn't need another while loop
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
System.out.print ("Enter First Number: ");
int n1 = kbd.nextInt();
System.out.print ("Enter Second Number: ");
int n2 = kbd.nextInt();
if(n1 < 0 || n2 < 0 ){
System.out.println("POSITIVE NUMBERS ONLY ");
}else{
int gcd = 1;
int k = 2;
while (k <= n1 && k <= n2){
if (n1 % k == 0 && n2 % k == 0)
gcd = k ;
k ++;
}
System.out.println("The GCD of " + n1 + " and " + n2 + " is " + gcd);
}
}

Tidying code and fixing errors (java)

I need to modify the code so that,
its structure is better
it is more readable
it uses methods and parameters
only one statement needs to be changed if a different number of integers is to be input
My new code is at the bottom but it's not working yet, can't quite figure out what else I need to do to it. If anyone could help that would be fab.
Original Code
import java.util.Scanner;
public class MakeMeBetter
{
public static void main(String[] args)
{
int[] a = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Scanner b = new Scanner(System.in);
System.out.println("Please input 10 numbers in the range 1-19 :> ");
int c = b.nextInt();
while (c < 1 || c > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[0] = c;
int d = b.nextInt();
while (d < 1 || d > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[1] = d;
int e = b.nextInt();
while (e < 1 || e > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[2] = e;
int f = b.nextInt();
while (f < 1 || f > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[3] = f;
int g = b.nextInt();
while (g < 1 || g > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[4] = g;
int h = b.nextInt();
while (h < 1 || h > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[5] = h;
int i = b.nextInt();
while (i < 1 || i > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[6] = i;
int j = b.nextInt();
while (j < 1 || j > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[7] = j;
int k = b.nextInt();
while (k < 1 || k > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[8] = k;
int l = b.nextInt();
while (l < 1 || l > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[9] = l;
System.out.println("\nArray contents");
System.out.print((a[0] < 10 ? " " : "") + a[0] + " ");
System.out.print((a[1] < 10 ? " " : "") + a[1] + " ");
System.out.print((a[2] < 10 ? " " : "") + a[2] + " ");
System.out.print((a[3] < 10 ? " " : "") + a[3] + " ");
System.out.print((a[4] < 10 ? " " : "") + a[4] + " ");
System.out.print((a[5] < 10 ? " " : "") + a[5] + " ");
System.out.print((a[6] < 10 ? " " : "") + a[6] + " ");
System.out.print((a[7] < 10 ? " " : "") + a[7] + " ");
System.out.print((a[8] < 10 ? " " : "") + a[8] + " ");
System.out.print((a[9] < 10 ? " " : "") + a[9] + " ");
System.out.println();
boolean noSwap = false;
int startAt = 0;
int stopAt = 9;
while (startAt < stopAt && noSwap == false)
{
noSwap = true;
for (int m=startAt; m<stopAt; m++)
{
if (a[m] > a[m+1])
{
int t = a[m];
a[m] = a[m+1];
a[m+1] = t;
noSwap = false;
}
}
stopAt = stopAt - 1;
}
System.out.println("\nArray contents");
System.out.print((a[0] < 10 ? " " : "") + a[0] + " ");
System.out.print((a[1] < 10 ? " " : "") + a[1] + " ");
System.out.print((a[2] < 10 ? " " : "") + a[2] + " ");
System.out.print((a[3] < 10 ? " " : "") + a[3] + " ");
System.out.print((a[4] < 10 ? " " : "") + a[4] + " ");
System.out.print((a[5] < 10 ? " " : "") + a[5] + " ");
System.out.print((a[6] < 10 ? " " : "") + a[6] + " ");
System.out.print((a[7] < 10 ? " " : "") + a[7] + " ");
System.out.print((a[8] < 10 ? " " : "") + a[8] + " ");
System.out.print((a[9] < 10 ? " " : "") + a[9] + " ");
System.out.println();
double n = (a[0] + a[1] + a[2] + a[3] +
a[4] + a[5] + a[6] + a[7] +
a[8] + a[9]) / 10;
System.out.println("The minimum number is: " + a[0]);
System.out.println("The maximum number is: " + a[9]);
System.out.println("The average value is: " + n);
System.out.println("The median is: " + a[4]);
}
}
New Code
import java.util.Scanner;
import java.math.*;
public class MakeMeBetterImproved {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
int[] numbers = new int[10];
int array = 0;
System.out.println("Enter 10 integers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = kybd.nextInt();
int MyIntArray = array + i;
}
System.out.println("The minimum number is: " + math.min);
System.out.println("The maximum number is: " + math.max);
System.out.println("The average value is: " + math.average);
System.out.println("The median is: " + math.median);
}
}
Consider using a more dynamic structure than a primitive array.
I like ArrayLists: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Reorganize your long code into while or for loops.
An example:
ArrayList<Integer> a = new ArrayList<Integer>();
Scanner b = new Scanner(System.in);
Integer c = null;
while (a.size() != 10) {
System.out.println("Please input a number in the range 1-19 :> ");
c = b.nextInt();
while (c < 1 || c > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
c = b.nextInt();
}
a.add(c);
}
System.out.println("\nArray contents");
for (int i=0; i<10; i++) {
System.out.print((a.get(i) < 10 ? " " : "") + a.get(i) + " ");
}
(And so on...)
Could you try this? I made a few changes.
package p2;
import java.util.Arrays;
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
int[] numbers = new int[10];
int sum = 0;
System.out.println("Enter 10 integers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = kybd.nextInt();
sum += numbers[i];
}
Arrays.sort(numbers);
System.out.println("The minimum number is: " + numbers[0]);
System.out.println("The maximum number is: " + numbers[9]);
System.out.println("The average value is: " + sum / numbers.length);
System.out.println("The median is: " + median(numbers));
}
public static double median(int[] m) {
int middle = m.length / 2;
if (m.length % 2 == 1) {
return m[middle];
} else {
return (m[middle - 1] + m[middle]) / 2.0;
}
}
}

Remove Extra Space At End

I wrote this in Java, and I want the output such that it creates a space after every value except for the last one. How do I get it to show up like 1[]2[]3[]4[]5[]6[]7 rather than 1[]2[]3[]4[]5[]6[]7[] ?
Here is my code!
public class Pentagonal {
public static void main(String[] args) {
int n, x, n1;
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
n = input.nextInt();
n1 = 1;
System.out.print("The pentagonal numbers are: ");
while (n1 <= n) {
x = (3 * n1 * n1 - n1) / 2;
if (n1 == n)
System.out.print(x);
else
System.out.print(x + " ");
n1++;
}
}
}
Your code is perfectly fine. I see no problem with it. Just check this out
while (n1 <= n) {
x = (3 * n1 * n1 - n1) / 2;
System.out.print(x + ((n1 == n) ? "\n" : " "));
n1++;
}
Replace this "\n"(new line) with " " (space) if you want!
Just Add the following line after while loop
System.out.print("\b");
Try:
if(n1==n)
System.out.print(x);
else
if(n1 != n-1) {
System.out.print(x+" ");
} else {
System.out.print(x);
}
n1++;

number of integers in output? simple java program

I'm working on a simple java code that outputs all factors of a user-inputted number. How do I count and then display the number of factors outputted?
System.out.println("Enter an integer to be factored:");
int d = Stdin.readInt();
System.out.println("The Factors of " + d + " are:");
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
System.out.println(w);
}
}
In the code above, it's the number of integers outputted in 'w' For instance, if the number inputted is 8 and its factors are 1,2,4,8, how do I write a code that says '8 has 4 factors' ?
Thanks
You simply need a variable to count factors:
System.out.println("Enter an integer to be factored:");
int d = Stdin.readInt();
int nFactors = 0;
System.out.println("The Factors of " + d + " are:");
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
System.out.println(w);
++nFactors;
}
}
System.out.println(d + " has " + nFactors + " factors");
You need a counter variable. Here is the code:
int counter =0;
for(int w = 1; w <= d; w++ ){
if(d % w == 0){
counter++;
System.out.println(w);
}
System.out.println(d + " has " + counter + "factors ");
Try with this code:
import java.util.Scanner;
public class EmbalzadoFactorial {
public static Scanner sc;
public static void main(String[] args) {
int Number, i;
sc = new Scanner(System.in);
System.out.print("Please Enter any number to Find Factors: ");
Number = sc.nextInt();
System.out.println("The factors are: ");
for(i = 1; i <= Number; i++) {
if(Number%i == 0) {
System.out.format(" %d ", i);
System.out.print ("and");
System.out.format("%s %n ", i);
}
}
}
}

Categories