Suggestion with Java(Line Jump) - java

I need some help with my java project. What I want to do is to let the user put in a number and my program will then print that number starter from 1. I have also added field width to 5. What I want to do now is to make line jumps: For example the first line will have 1 character, the next will have 2, then next will have 3 and so on. The field width from the start will also increase on every line. Here is my code:
import java.util.Scanner;
public class ProjectB {
public static void main(String[] args) {
printNumbersB(0);
}
public static void printNumbersB(int x){
Scanner input = new Scanner(System.in);
System.out.print("Please put in: ");
x = input.nextInt();
for(int y = 1; y <= x; y++){
System.out.printf("%5d", y);
input.close();
}
}
}
How output should be:
http://imgur.com/2QLTRlA

Try this:
int n = 45;
int counter = 1;
for (int i = 1; i < n; i++) {
for (int j = i; j < counter + i; j++) {
System.out.printf("%" + counter + "d", j);
}
i += counter - 1;
System.out.println();
counter++;
}

I do not understand what you want to do. But from what I understand this is what you want:
public static void main(String[] args) {
printNumbersB();
}
public static void printNumbersB(){
Scanner input = new Scanner(System.in);
System.out.print("Please put in: ");
x = input.nextInt();
//Should use String Builder
String accumlationString = "";
for(int y = 1; y <= x; y++){
System.out.printf(accumulationString + "%5d", y);
accumulationString = accumulationString + "%5d";
;
input.close();
}
}
}
FINAL EDIT:
This works but their is a more efficient way to do this:
public static void printNumbersB() {
Scanner input = new Scanner(System.in);
System.out.print("Please put in: ");
int x = input.nextInt();
input.close();
//Should use String builder
String tab = "";
int lineNumber;
int num = 1;
breakfor:
for (int i = 1; i <= x; i++) {
System.out.println();
lineNumber = i;
tab = tab + " ";
for (int j = lineNumber; j > 0; j--) {
System.out.print(tab + num);
num = num + 1;
if(num > x)
break breakfor;
}
}
}
public static void main(String[] args) {
printNumbersB();
}

Related

How do i put the total sum of prime numbers first, then the prime numbers below it

I'm currently using this code, and I'm trying to output the total sum of how many prime numbers first and then the prime numbers list
Here is my code:
import java.util.*;
public class PrimeTime {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j < i; j++) {
if (i%j == 0)
f = 1;
}
if (f == 0) {
System.out.print(i+ " ");
s = s +1;
}
}
System.out.println("N =" +s);
}
}
This is the result I get:
Input number: 10
2 3 5 7 N = 4
But what I'm looking for is this result:
Input number: 10
N = 4
2 3 5 7
I don't know how, I tried putting the S.O.P in different places and I can't figure out how.
If you want to display N = 4 before 2 3 5 7, you can append the numbers (i.e. 2 3 5 7) to a StringBuilder and print the same after printing N = 4 e.g.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
StringBuilder numbers = new StringBuilder();
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0)
f = 1;
}
if (f == 0) {
numbers.append(i).append(' ');// Append to StringBuilder instead of printing
s = s + 1;
}
}
System.out.println("N =" + s);
System.out.println(numbers);
}
}
A sample run:
Input number: 10
N =4
2 3 5 7
Note: for checking the primality, checking up to Math.sqrt(i) is sufficient i.e. you should replace j < i with j <= Math.sqrt(i).
You can store the numbers in a list and then print them after the loop is over
Don't worry if you have not studied lists yet cause you will have to soon anyways.
here's the code:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r, s = 0;
LinkedList<Integer> list = new LinkedList<>();
System.out.print("Input number: ");
r = sc.nextInt();
System.out.println();
for (int i = 2; i < r; i++) {
int f = 0;
for (int j = 2; j < i; j++) {
if (i%j == 0)
f = 1;
}
if (f == 0) {
//System.out.print(i+ " ");
list.add(i);
s = s +1;
}
}
System.out.println("N = " +s);
for(int i = 0; i<list.size(); i++ ) {
System.out.print(list.get(i) + " ");
}
}

How to initialize variable back to 0 in a while loop in java?

