Nested For loops to print numbers 11223344556677889900 - java

I am trying to print the sequence of numbers 11223344556677889900 using nested for loops. I am unsure of the algorithm to print the sequence since it ends in 00. I have the following method code but I print the 00 as literals and am sure that there must be a better way. Any help is much appreciated.
public static void drawNumbers(){
for (int line = 1; line <= 2; line++) {
for (int i =1; i <= 9; i++) {
for (int j =1; j<=2; j++) {
System.out.print(i);
}
}
System.out.print("00");
}
}

Run your loop up to 10 and instead of:
System.out.print(i);
do:
System.out.print(i % 10);
public static void drawNumbers(){
for (int line = 1; line <= 2; line++){
for (int i =1; i <= 10; i++){
for (int j =1; j<=2; j++){
System.out.print(i%10);
}
}
}
}

You can run the "i" loop until 10 and print i%10. This would print 0 when the value reaches 10.

One other way would be to insert an if statement inside the for where you would check if the value of i was equal to "10", and if it were, it would print the value "00"

Just only print the last digit of i
for (int i = 1; i <= 10; i++) {
String s = String.valueOf(i);
int lastDigit = s.length() - 1; // last digit position in string
System.out.print(s.substring(lastDigit, lastDigit + 1)); // print last digit
System.out.print(s.substring(lastDigit, lastDigit + 1)); // print last digit
}
Output:
11223344556677889900

Changed the logic a little to fit your requirement
public static void drawNumbers() {
for (int line = 1; line <= 1; line++) {
for (int i =1; i <= 10; i++) {
for (int j =1; j<=2; j++) {
if(i==10) {
System.out.print(0);
} else {
System.out.print(i);
}
}
}
}
}
To use only one loop run it like
public static void drawNumbers() {
for(int i = 1 ;i <= 10; i++) {
if(i ==10) {
System.out.print(0 + "" + 0);
} else {
System.out.print(i + "" + i);
}
}
}

Related

Trouble understanding parts of this code using for-loops?

public class PA4 {
public static void main(String[] args) {
for (int line = 1; line <= 6; line++) {
for (int j = 1; j <= (line - 1); j++) {
System.out.print(".");
}
System.out.print(line);
for (int j = (line + 1); j <= 6; j++) {
System.out.print(".");
}
System.out.println();
}
}
}
This code produces this output:
1.....
.2....
..3...
...4..
....5.
.....6
I understand the first loop and how it prints the dots by subtracting one from each line but I can't understand how the second loop works and how it prints the dots, or how assigning "j" the value of "line + 1" does whatever it's doing.
Maybe this is clearer; it does the same thing.
public class PA4 {
public static void main(String[] args) {
for (int line = 1; line <= 6; line++) {
for (int j = 1; j <= 6; j++) {
if (j == line)
System.out.print(line);
else
System.out.print(".");
}
System.out.println();
}
}
}
Your inner portion (two loops plus), together, always count up to 6, printing dots and one number. But they do it by counting up to line-1, then printing the number, then printing more dots, counting from just after the number up to 6. I've done it above in a single loop, but it's the same thing.
Corrected indentation
for (int line = 1; line <= 6; line++) {
for (int j = 1; j <= (line - 1); j++) {
System.out.print(".");
}
System.out.print(line);
for (int j = (line + 1); j <= 6; j++) {
System.out.print(".");
}
System.out.println();
}
replacing with a hard-coded value
// for three it would be
for (int j = 1; j <= (2); j++) { // personally I would do j < 3
System.out.print(".");
}
System.out.print(3);
for (int j = (4); j <= 6; j++) {
System.out.print(".");
}
System.out.println();
output
..3...

JAVA Numberline errors

