How to print this pattern in java [closed] - java

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
**if argument multiply 3 print "JU"
if argument multiply 5 print "MP"
if argument multiply 3 and 5 print "JUMP"**
ex :
> java myclass 16
OUTPUT:
1
2
JU
4
MP
JU
7
8
JU
MP
11
JU
13
14
JUMP
16

// for each number in the sequence 1..N execute the following
for (int i = 1; i <= N; ++i) {
// print JU if divides by 3
if (i % 3 == 0) {
System.out.print("JU");
}
// print MP if divides by 5
if (i % 5 == 0) {
System.out.print("MP");
}
// note that if it divides by 3 and by 5 (by 15 actually)
// it print JU and MP in a row and it becomes JUMP
// if not dividable - print it itself
if (i % 3 != 0 && i % 5 != 0) {
System.out.print(i);
}
System.out.println();
}

This a variant of FizzBuzz and is a common challenge given to new programmers or in interviews. Why not have a look at the Rosetta Code's Java section on FizzBuzz to see a whole range of ways to do it. You'll obviously need to change Fizz for 'JU' and Buzz for 'MP'.

Try this
public static void getNumber(int upperLimit){
boolean status=true;
int i=1;
while (status) {
if(i%3==0){
System.out.println("JU");
}else if(i%5==0){
System.out.println("MP");
}else if(i%5==0&&i%3==0){
System.out.println("JUMP");
}else {
System.out.println(i);
}
if(i==upperLimit){
status=false;
}
i++;
}
}

Related

java program to loop and output 2,5,7,10,12 [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 4 months ago.
Improve this question
Loop
how to loop this without using array? I'm clearly confuse in this in java module I don't know the solution ;-; I'm new to java pls helppp mee
Try this.
for (int i = 5; i <= 25; i += 5)
System.out.println(i / 2);
Or
for (int i = 1; i <= 5; i++)
System.out.println(i * 5 / 2);
output:
2
5
7
10
12
Think for yourself why this produces the correct result.
public class Main {
public static void main(String[] args) {
int outputNum = 0;
for(int i = 0; i < 5; i++){
if (i % 2 == 0){
outputNum += 2;
}
else {
outputNum += 3;
}
System.out.print(outputNum + (i == 4 ? "" : " "));
}
}
}
This does exactly what you need.
Explanation:
In the loop if the i variable is even we add 2 to the output number (since you need to add 2 then 3 then 2...) and if i is uneven then we add three. At the end of the for loop we print the number without the newline using System.out.print and we separate them by a space if we aren't printing the last number using a ternary operator (i == 4 ? "" : " ") which returns a space if i is not equal to 4 and an empty string if it is, we do this to avoid having a space on the end of our output, so we get this 2 5 7 10 12 instead of this 2 5 7 10 12

while (1) loop with continue and break statements [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 need help with a while(1) loop that contains a continue and break statement. It must count from numbers 1 to 20 and for every even number, it must output the values. I have to use a continue after my writeToPage statement and use a break statement when it reaches 20.
This is what I tested out but the file will not even load:
writeToPage("Program 4: Continue and Break");
writeToPage("");
while(1) {
if (i % 2 == 0){
writeToPage(+ i);
continue;
}
if (i >= 20){
break;
}
}
I'm not sure if I'm putting them in the wrong place.
For infinite loop, you need to write while(true) instead of while(1).
If i % 2 != 0 is true, simply increment i by 1 and continue; otherwise, print the value of i and increment i by 1.
Demo:
public class Main {
public static void main(String[] args) {
int i = 1;
while (true) {
if (i % 2 != 0) {
i++;
continue;
} else {
writeToPage(i);
i++;
}
if (i >= 20) {
break;
}
}
}
static void writeToPage(int i) {
System.out.println(i);
}
}
Output:
2
4
6
8
10
12
14
16
18
20

What is my mistake in this problem. Simple Comparison Error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Task
Given an integer, n, perform the following conditional actions:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5 , print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird
I have written a code but it is showing error in printing 18 and 20.
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.close();
if(N%2 != 0){
System.out.println("Weird");
}
else if(N%2 ==0 && N>=2||N<=5)
{
System.out.println("Not Weird");
}
else if(N%2 ==0 && N>=6||N<=20)
{
System.out.println("Weird");
}
else if(N%2 ==0 && N>20)
{
System.out.println("Not Weird");
}
}
}
You need to change your or condition || to and condition && to check for the range like
else if(N%2 == 0 && N >= 2 && N <= 5)

