Determine if a number contains a specific digit [duplicate] - java

This question already has an answer here:
Determine if a number contains a digit for class assignment
(1 answer)
Closed 2 years ago.
My idea is to create a loop and not print numbers that contain a 3 in them like 13, 23, 43,etc. between 2 numbers given by a user.
My problem is on the loop. How do I check that the numbers contain a 3 on them?
For example if it prints from 2 to 24. It should not print 3,13 and 23.
for(int i = x; i <= y; i++){
if(i%3 == 0){
System.out.print("");
else{
System.out.println(i);
}
}

for(int i = x; i <= y; i++){
if(i%10 == 3){
System.out.print("");
}
else{
System.out.println(i);
}
}

Related

How to separate integer in loop? [duplicate]

This question already has an answer here:
how to separate decimal number? [closed]
(1 answer)
Closed 2 years ago.
When I try to separate decimal from others it work fine with using boolean.
But I doesn't work anymore after another integer come but.
This code is not written by me
String str = "123/0.312/43";
boolean b= false;
for(int i = 0; i < str.length(); i++){
if (b || str.charAt(i + 1) == '.'){
b = true;
System.out.print(str.charAt(i));
} else {
System.out.println(str.charAt(i));
}
}
but it only work like this
1
2
3
/
0.312/43
The number 4 and 3 are another numbers so.
How could I return?
THIS IS THE EXPECTED RESULT THANKS
1
2
3
/
0.312
/
4
3
public static void splitDigits(String str) {
boolean slash = false;
for (String part : str.split("/")) {
if (slash)
System.out.println('/');
slash = true;
if (part.contains("."))
System.out.println(part);
else {
for (int i = 0; i < part.length(); i++)
System.out.println(part.charAt(i));
}
}
System.out.println();
}

How to compute a mathematical expression given from a user? [duplicate]

This question already has answers here:
Evaluating mathematical expressions
(5 answers)
How to evaluate a math expression given in string form?
(26 answers)
Closed 3 years ago.
Say A user inputs the following 100 + 12 - 1 / 3
Using Scanner, I get that input and then split it to 100,+,12,-,1,/3
Now what I want to do is calculate the total in the order provided.
112 then 111 then 37 so the answer is 37.
This is my current thought process but now I'm stuck on how to implement it in Java for the rest of the length and operators.
This only work for "1 + 1"
or "1 / 1"
But what I need is for some variable to hold the first 2 integers result and then -,*,/ or add based on the rest of the expression.
Scanner numstr = new Scanner(System.in);
String input = numstr.nextLine();
String [] str = input.split(" ");
int sum =0;
int add =0;
int temp;
for (int i=0;i<str.length;i++) {
if (i%2 >0 && str[i].equals("+"))
{
sum = Integer.parseInt(str[i-1]) + Integer.parseInt(str[i+1]);
//sum = add;
System.out.println(sum);
}
temp =sum;
else if (i%2 >0 && str[i].equals("-"))
{
sum = Integer.parseInt(str[i-1]) - Integer.parseInt(str[i+1]);
//sum = temp - Integer.parseInt(str[i+1]);
System.out.println(sum);
}
//temp = sum;
else if (i%2 >0 && str[i].equals("/"))
{
sum = Integer.parseInt(str[i-1]) / Integer.parseInt(str[i+1]);
System.out.println(sum);
}
else if (i%2 >0 && str[i].equals("*"))
{
sum = Integer.parseInt(str[i-1]) * Integer.parseInt(str[i+1]);
System.out.println(sum);
}
else if (i%2 >0 && !str[i].equals("+")&&!str[i].equals("-")&&!str[i].equals("/")&&!str[i].equals("*"))
{
System.out.println("unknow operator");
}
}
so for 1 + 2 + 5 * 30
would be:
3,
8,
240

Understanding code to find the largest prime factor of a number [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 found this code on an old youtube video and am having the hardest time understanding the logical sequence for the output. I was hoping any of you can help clarify.
Let's assume I pass a value of 21 to the getLargestPrime method.
In line 17 the conditional statement checks whether 21 % 2 != 0, that's True. So it carries another iteration of the code. But this time m = 3, which 21 % 3 != 0, that's actually False. Why is the code still executing the else statement? This is question #1.
Question #2
How can the if statement inside the else ever execute? When does number become == 1?
I know that this is probably super basic for you guys but for some reason I cannot follow the sequence in the output.
public class LargestPrime {
public static int getLargestPrime(int number){
if(number < 0){
return -1;
}
int m = 2;
int ans = 0;
int numbern;
if(number == 1){
System.out.println("This number is not a prime");
} else{
while (ans == 0){
if(number % m !=0){
m = m +1;
System.out.println(m + " value of m");
} else {
numbern = number;
number = number / m;
if(number == 1){
System.out.println(numbern + " is the largest prime factor of your number");
ans++;
}
}
}
} return number;
}
}
This is the output:
3 value of m
4 value of m
5 value of m
6 value of m
7 value of m
7 is the largest prime factor of your number
1
21%3!=0 returns false. Because of this the jvm executes the code after else. In e.g. 21%2!=0, that is true, it executes the code after if.
It is 1, if number is as m.
This is when your calculation gets to an end because there cannot be any more numbers after your number.

Replacing Integers with Strings Java

I'm doing a basic Java tutorial and below is the question.
Write a method that prints the numbers from 1 to 100. But for multiples of three print ÒFizzÓ instead of the number,and for the multiples of five print ÒBuzzÓ. For numbers which are multiples of both three and five print ÒFizzBuzzÓ."
My code is below
public static void fizzBuzz(){
for(int i = 0; i < 101; i= i +1 ){
System.out.println(i);
if (i%15 == 0){
System.out.println("ÒFizzBuzzÓ");
}else if (i % 3 == 0){
System.out.println("ÒBuzzÓ");
}else if (i % 5 == 0){
System.out.println("ÒFizzÓ");
}
}
}
It seemingly runs fine, but on closer inspection of the output, the "Fizz" and "Buzz" lines are printed AFTER the relevant numbers and are not printed as a replacement of the numbers
For example, I get the below
9
ÒBuzzÓ
10
ÒFizzÓ
11
12
ÒBuzzÓ
13
14
15
ÒFizzBuzzÓ
16
How do I get the relevant numbers to be replaced by the correct string statements instead of what I currently have? I only managed to find tips on converting strings to integers, but not replacement of integers to strings on SO, so I would appreciate any help :)
Move printing of number in else part of your if else ladder as like:
for(int i = 1; i < 101; i= i +1 ){
if (i%15 == 0){
System.out.println("ÒFizzBuzzÓ");
}else if (i % 3 == 0){
System.out.println("ÒBuzzÓ");
}else if (i % 5 == 0){
System.out.println("ÒFizzÓ");
} else {
System.out.println(i);
}
}

Trying to properly print an array in Java

I am new to Programming so bear with me if I do not properly present my issue. I have an assignment to create a program to assign integer values 1-25 to a 25 element integer array. I need to print the array as five separate lines with each line containing 5 elements separated by commas. The last element does not have a comma following it. The final output should be as follows:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
The code that I came up with comes close, but it's not quite right. The code that I came up with is:
public class Test2 {
/**
* #param args
* the command line arguments
*/
public static void main(String[] args) {
int[] numbers = new int[25];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
System.out.print(numbers[i] + ",");
if (i % 5 == 0 && i > 0)
System.out.println();
}
}
}
The printout that I get from my code is as follows:
1,2,3,4,5,6,
7,8,9,10,11,
12,13,14,15,16,
17,18,19,20,21,
22,23,24,25,
I am not sure why I am getting 1-6 on the first line as well as how to remove the comma at the end of each line. Any help pointing out my errors would be appreciated.
The error is that you are checking if int i is divisible by 5 (i % 5), not numbers[i] (numbers[i] % 5). This way, your code prints:
number 1 when i = 0,
number 2 when i = 1,
number 3 when i = 2,
number 4 when i = 3,
number 5 when i = 4,
number 6 when i = 5
and finally prints line break.
The correct code is:
int[] numbers = new int[25];
for (int i = 0; i < numbers.length; i++) {
numbers[i]=i+1;
System.out.print(numbers[i]);
if (numbers[i] % 5 == 0 && i > 0) {
System.out.println();
} else {
System.out.print(",");
}
}
The above code will print (as intended):
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
You're getting 6 numbers on the first line, because you start counting at i=0, and only print the newline once i=5; at which point the number you've just printed is 6, not 5 - you're printing i+1 in each iteration.
If you made your logic such that it printed EITHER a comma OR a newline but not both, you'd get rid of the commas at the ends of the lines.
You're close. Very close.
Consider what your condition is checking - you want to inspect a value i, and want to stop when that particular value is divisible by 5 but is nonzero.
The problem is that you have the wrong value - i isn't what you want, but numbers[i]. The reason: each number in numbers[i] is offset of i by 1.
What you want to do is check if numbers[i] is divisible by 5. You still need to check for a nonzero i, though.
if(numbers[i] % 5 == 0 && i > 0) {
System.out.println(numbers[i]);
} else {
System.out.print(numbers[i] + ",");
}
Replace
if (i % 5 == 0 && i > 0)
with
if (i % 5 == 4)

Categories

Resources