print sequence of number by java [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have for loop without printed value , I should put the value which will print in this for loop :
for (int i = 1; i <= 8; i++) {
out.print(_________+" ");
}
The sequence should print : 57 46 35 24 13 2 -9 -20
Any help for solve this

Your sequence should start with 57 and go down by 11 until -20 (if I am right).
So, just start the loop with 57, use >=20 as condition and execute -=11 every time:
for(int i=57;i>=20;i-=11){
out.print(i+" ");
}
In the loop body, i (that changes acvording to your sequence) and a space is being printed every time.
If you want to use the same loop, you can do this:
for (int i = 1; i <= 8; i++) {
out.print(68-11*i+" ");
}
This calculates the sequence values by taking the next higher value (than the first value) and substracted 11 for the first time, 22 for the second time etc.

Related

How to fill an array with numbers till certrain number in Java that user has entered? [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 2 years ago.
Improve this question
I would appreciate any help on this problem that I have:
User enters a numeric value, like 6.
Now program has to fill out an array of 20 elements:
for (i=0; i <= 20; i++)
But I need to fill an array till certain number that the user has entered, for example if User entered 6
then the output must be:
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2
Or if user entered 2, the output must be:
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
Arrays element count needs to be 20, not more, not less.
I get stuck in the place where I need to define that the arrays maximum valued element must be the one the User has written.
Will appreciate any help, thanks!
Have you looked into the Modulo operator? In Java, the modulo operator allows someone to get the remainder of a division. For example: 4 % 3 = 1, and 3 % 3 = 0.
https://en.wikipedia.org/wiki/Modulo_operation
Because this is homework, I’ll give you direction rather than code.
The modulus operator % returns the remainder after division.
Consider how the result of the following operation relates to the value of the desired element at index i:
i % userInput
Modulo operator is your friend here.
Just iterate ( for loop from 0 to 19 ) over array you have to fill and put index % userValue + 1 into it
I tried the Modulo operator and it worked like a charm to solve this problem. And Bohemian, you are right, it is for a homework. Thank you, my question is answered.

What is wrong with this code for generating random integers in Java??? (Unexpected Outputs) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The code below is supposed to generate random integers from 97 till 122:
for(int x = 0; x < 10; x++) {
int a = (int)(Math.random()*(26 + 97));
System.out.println(a);
}
The outputs I am getting are all over the place. They go below 97.
Here are the outputs for one of the runs:
33
113
87
73
22
25
118
29
16
21
It's a paren problem. Try this instead.
(int)(Math.random()*26) gives a number between 0 and 25 inclusive.
Add 97 to that range and you get between 97 and 122 inclusive.
int a = (int)(Math.random()*26) + 97;

Coding java program to calculate check digit numbers [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 6 years ago.
Improve this question
I have this java project(using eclipse) that calculates check digit numbers. I'm stuck on how to code this one.
The check has a check digit so that it makes the sum of the sights including the check digit divisible by 7. Assume there are always 4 digits plus the check digit. The sample is 3875 with the number being 5 Second trial problem is 5862 and check number needs to be found. How do I go about doing this? I got to entering each digit and adding them but how can i do the rest?
This is for an into to computer science class so please no super complex stuff as if we didn't learn it I cant use it.
My teacher sucks by the way we learn none of this. I already did part a I need part b. Thanks. Here's an image to hell clarify.
First of all, you need to develop some "programmer logic", these problems help to develop it.
Airline tickets divide the actual identification number by 7 and assign the remainder to the check digit. Number can be of any length
Example:
12358 #3
Let's break this example:
12358 / 7 = 1765
and the reminder is 3
Let's do the same with the 2nd number on the example:
45349 / 7 = 45346
and the reminder is 3
So, your logic is correct.
An American Express traveler's check has a digit so that it maskes the sum of the digits, including the check digit, evenly divisible by 7.
Example:
3875 #5
In this problem the thing is a little different, you need to sum the digits:
3875 -> 3 + 8 + 7 + 5 = 23
Now you need to get the reminder of 23 / 7
23 / 7 = 3
And a reminder of 2
7 - 2 = 5
That's your checkDigit
5862
5862 -> 5 + 8 + 6 + 2 = 21
21 / 7 = 3
Reminder = 0
checkDigit = 7 - 0 = 7
So the formula is:
Split the number into digits
Sum the digits
Get the mod 7 of the sum
Make a rest of 7 - reminder

Logic for Nine to one equals 100 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I was recently asked this question in an interview . Can anyone help me with a code to solve this problem?
We have a sequence of non-zero digits 123456789. The problem is to place plus or minus signs between them so that the result of thus described arithmetic operation will be 100. We can use a number only once . However we can use the operators(+,-,*,/) any number of times
Edit : I was asked to write a Java code for this question . So i believe its relevant
The followup question was to get all possible combinations
Example
Here is an example.
Split the numbers as follows
1 with multiply
4,7,89 with sum
3,6 with sum
4,5 with subtract
3+6 - 4 - 5 = 0
4 + 7 + 89 = 100
1 * 100 = 100
Automatic way of finding all possible combinations.
You essentially have 1 set:
the set {1..9} merged with the set {-,+,/,*,nothing} (nothing being the absence of a symbol)
You need to iterate over all the order possibilities. That will take for a long time. Exclude cases where there are 2 symbols side by side e.g. -/.
I believe this will lead to k-combinations.

Count the occurrence of consecutive 1s in 0-1 array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a set of 1s and 0s. How do I count the maximum number of consecutive 1s?
(For example, x = [ 1 1 0 0 1 1 0 0 0 1 0 0 1 1 1 ]). Here the answer is 3 because the maximum number of times 1 occurs consecutively is 3.
Thank you so much.
Iterate over the array, keeping the count of how many consecutive 1s you've seen. Whenever you see a 1, increment the count. Whenever you see a 0, reset the count to zero. The answer to your problem is the largest counter value seen during the iteration.
int cnt = 0,max=0;
for(int i=0;i<x.length;i++){
cnt=0;
while(i<x.length&&x[i]==1){
cnt++;
i++;
}
if(cnt>max) max=cnt;
}
this should work
Since this smells like homework, all you get is an algorithm!
Initialize counter, max
For all the elements in the Array.
If element is '1'
increment the counter
Else
max=GetMaxOf(max, counter)
reset counter
End If
End For

Categories