Phone Call Programming Logic [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Some phone usage rate may be described as follows:
The first minute of a call costs min1 cents.
Each minute from the 2nd up to 10th (inclusive) costs min2_10 cents each minute.
After the 10th minute, the call costs min11 cents for every additional minute.
You have s cents on your account before the call. What is
the duration of the longest call (in minutes rounded down to the
nearest integer) you can have?
Input data:
For min1 = 3, min2_10 = 1, min11 = 2, and s = 20, the output should be phoneCall(min1, min2_10, min11, s) = 14.
Here's why:
The first minute costs 3 cents, which leaves you with 20 - 3 = 17 cents. The total cost of minutes 2 through 10 is 1 * 9 = 9, so you can talk 9 more minutes and still have 17 - 9 = 8 cents. Each next minute costs 2 cents, which means that you can talk 8 / 2 = 4 more minutes.
Thus, the longest call you can make is 1 + 9 + 4 = 14 minutes long.
I'm not sure what's wrong with my code's logic here.
int phoneCall(int min1, int min2_10, int min11, int s) {
int sum = 0;
if (s >= min1) {
sum++;
s = s - min1;
for (int i = 1; i <= 9; i++) {
if (s >= min2_10) {
sum = sum++;
s = s - min2_10;
} else
break;
}
sum = sum + s / min11;
}
return sum;
}
In the if statement inside of your for loop you can do one of two things here to get your return to be 14.
Change the
sum=sum++; to
sum += 1; or remove the
sum= so it its just
sum++;
This should return 14 as the sum.
sum=sum++;
should be replaced with sum++;
Read more about operator precedence operator precedence and return values
Hello #ShubhamSahay You gotta make your code like this:
public static int phoneCall(int min1, int min2_10, int min11, int s) {
int sum=0;
if(s>=min1)
{
sum++;
s=s-min1;
for(int i=1;i<=9;i++)
{
if(s>=min2_10)
{
/*Change 1*/ sum++;
s=s-min2_10;
}
else
break;
}
/*Change 2*/ sum=sum+(s/min11);
}
return sum;
}
So here is why
1st Change: You need to do just sum++
2nd Change: you gotta put those that things in brackets

How to print the even numbers out of any random number in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am looking for how to do the following.
I am looking to have a method accept a parameter of any number and have the method return the even numbers that were in the number that was the parameter. So if my number was "1234", the method would return the value 24, by removing the odd numbers (1 and 3), or pulling out the even numbers (2 and 4).
Here is what I want my main method to look like:
System.out.println("testing removeOdds: 123456 = " + removeOdds(123456)); // Returns 246
System.out.println("testing removeOdds: 24680 = " + removeOdds(24680)); // Returns 24680
System.out.println("testing removeOdds: 13579 = " + removeOdds(13579)); // Returns 0
I don't want to use a while loop or a string to do this. Thanks.
You will need
a loop (preferably a while loop)
the division operator (/)
the modulo operator (%)
a Java compiler
EDIT:
Assume your number is "n" = 1234
now n % 10 gives you 4.
n / 10 = 123.
Now if you do n % 10, you get 3.
So, put everything in a while loop with condition n > 0 do n % 10 then do n % 2. Take the value of n % 2 out and print it.
Code:
public static void main(String[] args) {
int n = 123435644;
while (n > 0) {
int val = n % 10;
if (val % 2 == 0) {
System.out.println(val);
}
n = n / 10;
}
}
O/P:
4
4
6
4
2

Categories