Create triangle with "1" " 0" by for loop [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I write a program in Java to make the following triangle?
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

Try:
String s = "";
for(int i = 0 ; i < nLines ; ++i) {
s = (i % 2 == 0 ? "1 " : "0 ") + s;
System.out.println(s);
}

I don't want to give you a solution, but there's some patterns you can see:
The number of digits is the same as the number of the line, assumign it starts in 1. For example, in the first line, you have 1 digit; in the second you have 2 digits.
If it's an odd line, the first digit is a 1; otherwise, it's a 0.
You always switch between 0 and 1, until you've reached the number of the line.

public class CurvedZebraTriangle{
public static void main(String []args){
int n=5;
for(int i = 0; i <= n; ++i)
{
for(int j = 0; j< i; j++)
System.out.print((i+j) % 2 == 0 ? "0 " : "1 ");
System.out.print("\n");
}
}
}

Related

Write a program to print the following pattern using a nested loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
The pattern of 2n+1 should be stair case
1
1 4
1 4 9
1 4 9 16
1 4 9 16 25
I'm trying to imply the 2n+1 equation into the for loop. I'm messing around for an hour but still can't think a way to solve this problem. This is what I have tried so far.
public class hw5{
public static void main(String[] args){
for(int i=1 ; i <= 5; i++){
for(int j=1; j <= 25; j = j + 3){
System.out.print(j);
}
System.out.println();
}
}
}
The series printed is just the square of integers starting at one. So just save each of the previous squares in a string.
String s = "";
int max = 5;
for(int i = 1; i <= max; i++) {
System.out.println(s+= (i*i)+" ");
}
prints
1
1 4
1 4 9
1 4 9 16
1 4 9 16 25
However, starting with 1, the sum of the consecutive odd numbers also form perfect squares. And 2n+1 is the representative form of an odd number for any integral value of n.
So you can also do it like this.
compute the sums of all the computed odd numbers in the loop
after each sum is computed, append to a string and print
int sum = 0;
String s = "";
int max = 5;
for (int n = 0; n < max; n++) {
sum += (2*n + 1);
System.out.println(s += sum + " " );
}
prints
1
1 4
1 4 9
1 4 9 16
1 4 9 16 25
for (int i=1; i<6; i++) {
for (int j=1; j<i+1; j++) {
System.out.print(j*j+" ");
}
System.out.println();
}

How I can sum all numbers in array and print them as integer value in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
enter code hereI have this number "811218-3476"as string,I want to multiply 8 by 2,1 by 1, 1 by2, 2 by1 and so on as following:
8 1 1 2 1 8 3 4 7 6
2 1 2 1 2 1 2 1 2 1 *
16 1 2 2 2 8 6 4 14 7 ----> Result
My question is how I kan do sum for the result, I did sum one number with another number like
16 + 1+2+2+2+8+6+4+14+7 = 62,
but I want to do sum as following:
1+6+1+2+2+2+8+6+4+1+4+7 = 47.
I do not need you to write a code I have wrote it, but I want to know how I kan sum 1+6 instead of 16 as example. my code is here and it works fine.
I hope help know.
Thanks.
enter code here
public static boolean checknumber(String s) {
if(checkPersonNummer(s)== true) {
char [] charray = s.toCharArray();
int newch = 0 ;
int j = 0;
int i = 0;
String sum= "";
int x = 0;
for( j = 2; j < 8 ; j++) {
System.out.print(" "+ charray[j] + " ");}
for( j = 9; j < charray.length ; j++) {
System.out.print(" "+ charray[j] + " ");}
System.out.println(" ");
for( j = 2; j < 8 ; j++) {
if(j%2 == 0) {
System.out.print(" "+ 2 + " ");
} else {
System.out.print(" "+ 1 + " ");
}
}
// System.out.println(" ");
for( j = 9; j < charray.length; j++) {
if(j%2 == 0) {
System.out.print(" "+ 2 + " ");
} else {
System.out.print(" "+ 1 + " ");
}
}
System.out.print("\n--------------------------------------------");
System.out.println("");
for( i = 2;i < 8;i++) {
if(i% 2 == 0) {
newch = Character.getNumericValue(charray[i] )* 2;
sum += newch;
}
else {
newch = Character.getNumericValue(charray[i]) * 1;
sum += newch;
}
System.out.print(newch + " " );
}
for( i = 9;i < charray.length;i++) {
if(i% 2 == 0) {
newch = Character.getNumericValue(charray[i] )* 1;
sum += newch;
}
else {
newch = Character.getNumericValue(charray[i]) * 2;
sum += newch;
}
System.out.print(newch + " " );
}
System.out.println();
System.out.print("Total = " + sum);
}
return true;
}
}
This sounds like a homework assignment, and we're not a homework writing team. But I'll give you some basic advice.
First, I had one person tell me, "programming is the art of editing the null program until it does what you want". By that he meant that we start with a program that does nothing and slowly built it up.
That's a good way to start.
So... Start your program. You need to get your data in. Do that, and figure out how to print it.
After that, think about printing out each number times either 1 or 2 as your problem requires, and print each calculation as you go.
Then all you have to do is keep a variable outside of this loop to store the sum, and add each mini-calculation to it, then print it at the end.
Start small. Work up to the final answer. Lots of debug output while working on it.
Lets say I have an array of numbers [2,3,4,5,6], Think of this question as you are multiplying the numbers with 1 those are at odd position in the array and multiplying the numbers with 2 those are at even position in the array.
Answer to How to add 1 + 6 instead of 16
Check if the number is less than 10, if it is than you don't have to
worry about it But,
If the number is greater than 10 you can use the % operator.
For instance
15 % 10 gives you 5 and 15 / 10 gives you 1 that way you can separate two individual digits.