I want my code to loop, but reinitialize the variable back to 0. Every time I input a number, it add it to the previous result, but I want it to reset. I attached two images below. One is the actual output and the other is the expected output.
import java.util.Scanner;
public class AddOrMultiplyNNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String am;
int sum = 0;
int total = 1;
int n = 0;
while (true) {
System.out.print("enter an integer number: ");
n = input.nextInt();
if(n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
am = input.nextLine();
if (am.equals("a")) {
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
}
}
Actual Output
Desired Output
You can use continue
if(n == 0) {
sum = 0;
total = 1;
continue;
}
You can initialize the variables inside the while loop
public class AddOrMultiplyNNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String am;
while (true) {
int sum = 0;
int total = 1;
int n = 0;
System.out.print("enter an integer number: ");
n = input.nextInt();
if(n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
am = input.nextLine();
if (am.equals("a")) {
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
}
}
To zero a variable, simply assign 0 at the appropriate point.
sum = 0;
An appropriate place to insert this statement for your desired output is immediately before the first for loop. Likewise, reset total before the second for.
However, a much better way to write this is to declare the variable where it is needed, as you have done for x and y. This applies to am, sum, total and n
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("enter an integer number: ");
int n = input.nextInt();
if (n == 0) {
break;
}
System.out.print("enter either 'a' or 'm': ");
input.nextLine();
String am = input.nextLine();
if (am.equals("a")) {
int sum = 0;
for (int y = 1; y <= n; y++) {
sum = sum + y;
}
System.out.println(sum);
} else if (am.equals("m")) {
int total = 1;
for (int x = 1; x <= n; x++) {
total = total * x;
}
System.out.println(total);
}
}
I don't know if I fully understand your question, but just do sum=0; and total=1; after you print out the final result. You should also consider doing a try/catch statement for robustness on the nextInt so that characters and strings don't break your program...
try {
n = input.nextInt();
}
catch (Exception e) {
System.out.println("Not a Number");
}

Java Scanner cannot read nextInt() unless enter is pressed / new line character

My program for the coding challenge produces the correct output, however, it requires me to press enter a second time for it to work. Here is the programming challenge.
It prints out the first line just fine but requires me to press enter again for it to work.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
sc.nextLine();
for(int i = 0; i < testCases; i++){
int size = sc.nextInt();
sc.nextLine();
int[] arr = new int[size];
for(int x = 0; x < size; x++){
arr[x] = sc.nextInt();
}
sc.nextLine();
if(size > 1){
solve(arr);
}
else{
System.out.println("0");
}
}
}
private static void solve(int[] arr) {
for(int i =0 ; i < arr.length; i++){
arr[i] = Math.abs(arr[i]);
}
Arrays.sort(arr);
for(int i = 0; i < arr.length - 1; i++){
int f = i + 1;
if(arr[i] == arr[f]){
System.out.print((arr[i] * -1) + " " + arr[i]);
}
}
System.out.println();
}
}

Java sum from 1 to n with output showing work

I have a homework problem that I've almost finished, but I'm just stuck on how to output it correctly.
Write a program that reads a positive integer n and prints the sum of all integers from 1 to n as follows:
1+2+…+n=n(n+1)/2
The output does not contain any spaces.Example of input: 5 Corresponding output: 1+2+3+4+5=15
Here's my code:
import java.util.Scanner;
public class Homework2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i = 0;i <= n; i++) {
sum = sum + i;
}
System.out.printf("the sum of %d is %d%n", n, sum);
}
}
What I have in the printf command is just a placeholder until I can figure out the correct output.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1;i <= n; i++) {
if (i != n) {
System.out.print(i + "+");
} else {
System.out.print(i + "=");
}
}
System.out.print(n*(n+1)/2);
}
the above will work. You must be strict to the expected output.
You need to print within the loop.
And you should start at 1, like the instructions say
for (int i = 1;i < n; i++) {
sum = sum + i;
System.out.printf("%d+", i);
}
sum += n;
System.out.printf("%d=%d\n", n, sum);
You can deal with the case of printing n and = separately like so:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Print the sum of all integers from 1 to n program");
System.out.println("=================================================");
System.out.print("Please enter n: ");
int n = scanner.nextInt();
int sum = 0;
for(int i = 1; i < n; i++) {
System.out.print(i + "+");
sum += i;
}
sum += n;
System.out.print(n + "=" + sum);
}
}
Try it here!
Example output:
Print the sum of all integers from 1 to n program
=================================================
Please enter n: 5
1+2+3+4+5=15

Error Showing in (int i=n;i>0;i--)

I tried to write code for an factorial number program,following code
public class Factorial {
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
System.out.println("Enter the number whose factorial you want: ");
int n = in .nextInt();
int f = 1;
for (int i = n; i & gt; 0; i--) //error show what's wrong in for loop {
f = f * i;
}
System.out.println("Factorial of " + n + " is " + f);
}
}
Yes your error point is & gt; which is not an operator actual operator is >. > is XML encoded from of >.
Use this:
for (int i = n; i > 0; i--)
Instead of
for (int i = n; i & gt; 0; i--)
This happens usually when you copy past code from somewhere.
Complete code should be:
public class Factorial {
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
System.out.println("Enter the number whose factorial you want: ");
int n = in .nextInt();
int f = 1;
for (int i = n; i > 0; i--) //error show what's wrong in for loop {
f = f * i;
}
System.out.println("Factorial of " + n + " is " + f);
}
}

Categories

Resources