How would I get this output? - java

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

Related

Showing the whole process of GCD in 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);
}
}
}
}

how to exclude negative numbers from average calculation in while loop

I have a program that accepts user inputs and calculate Max, Min, and Average. The program closes when the user inputs any negative number. How do i exclude the negative number from the average calculation? Here is what i have so far.
// variable
double n = 1;
double ave = 0;
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE ;
int count = 0;
double neg;
//creat scanner object
Scanner input = new Scanner(System.in);
//loop
while (n > 0) {
System.out.print("Input an income (any negative number to quit): ");
n = input.nextDouble();
sum = sum + n;
count++;
ave = sum / count;
if(n<0) neg = n;
if(n>max && n >= 0 ) max = n;
if(n<min && n >= 0) min = n;
if(n>0) ave = n; }
System.out.print(" Average " + ave + "\n Maximum " + max + "\n Minimum " + min);
}
}
Add an if condition:
n = input.nextDouble();
if(n < 0)
break;
sum = sum + n;
The following code only sums the input numbers when n is not negative.
import java.util.Scanner;
public class sample {
public static void main(String[] args) {
double n = 1;
double ave = 0;
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
int count = 0;
double neg;
Scanner input = new Scanner(System.in);
// loop
while (n > 0) {
System.out.print("Input an income (any negative number to quit): ");
n = input.nextDouble();
if(n >= 0){
sum = sum + n;
count++;
}
if (n < 0)
neg = n;
if (n > max && n >= 0)
max = n;
if (n < min && n >= 0)
min = n;
if (n > 0)
ave = n;
}
System.out.print(" Average " + ave + "\n Maximum " + max
+ "\n Minimum " + min);
}
}
Try this:
double n = 1;
double ave = 0;
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE ;
int count = 0;
// create scanner object
Scanner input = new Scanner(System.in);
// loop until n is negative
while (n >= 0) {
System.out.print("Input an income (any negative number to quit): ");
n = input.nextDouble();
if (n >= 0) {
if (n > max) max = n;
if (n < min) min = n;
sum = sum + n;
count++;
}
}
if (count > 0)
ave = sum / (double) count;
System.out.print(" Average " + ave + "\n Maximum " + max + "\n Minimum " + min);

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

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

easy for loop not working

So I was trying to make
(EX) Enter some values: 1 -2 -3 2 5
num of positive num is 5 num of neg num is -3
total is 3 avg is .6
I wanted to make it like this but when i run it,
it doesn't work
what part is error???
import java.util.*;
public class Welcome {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter an int value, the program exits if the input is 0: ");
int num = input.nextInt();
int countpos = 0;
int countneg = 0;
int totalnum = 0;
int total = 0;
double avg = 0.0;
while(num != 0){
if(num < 0)
countpos++;
else
countneg++;
total = total + num;
totalnum++;
}
System.out.print("num of pos is: " + countpos);
System.out.print("num of neg is: " + countneg);
System.out.print("total is: " + total);
System.out.print("the avg is: " + total / totalnum );
}
}
you have to do num = input.nextInt(); in the loop too
while(num != 0){
if(num < 0)
countpos++;
else
countneg++;
total = total + num;
totalnum++;
num = input.nextInt();
}

Categories