Im trying to take a number and print it's odd number like this:
if i take 5 as a number it should give this:
1 3 5
3 5
5
and if i take 9 it should do the same thing:
1 3 5 7 9
3 5 7 9
5 7 9
7 9
9
This is what i have so far and i am stuck. i can't get the 5 to print after the 3 and to end it with 5 for the triangle:
public class first{
static void afficher(int a){
for(int i=1;i<=a;i++){
if(i%2!=0){
System.out.printf("%d",i);
}
}
System.out.println();
for(int j=3;j<=a-2;j++){
if(j%2!=0){
System.out.printf("%d",j);
}
}
}
public static void main(String[]args){
afficher(5);
}
}
This prints:
1 3 5
3
If you print a surface (2d thus), one expects that the algorithm runs in O(n^2) time complexity. Thus two nested fors:
public class first{
static void afficher(int a){
for(int i = 1; i <= a; i += 2) {
for(int j = i; j <= a; j += 2){
System.out.print(j);
System.out.print(' ');
}
System.out.println();
}
}
}
One can optimize the algorithm a bit by not checking if the number is odd, but taking steps of 2.
See demo.
You have to use nested for-loops to resolve this problem. Go through the following code
public class OddNumberLoop {
public static void main(String[] args) {
Scanner inpupt = new Scanner(System.in);
System.out.print("Input the starting number : ");
int start = inpupt.nextInt();
for(int i = 1 ; i <= start; i += 2){
for(int x = i; x <= start; x += 2) System.out.print(x+ " ");
System.out.println();
}
}
}
The reason it is printing as follows because:
1 3 5 -> your i loop runs here (from 1 to 5)
3 -> your j loop runs here (from 3 to (less than OR equal to 5))
So I suggest the following:
Use 2 nested loops (for universal values):
i running from 1 to the input number increasing by 2
j running from i to the input number increasing by 2 also ending with line change'/n'
Keep a check whether the input number is odd or not.
Related
I am trying to make a program where I can input the size of a 2D array, the highest number in a 2D array, and the most amount of a certain number in the 2D array, and then fill it with random numbers in between 1 and the highest number. In my code, I specify that the max amount of times a number should repeat is 4, yet my output doesn't match that. Any suggestions?
This is my code:
class Main {
public static void main(String[] args) {
System.out.println(fill(6, 9, 4));
}
public static String fill(int size, int max, int most) {
int[][] list = new int[size][size];
int count = 0;
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list[i].length; j++) {
int x = (int)((Math.random()* max) + 1);
int y = 0;
count = 0;
for (int k = 0; k < list.length; k++) {
for (int l = 0; l < list[k].length; l++) {
if(list[k][l] == x) count++;
}
}
if(count < most) {
list[i][j] = x;
} else {
while(true) {
y = (int)((Math.random()* max) + 1);
if(y != x) break;
}
list[i][j] = y;
}
System.out.print(list[i][j] + " ");
}
System.out.println();
}
return "";
}
}
And this is my output:
9 4 6 1 9 1
7 1 4 4 3 2
6 1 4 2 7 9
5 9 4 7 2 5
3 5 3 5 7 4
3 8 8 6 2 6
Problem: There are 6 "4"s and 2 "8"s
You generate a random number.
You then check if this random number is 'invalid', in the sense that it's been used too many times.
Then, you generate a new random number, check that this isn't the same as your previous number, and then just roll with that. You are failing to check if this number, too, is 'overloaded'. So, what could have happened here is that your algorithm picked '9', counts 9s, finds 4 of them, rolls up a new random number, 9 again, so it rolls yet another number, 4, and just puts 4 in, without checking again.
Rejigger your while loops.
Or, better yet, make a utility class to offload the job of generating a random number, but not a number that's already been returned N times, to a separate class, so that you can untangle this messy code.
Your method
while(true) {
y = (int)((Math.random()* max) + 1);
if(y != x) break;
}
does not check that count of y did not already reached most
Your Issue is here:
while(true) {
y = (int)((Math.random()* max) + 1);
if(y != x) break;
}
list[i][j] = y;
This basically just rules out that x will be repeated more than most, but not y.
On a side note, I recommend using hash maps to keep track of the occurrences instead of iterating over the whole array over and over.
This is my code:
class Main{
public static void main(String args[]){
System.out.println("Enter the value of N: ");
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int max = 0, min = 0;
if(n1<=50){
for(int i=1;i<=n1;i++){
for(int j=1;j<=n1;j++){
max = n1*i;
min = (max-n1)+1;
if(i%2!=0){
while(max<=min){
System.out.print(max);
max--;
}
}
if(i%2==0){
while(min<=min){
System.out.print(min);
min++;
}
}
}
System.out.println("");
}
}
else
System.out.print("Invalid Value of n1");
}
}
the problem is to print a zigzag matrix like
if we enter n=4 then the output should be like:
4 3 2 1
5 6 7 8
12 11 10 9
13 14 15 16
and if we enter 3 it should come like
3 2 1
4 5 6
9 8 7
now in the above code its going to a infinite loop
Considering it is most likely some kind of a homework, I won't hand you a solution as it takes away the learning process. Instead, I'll just give you some hints.
You need 2 nested for loops, the outer one for rows and the inner one for columns.
Find out what the max and min numbers in a given row are. They are connected to the row number.
If the row number is odd, start with the max number and go down. If it is odd, start from the min number and go up.
So I'm kind of new to Java and decided to create a sliding number puzzle of some sort. Here's what I have :
int[] puz = {1,2,3,
4,5,6,
7,8,9}
for(int i=0; i<puz.length; i++){
System.out.println(puz[i]);
}
The 1 is supposed to be the blank spot but I'll figure that out later. My problem is that the code prints:
1
2
3
4
5
6
7
8
9
when I want it to print:
1 2 3
4 5 6
7 8 9
I've also tried doing a nested loop that I'm too embarrassed to show on here due to how hideous it was.
Would I try using a 2d array instead?
I guess you could try...
int puz = {1,2,3,4,5,6,7,8,9};
int n = Math.ceil(Math.sqrt(puz.length));
for (int i = 0; i < puz.length; i++) {
System.out.print(puz[i] + ((i + 1) % n == 0 ? "\r\n" : " ");
}
Try creating a variable counter and increment it every time you iterate through the loop. Using a modulus operator, divide it by 3 and when remainder is 0, create a new line.
int puz = {1,2,3,4,5,6,7,8,9};
int counter = 1;
for(int i=0; i<puz.length; i++){
System.out.print(puz[i]);
if (counter % 3 == 0){
System.out.println("");
}
counter++;
}
The trick here is to use the modulus operator. This operator divides one number by another, and returns the remainder. In java (and everywhere else as far as I know), % is the modulus operator. If you want every third number to have a line break after it, simply divide by three using modulus division, like so:
int[] puz = {1,2,3,4,5,6,7,8,9};
//For what it's worth, you don't have this semicolon in your question, so I added it in.
for(int i=0; i<puz.length; i++){
System.out.print(puz[i] + " ");
if(i % 3 == 2){//It's equal to 2 because you start at 0 and not 1.
System.out.println("");
}
}
This code, when executed, prints the following, which is what you wanted:
1 2 3
4 5 6
7 8 9
Dear Professionals!
I'm a super beginner of programming java.
I'm just learning a basic stuff in school.
While I'm doing my homework, I'm stuck in one problem.
The question is Using nested loops to make this stack-up number pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
I can only use while loop (because we haven't learned for or do loops yet), and the outer loop body should execute 10 times.
I can use print and println for making this pattern.
I tried many different methods with while loop, but I can't figure it out.
Please, please give me some hint.
This is the code that I'm working on it so far:
class C4h8
{
public static void main(String[] args)
{
int i, j;
i = 1;
while(i <= 10)
{
j = 1;
while (j <= 10)
{
System.out.print(j);
j++;
}
System.out.println();
i++;
}
}
}
but it only displays:
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
My question may look like a silly one, but I'm really struggling with it because like I mentioned, I'm a super beginner..
Please help me, so that I can learn and move on!
Thank you so much!
Use the following: You need to limit the variable j by variable i to achieve your output
class C4h8
{
public static void main(String[] args)
{
int i, j;
i = 1;
while(i <= 10)
{
j = 1;
while (j <= i) // limit the variable j by i
{
System.out.print(j+" ");
j++;
}
System.out.println();
i++;
}
}
}
In less code with while loop
int i = 0;
int limit = 10;
while(++i <= limit){
int j = 0;
while(++j <= i)
System.out.print(j+" ");
System.out.println();
}
I just need something explained to me. When we declare ints It was my notion that it doesn't matter where you declare it as long as its in the beginning so I made this little bit of code to print out a multiplication table.
import java.util.Scanner;
public class Learn {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int last = 5;
int i = 1;
while (i <= last){
int j = 1;
while (j <= last) {
System.out.print(i*j);
System.out.print(" " );
j = j + 1;
}
System.out.println();
i = i + 1;
}
}
}
This prints out.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
but If I take the int j = 1; and I put it outside of the while loop above it it only prints out 1 2 3 4 5. Why does this happen?
If j = 1 is inside the loop, then it will be reset every-time that the outer loop loops.
If not it will not be reset and the condition of j <= last will not be true on subsequent loops.
When you put j outside the while loop, it's value will be initially 1 and will continue accumulating/increasing
but if j will be inside the first while loop, j will be initially 1 until 5 only. It is because it is always set to 1 every time 1 loop is executed.
To correct your notion:
You had also initialize the variable j aside from declaring it.
Declaring:
int j;
Initializing:
int i;
Initializing and Declaring:
int j=1;
:)