Printing Common Divisors of Inputted Positive Integers - java

I need to print the common divisors between two positive integers that were entered. They need to be printed in ascending order. If they are relatively prime, "1" needs to be printed. The code I have here is nowhere near correct. I'm really confused on how to use the loops properly while keeping it in ascending order.
Sample input:
Integer a: 8 Integer b: 12
Sample Output:
Common divisors of 8 and 12:
1
2
4
8 and 12 are not relatively prime.
Alternate Input:
Integer a: 8 Integer b: 9
Common divisors of 8 and 9:
1
8 and 9 are relatively prime.
import java.util.Scanner;
public class RelativelyPrime {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num1 = scnr.nextInt();
int num2 = scnr.nextInt();
System.out.println("Common divisors of " + num1 + " and " + num2 + ":");
int div1 = 0;
int div2 = 0;
int same = 0;
for (int i=1;i<=num1;i++) {
while (div1 <= num1) {
div1 = num1/i;
}
while (div2 <= num2) {
div2 = num2/i;
}
if (div1 == div2) {
div1 += same;
System.out.println(same);
System.out.println(num1 + " and " + num2 + " are not relatively prime.");
}
if (div1 != div2) {
System.out.println(1);
System.out.println(num1 + " and " + num2 + " are relatively prime.");
}
}
}
}

You can try something simple like the below:
import java.util.Scanner;
public class RelativelyPrime {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num1 = scnr.nextInt();
int num2 = scnr.nextInt();
System.out.println("Common divisors of " + num1 + " and " + num2 + ":");
for(int i = 1; i<= Math.min(num1,num2); i++){
if(num1%i==0 && num2%i==0) {
System.out.println(i);
}
}
}
}

Scanner input = new Scanner (System.in);
num1 = input.nextInt();
num2 = input.nextInt();
List<Integer> list = new ArrayList<>();
for(int i=0; i<=Math.min(num1,num2);i++){
if(num1%i==0 && num2%i==0){
list.add(i);
}
}
System.out.println("Divisors:");
for(int a : list){
System.out.println(a); }
if(list.size()<2){
System.out.print("Not Relatively Prime"); }
else { System.out.print("Relatively Prime");}

Related

I want to print how many number is greater than the average

I already print the total and average. However I can't print how many numbers is greater than average.
I think the problem is the number>= average, it seems like only adding the last input.
public static void main(String[] args) {
int i;
int number = 0;
double total=0;
double average=0;
int aboveaverage=0;
Scanner read = new Scanner (System.in);
for(i=1;i<9;i++){
System.out.print("Enter number " + i +": ");
number=read.nextInt();
if(number<0){
System.out.println("Invalid Input");
break;
}
total+=number;
}
if(number>=average){
aboveaverage+=1;
System.out.println("Greater than average is :" + aboveaverage);
}
average=total/8;
System.out.println("Print total : "+ total);
System.out.println("Print Average : " +average );
}
}
You will need to collect all numbers (by placing them in an int array), and iterate over all numbers, for example inside a for loop.
public static void main(String[] args) {
int i;
int number = 0;
int numberCount = 8;
int[] numberArray = new int[numberCount];
double total = 0;
double average = 0;
int aboveAverage = 0;
Scanner read = new Scanner (System.in);
for(i = 0; i < numberCount; i++){
System.out.print("Enter number " + (i + 1) + ": ");
number = read.nextInt();
if(number < 0){
System.out.println("Invalid input");
continue;
}
numberArray[i] = number;
total += number;
}
average = total / numberCount;
for(i = 0; i < numberCount; i++){
if(numberArray[i] > average) {
aboveAverage++;
}
}
System.out.println("Count of numbers greater than average: " + aboveAverage);
System.out.println("Print total: " + total);
System.out.println("Print average: " + average);
}

Trouble with a simple calculator code (Java)

So I've been trying to make a simple calculator program in Java myself and I seem to have encountered a problem. The code doesn't seem to have an error (none showing in Eclipse or in Command Prompt), but when I run it it ends after you input the operation. Here's the example of my code:
public class vjezba1
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
Scanner text=new Scanner(System.in);
String opr;
int x;
int y;
int sum;
System.out.println("This is a simple calculator program.");
System.out.println("Input first number: ");
x = input.nextInt();
System.out.println("Input second number: ");
y = input.nextInt();
System.out.println("Choose an operation (+,-,*,/): ");
opr = text.nextLine();
if(opr == "+"){
sum = x + y;
System.out.println("Result is: " + sum);
}else if(opr == "-"){
sum = x - y;
System.out.println("Result is: " + sum);
}else if(opr == "*"){
sum = x * y;
System.out.println("Result is: " + sum);
}else if(opr == "/"){
sum = x / y;
System.out.println("Result is: " + sum);
}
}
}
Any and all insight is appreciated.
Use method
string.equals("string")
when comparing two strings in Java
Also start names of your classes with an upper case letter, that is a programming convention.
http://www.oracle.com/technetwork/java/codeconventions-135099.html
import java.io.*;
import java.util.Scanner;
public class Vjezba1
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
Scanner text=new Scanner(System.in);
String opr;
int x;
int y;
int sum;
System.out.println("This is a simple calculator program.");
System.out.println("Input first number: ");
x = input.nextInt();
System.out.println("Input second number: ");
y = input.nextInt();
System.out.println("Choose an operation (+,-,*,/): ");
opr = text.nextLine();
System.out.print(opr);
if(opr.equals("+")){
sum = x + y;
System.out.println("Result is: " + sum);
}else if(opr.equals("-")){
sum = x - y;
System.out.println("Result is: " + sum);
}else if(opr.equals("*")){
sum = x * y;
System.out.println("Result is: " + sum);
}else if(opr.equals("/")){
sum = x / y;
System.out.println("Result is: " + sum);
}
}
}

