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++;
Related
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);
}
}
}
}
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");
}
}
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);
}
}
I'm trying to half the input if it % 12 == 0, but if it isn't then you multiply it by 3 and add 1 onto the sum.
The question that I'm working off is: http://i.imgur.com/VzuPtZJ.png
With the code I have currently(which is below), if I enter 12, like in the question I start off with 6, but then the results begin to go wrong and then they go insanely wrong with values in the millions and negative millions etc.
import java.util.*;
public class sheet12t3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int aNumber = Integer.parseInt(in.nextLine());
hailstone(aNumber);
}
public static void hailstone(int x)
{
int count = 1;
int max = 0;
String results = "Hailstone series for the number " + x + " is ";
while (x >= 1)
{
if (x % 12 == 0)
x = x / 2;
else
x = 3 * x + 1;
count++;
results += + x + ", ";
if (x > max)
max = x;
}
results += "a total of " + count + " numbers in this sequence with a maximum value of " + max;
System.out.print(results);
}
}
The question says divide by two if the number is even. But you divide by 2 only when it is dividable by 12.
Change this line
(x % 12 == 0)
to
(x % 2 == 0)
And change while (x >= 1) to while (x > 1)
Here is the code you are looking for:-
import java.util.*;
public class sheet12t3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int aNumber = Integer.parseInt(in.nextLine());
hailstone(aNumber);
}
public static void hailstone(int x)
{
int count = 1;
int max = 0;
String results = "Hailstone series for the number " + x + " is ";
while (x > 1)
{
if (x % 2 == 0)
x = x / 2;
else
x = 3*x + 1;
System.out.print(x+" ");
max++;
}
}
}
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);
}
}
}
}