I am trying to make the following number line is Java
2,3,5,7,11,13,17 (Prime Numbers)
I tried this code
for(int i =0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < 1; j++) {
if(i % j == 0) {
break;
} else {
System.out.print(i + ",");
}
}
}
But it doesn't work
Anyone help please?
Your code is quite poor, but the minimal amount of changes needed to make it work yields this code:
outerLoop:
for(int i = 0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < i; j++) {
if(i % j == 0) {
continue outerLoop;
}
}
System.out.print(i + ",");
}
But a further improvement would be to start the first loop at 2 right away:
outerLoop:
for(int i = 2; i <= 100; i++) {
for(int j = 2; j < i; j++) {
// and so on...
EDITED
There are a lot of errors in that code, first of all that second loop is a infinite loop and second one that the System.out.println line should not be in second loop it should be at end of first loop! If you place it in second it will print numbers hundreds of time.
This is the correct code :
for(int i = 2; i <= 100; i++)//begin loop from 2 instead of 0
{
boolean flag = true;
for(int j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = false;
break;
}
}
if(flag)System.out.print(i + ",");
}
You need to set a flag to check if a factor was found outside the loop.

Different variations for using loops to a pattern in java

I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern
1
121
12321
1234321
My code is the following but doesn't work like the example above.
for (int i = 1 ; i <= rows ; i++) {
for (int j = (rows + 1 - i) ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.print("\n");
}
Your code prints only the suffix of each line, you are missing to write 12....i for each line.
In addition, the loop should start from i, not from rows-i+1.
for (int i = 1 ; i <= rows ; i++) {
//add an inner loop that prints the numbers 12..i
for (int j = 1 ; j < i ; j++ ) {
System.out.print(j);
}
//change where j starts from
for (int j = i ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.println(""); //to avoid inconsistency between different OS
}
First note that 11*11 = 121, 111*111=12321, etcetera.
Then that 10n - 1 is a number that consists of n 9's, so (10n - 1)/9 consists of n 1's.
So we get:
int powerOfTen = 1;
for (int len = 0; len < 5; len++)
{
powerOfTen = powerOfTen*10;
int ones = (powerOfTen-1)/9;
System.out.println(ones*ones);
}
Code explains everything!
public static void main(String[] args) {
String front = "";
String back = "";
int rows = 5;
for (int i = 1; i <= rows; i++) {
System.out.println(front+i+back);
front += i;
back = i + back;
}
}
Try this one: it may seems too much looping, but yet easy to understand and effective.
public static void main(String[] args) {
int rows=5;
int i,j;
for(i=1;i<=rows;i++)
{
/*print left side numbers form 1 to ...*/
for(j=1;j<i;j++)
{
System.out.printf("%d", j);
}
/*Print the middle number*/
System.out.printf("%d", i);
/*print right numbers form ... to 1*/
for(j=i-1;j>0;j--)
{
System.out.printf("%d", j);
}
System.out.println("");
}
}
int n=0;
for(int m =0; m<=5; m++){
for(n= 1;n<=m;n++){
System.out.print(n);
}
for(int u=n;u>=1;u--){
System.out.print(u);
}
System.out.print("");
}

Asterisk in Nested loops, Java

I am trying to make my code print out the Asterisk in the image, you see below. The Asterisk are align to the right and they have blank spaces under them. I can't figure out, how to make it go to the right. Here is my code:
public class Assn4 {
public static void main(String[] args) {
for (int i = 0; i <= 3; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
for (int x = 0; x <= 1; x++) {
System.out.println(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
}
System.out.println();
}
}
Matrix problems are really helpful to understand loops..
Understanding of your problem:
1) First, printing star at the end- That means your first loop should be in decreasing order
for(int i =7;i>=0; i+=i-2)
2) Printing star in increasing order- That means your second loop should be in increasing order
for(int j =0;j<=7; j++)
Complete code:
for(int i =7;i>=0; i=i-2){ // i=i-2 because *s are getting incremented by 2
for(int j =0;j<=7; j++){
if(j>=i){ // if j >= i then print * else space(" ")
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println();// a new line just after printing *s
}
Starting loops with 1 can sometimes help you visualize better.
int stopAt = 7;
for (int i = 1; i <= stopAt ; i += 2) {
for (int j = 1; j <= stopAt; j++) {
System.out.print(j <= stopAt - i ? " " : "*");
}
System.out.println();
}
Notice, how each row prints an odd number of *s ending at the line with 7. So, you start with i at 1 and go through 3 1+2, 5 3+2, and then stopAt 7 5+2.
The nested for loop has to print 7 characters always to make sure *s appear right aligned. So, the loop runs from 1 to 7.
Here the complete code:
for(int i = 0; i < 8; i++){
if( i%2 != 0){
for(int x = 0; x < i; x++){
System.out.print("*");
}
}else{
System.out.println();
}
}

Find the prime-->sieve way

I tried it several times but still gives me ArrayOutOfIndex. But i want to save the memory so i use
boolean[]isPrime = new boolean [N/2+1];
instead of
boolean[]isPrime = new boolean [N+1];
This gives me ArrayOutOfIndex for line 23 and 47
line 23:
for (int i = 3; i <= N; i=i+2) {
isPrime[i] = true;
}
line 47:
for (int i = 3; i <= N; i=i+2) {
if (isPrime[i]) primes++;
...
}
Full code:
public class PrimeSieve {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java PrimeSieve N [-s(ilent)]");
System.exit(0);
}
int N = Integer.parseInt(args[0]);
// initially assume all odd integers are prime
boolean[]isPrime = new boolean [N/2+1];
isPrime[2] = true;
for (int i = 3; i <= N; i=i+2) {
isPrime[i] = true;
}
int tripCount = 0;
// mark non-primes <= N using Sieve of Eratosthenes
for (int i = 3; i * i <= N; i=i+2) {
// if i is prime, then mark multiples of i as nonprime
if (isPrime[i]) {
int j = i * i;
while (j <= N){
tripCount++;
isPrime[j] = false;
j = j + 2*i;
}
}
}
System.out.println("Number of times in the inner loop: " + tripCount);
// count and display primes
int primes = 0;
if(N >= 2 ){
primes = 1;
}
for (int i = 3; i <= N; i=i+2) {
if (isPrime[i]) primes++;
if (args.length == 2 && args[1].equals("-s"))
; // do nothing
else
System.out.print(i + " ");
}
System.out.println("The number of primes <= " + N + " is " + primes);
}
}
You should store and access the array using the same indexing function: isPrime[i/2]
When you change the size of your array from [N+1] to [N/2+1], you need to also update the end-conditions of your for-loops. Right now your for-loops run until i=N, so you are trying to do isPrime[i] when i > (N/2+1) ... so you get an ArrayIndexOutOfBoundsException.
Change this:
for (int i = 3; i <= N; i=i+2)
to this:
for (int i = 3; i <= N/2; i=i+2)
Well, for example if N=50 your isPrime only holds 26 elements, and you're trying to access the elements at 3,5..47,49 (which, of course, is out of bounds)
What you probably want is to use i/2 (as the index) inside your loops, that way you are still iterating over the numbers 3,5..47,49, but you use the correct indexes of your vector.

Categories

Resources