Dividing just by subtracting and finding the remainder- without using the division operator

I seem have been able to figure out the first par at with the multiplication but my logic is a tad confused when trying to figure out the remainder. When i run it i just get a negative number instead of it looking like division- any help will be greatly appreciated!
Main Java Class
import java.util.Scanner;
public class assignment4 {
public static void main(String args[]) {
Scanner inputReader = new Scanner(System.in);
int multiplicand, multiplier;
int dividend, divisor;
int total=0, total2 = 0, countMult, countDiv;
System.out.print("Enter a multiplican: ");
multiplicand = inputReader.nextInt();
System.out.print("Enter a multiplier: ");
multiplier = inputReader.nextInt();
countMult = multiplier;
while(multiplier > 0) {
total = total + multiplicand;
multiplier--;
}
System.out.println(multiplicand + " times " + countMult + " equals " + total );
System.out.print("Enter a dividend: ");
dividend = inputReader.nextInt();
System.out.print("Enter a divisor: ");
divisor = inputReader.nextInt();
countDiv = divisor;
while(divisor > 0) {
total2 = total2 - dividend;
divisor--;
}
System.out.print(dividend + " divided by " + countDiv + " equals " + total2 );
}
}

infinite loop when asking for number

Hi all thanks for taking the time, I am continuosly getting an infinate loop when i am inserting the variable m could anyone please take a look thanks.
public static void main program7_2(String args [])
{
Scanner sc = new Scanner (System.in);
System.out.println("Please enter the first number: ");
int n = sc.nextInt();
while((n%2)== 0)
{
System.out.println("The number you entered is incorrect please enter an odd number:");
n = sc.nextInt();
}
System.out.println("Please enter the second number: ");
int m = sc.nextInt();
while((m%2)== 0)
{
System.out.println("The number you entered is incorrect please enter an odd number:");
m = sc.nextInt();
}
int sum =0;
for (int i = n; n<=m; i++)
{
if ((i%2) != 0)
sum = sum + i;
}
System.out.println("Sum of the numbers between "+n+ " and " +m+": " + sum);
}
The problem of the program is to enter 2 odd numbers and get the sum of the odd numbers in between
Thanks and regards
Instead of n<=m in your for loop use i<=m since you are using i as your counter and not n
This is the error :
for (int i = n; n<=m; i++) /* terminating condition "n<=m" is never met here */
{
if ((i%2) != 0)
sum = sum + i;
}
Why : this loop increments i but terminating condition is n<=m which is never met....so either do i<n Or i<m which ever suits you for terminating the condition!!
if u want to get the sum of nubers between those two use
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first number: ");
int n = sc.nextInt();
while ((n % 2) == 0) {
System.out.println("The number you entered is incorrect please enter an odd number:");
n = sc.nextInt();
}
System.out.println("Please enter the second number: ");
int m = sc.nextInt();
while ((m % 2) == 0) {
System.out.println("The number you entered is incorrect please enter an odd number:");
m = sc.nextInt();
}
int sum= 0;
for (int i = n; i <= m; i++) {
sum = sum + i;
}
System.out.println("Sum of the numbers between " + n + " and " + m + ": " + sum);
}
}

How do I make this output prime numbers as factors instead of all factors? [duplicate]

This question already has answers here:
Java Display the Prime Factorization of a number
(7 answers)
Closed 9 years ago.
I need this to output factors as prime numbers only.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a number:");
int theNum = keyboard.nextInt();
int i;
System.out.println("\nThe prime factors of " + theNum + " are:");
for(i=1; i <= theNum/2; i++)
if(theNum % i == 0)
{
System.out.print(i + " ");
}
}
}
You basically just need to check for prime numbers
Some prime-number check function I copied from here:
boolean isPrime(int n) {
//check if n is a multiple of 2
if (n%2==0) return false;
//if not, then just check the odds
for(int i=3;i*i<=n;i+=2) {
if(n%i==0)
return false;
}
return true;
}
Then just add the isPrime(i) check:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a number:");
int theNum = keyboard.nextInt();
System.out.println("\nThe prime factors of " + theNum + " are:");
for(int i = 1; i <= theNum / 2; i++)
if (theNum % i == 0 && isPrime(i))
System.out.print(i + " ");
}
Test.

Categories

Resources