How to set counter with predefined variable in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to set counter with variable to start number as variable. e.g my variable "COUNT" could be any value between 1 to 12 and followed by remaining numbers. Consider this
if COUNT = 3 then my series of number should go like below
3 (Assigned run time variable)
4
5
6
7
8
9
10
11
12
1
2
Many thanks for help in advance.
There are a couple of ways to do this - you can play with the modulus operator, % or you can take advantage of the fact that a for-loop can increment more than one variable at a time. So you use one variable to control the loop count, and another for the print value:
public static void main(String[] args) {
int x=3;
for (int i=0; i<12; i++, x++) {
if (x>12) {
x=1;
}
System.out.println(x);
}
}
Here is the output:
...
3
4
5
6
7
8
9
10
11
12
1
2
Process finished with exit code 0
You can do this in single for loop like this.
public static void main(String[] args) {
int COUNT = 3;
for (int i=0; i<12; i++) {
if (i + COUNT > 12) {
System.out.println(i + COUNT - 12);
} else {
System.out.println(i + COUNT);
}
}
}
int countStart = 3;
for(int count = countStart; count < 13; count++){
System.out.println(count);
}
for(int count = 1; count < countStart; count++){
System.out.println(count);
}
The code starts from COUNT which is written here as countStart, and keeps printing until it reaches 12.
From here, a new loop runs from 1 to countStart, printing all the values
With only one loop
int count=3;
for(int i=count;i<count+13;i++){
if(i<13)
System.out.println(i);
else if(i>13){
System.out.println(i-13);
}
}
}
Here your count start from 3
it will iterate the loop through 13+count to print all value
after that it will check if the i vale lessthan 13 then it will print the i value else it print i-13 from 1,2 ... upto cont-1

Java Nested loop to print numbers in a descending triangle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have to write a nested loop fragment that will have the following output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Numbers have to be in that exact order, i'm trying to use for loops but I can't really figure out how to remove the repeating numbers.
How would I achieve this using nested loops?
The output i'm currently getting looks like:
1
1 2
1 2 3
1 2 3 4
etc.
Nevermind, I just figured it out!
int x = 1;
for(int i = 0; i < 5; i++){
for(int j = 0; j <= i; j++ ){
System.out.print(x + " ");
x++;
}
System.out.println();
}
You want to have an external count, not base the value you're printing out on j. Thus increase the count in the nested loop every time and print that value.
Write a code something like below. I didn't check the syntax, that is the work for you.
for (int i = 0 ; i < k; i++){
for(int j = 0; j <= i; j++){
System.out.print(j + " ");
}
System.out.println();
}

Write a program that displays all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Write a program that displays all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6.
this is my program:
for (int i =100; i<= 1000; i++){
if(i % 5==0 && i % 6==0)
System.out.print(i +" ");
if (i %10==0){
System.out.println();
}
You can't really use modulo division here because you aren't keeping track of the number of times you have printed a number. Simply add a counter, and place a new line every time that counter is equal to 10.
For example:
int counter=0;
for(int i=100;i<=1000; i++) {
if(i % 5==0 && i % 6==0) {
System.out.print(i +" ");
counter++;
}
if(counter==10) {
System.out.println();
counter=0;
}
Corrected for cross system compatibility
Version without counter (wont work for all sets of numbers)
for(int i=100;i<=1000; i++) {
if(i % 5==0 && i % 6==0)
System.out.print(i +" ");
if(i!=100 && (i - 100) % 300 == 0)
System.out.println();
}

Categories

Resources