I would like to write a program that prints a sequence of integer numbers written in a single line with the numbers space-separated. The input of the program is a positive integer called n, this is the number of the elements of the sequence the program should print.
For example, if n = 7, then the program should output 1 2 2 3 3 3 4.
THIS IS MY CODE:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
n = scanner.nextInt();
while (n == 0) {
n = scanner.nextInt();
for (int i = n; i <= n; i++) {
System.out.println(i * n);
}
}
}
}
yeah I have no clue, that's why I am asking for help ;)
Here is a way with comments
n = scanner.nextInt();
int viewNumber = 1;// for printing number
while (n > 0) { // if any number printing left
for (int i = 1; i <= viewNumber; i++) { // This loop print viewNumber viewNumber's times
System.out.print(viewNumber + " ");
n--; // decrease n value after printing a number
if(n == 0) { // break the loop if print n numbers already
break;
}
}
viewNumber++; // increase view number after showing it's that times
}
I am not sure if I understand you, but following code
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(final String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n;
while ((n = scanner.nextInt()) > 0) {
int count = 0;
for (int i = 1; i < n && count < n; i++) {
for (int j = 1; j <= i && count < n; j++) {
System.out.print(i + " ");
count++;
}
}
System.out.println();
}
}
}
should print this:
7
1 2 2 3 3 3 4
8
1 2 2 3 3 3 4 4
Because I am assuming behavior from your example:
For example, if n = 7, then the program should output 1 2 2 3 3 3 4.
I believe this could be rewritten to more efficient code (but more dirty code for you).
Try This, I have used 2 for loops
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter any digit");
int n;
n = scanner.nextInt();
for(int a = 0;n>=a;a++) {
for (int i = 0; a > i; i++) {
System.out.print(" "+a);
}
}
}
Input: 5
Output: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Related
I am doing an exercise where I have to print 'x' (is an input) rows of numbers incrementing from 0 to 10.
If I input 3, the output should look like this
012
345
678
012
345
678
012
345
678
but instead, I get 3 rows of a 0 to 10 count.
I know it might be easy to code, but I am stuck in that!
I think I am not undestanding well the nested loops :(
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int j = 0; j < q; j++) {
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
}
}
}
You don't need two loops for this. All you need is to print a newline after every 3rd letter and an extra newline after every 3rd line. Your code can be like:
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
int lines = 0;
int letters = 0;
while (lines < q) {
System.out.print(i);
if (letters && letters % q == 0) {
System.out.println();
lines++;
}
if (lines && lines % q == 0) {
System.out.println();
letters = 0;
continue;
}
letters++;
}
}
PS: I haven't tried this code myself. But concept would be the same.
The answer above should solve your problem so I will try to explain what your code does.
Let's start with code inside first for loop:
for (int i = 0; i <= 10; i++) {
System.out.print(i);
}
System.out.println();
First we have a loop iterating through numbers from 0 to 10 and the output is:
012345678910
and a new line after that.
That means that output of your program will print above mentioned output q times.
012345678910
012345678910
012345678910
You can try with below code
public class quadrats {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int j = 0; j < q; j++) {
for (int i = 0; i < 9; i++) {
if(i%3 == 0)
System.out.println();
System.out.print(i);
}
System.out.println();
}
}
}
Print X quandrants of X rows and X columns each
Scanner in = new Scanner(System.in);
int q = in.nextInt();
// q quadrants
for (int iQuadrat = 0; iQuadrat < q; iQuadrat++) {
// count will keep track of the last number you print
int count = 0;
// q rows
for (int iRow = 0; iRow < q; iRow++) {
// q cols
for (int iCol = 0; iCol < q; iCol++) {
System.out.print(count);
// increment the count and take its modulo 10 so it stays between 0 and 9
count = (count+1)%10;
}
// line return at the end of the row
System.out.println();
}
// line return between quadrants
System.out.println();
}
For an input of 12, it will print 12 times this quadrant
012345678901
234567890123
456789012345
678901234567
890123456789
012345678901
234567890123
456789012345
678901234567
890123456789
012345678901
234567890123
I need to be able to get this pattern using for loops:
Q. Write a program to complete the first N rows of the following output. The number of rows N should be read from the keyboard.
Intended output:
1
2 4
3 6 9
4 8 12 16
Result:
1
24
399
4161616
525252525
Attempt:
(I haven't used scanner yet, because I wanted to try understand how to do it without scanner.)
import java.util.Scanner;
public class Test {
public static void main(String [] args){
int odd = 1;
for(int i=1;i<=5;i++)
{
int no=i;
for(int j=1; j<=i;j++)
{
System.out.print(no);
no = i*i;
}
System.out.println();
}
}
}
You were very close!
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}
See demo
I have an array consisting of numbers from 0 to 10. For example,
1, 3, 5, 7, 5, 3, 1, 9
And I want to count the number of occurrences of each number from 0 to 10 in the array. Something like this (a simulation of the output):
number | occurrence
0 0
1 2
2 0
3 2
4 0
5 2
6 0
7 2
8 0
9 1
10 0
EDIT: This is a high school assignment: Write a program that repeatedly prompts the user to supply scores (out of 10) on a test. The program should continue to ask the user for marks until a negative value is supplied. Any values greater than ten should be ignored. Once the program has read all the scores, it should produce a table with the following headings (and automatically fill in the rest of the table):
Score # of Occurrences
The program should then calculate the mean score, rounded to one decimal place.
My code in response to this question:
public static int[] resize(int[] a) {
int[] expandedArray = new int[a.length + 1];
for (int i = 0; i < a.length; i++) {
expandedArray[i] = a[i];
}
return expandedArray;
}
public static void main (String[]args) {
#SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
boolean positive = true;
int count = 0;
int[] originArray = new int[0];
for (#SuppressWarnings("unused")
int i = 0; positive; i++) {
System.out.println("Enter a score: ");
int input = scan.nextInt();
System.out.println("The input is recorded: " + input);
if (input < 0) {
positive = false;
System.out.println("The input is negative.");
}else if (input > 10){
System.out.println("///INVALID///: Must be out of 10!");
} else {
System.out.println("count: " + count);
originArray = resize(originArray);
System.out.println("originArray resized");
originArray[count] = input;
System.out.println("The index = count = " + count +" would be assigned input: " + input);
for (int j = 0; j <= count; j++) {
System.out.println(j + ": " + originArray[j]);
}
count++;
System.out.println("count++: " + count);
}
}
System.out.println("Program has stopped taking inputs. Outputting results:");
System.out.println(" Score | Occurrences ");
}
If you are given the input array, following code will produce the results for you in the counts array.
int [] counts = new int[11];
for (int i = 0; i < input.length; i++) {
counts[input[i]]++;
}
Here is my code to find even Fibonacci numbers and to add them:
package a;
import java.util.*;
public class A {
//this about finding Even Fibonacci numbers and adding them to sum.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int[] n = new int[t];
int[] nn = new int[t];
int i,j,sum;
for(int a0 = 0; a0 < t; a0++){
n[a0] = in.nextInt();
}
nn[0]=1;
for(i = 0 ; i<t;i++){
sum = 0;
for(j= 1;j<n[i];j++){
nn[j] = j+nn[j-1];
if(nn[j]%2==0)
{
sum += nn[j];
}
}
System.out.println(sum); //this is not printing the output
}
}
}
Sample Input
2
10
100
Sample Output
10
44
The problem is that this line System.out.println(sum); is not printing anything.
Any ideas?
In your code you have
for(int a0 = 0; a0 < t; a0++){
n[a0] = in.nextInt();
}
The problem is that the program is waiting for you to enter t integers. I don't know what values you want there, but change it to something more like this
for(int a0 = 0; a0 < t; a0++){
n[a0] = 0;//But instead of 0 the actual number that you want to set for the value.
}
I hope you find this helpful!
I do not see a problem here. Just took the code, compiled and executed it. After specifying the value for t and also providing t input values, I saw an output on the console.
stefan#linux-3047:~$ java A
5 (t)
1 (1st of 5 values)
2 (2nd of 5 values)
3 (3rd of 5 values)
4 (4th of 5 values)
5 (5th of 5 values)
0 (System.out.println)
2 (System.out.println)
6 (System.out.println)
6 (System.out.println)
6 (System.out.println)
Multiple problems exist here.
One error at line 25 if the t for example is 5:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at application.A.main(A.java:25)
Added some System.out.println(...) to see how you can fix it cause i don't know the code you want to add:
package application;
import java.util.*;
public class A {
// This about finding Even Fibonacci numbers and adding them to sum.
public static void main(String[] args) {
System.out.println("Enter a number below:\n");
Scanner in = new Scanner(System.in);
int t = in.nextInt();
System.out.println("You entered..." + t+"\n");
// Arrays
int[] n = new int[t];
int[] nn = new int[t];
int i, j, sum;
// First For Loop
for (int a0 = 0; a0 < t; a0++) {
System.out.println("Enter a number a0..");
n[a0] = in.nextInt();
System.out.println("You entered ao=:" + a0+"\n");
}
nn[0] = 1;
// Second For Loop
for (i = 0; i < t; i++) {
sum = 0;
for (j = 1; j < n[i]; j++) {
nn[j] = j + nn[j - 1];
if (nn[j] % 2 == 0) {
sum += nn[j];
}
}
// this is not printing the output
System.out.println("Sum is:="+sum);
}
}
}
Say I have user input 1, the output should be
1
if the input 2, the output should be:
1
12
If the input is 3, the output should be
1
12
123
I keep trying but cannot figure out how to do it help please
import java.util.Scanner;
public class JavaApplication2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("" + j);
}
System.out.println();
}
System.out.println(); //newline
}
}
Use a single for-loop. Use an array to store the previous numbers and output them along with the current number.
var userInput = 10;
var numbers = [];
for (var i = 0; i < userInput; i++) {
numbers.push(i);
document.write(numbers + "<br>");
}