A sequence of whole numbers, check if it is ordered true (in ascending or descending order), otherwise it is false. If a number has the same value as the number below, it will not break the order. The sequence ends with 0.
Sample Input 1: 9 8 7 6 5 4 3 2 1 0
Sample Output 1:true
--------------------------------
Sample Input 2: 1 2 3 3 9 0
Sample Output 2:true
--------------------------------
Sample Input 3: 1 2 5 5 2 3 0
Sample Output 3: false
--------------------------------
I need help, I've been trying for days ... I really appreciate any help ...
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = 0;
while (s.hasNextInt()) {
int i = s.nextInt();
a = i;
if (i < a) {
if (i < a) {
System.out.println("true");
} else if (i > a) {
System.out.println("false");
break;
}
} else if (i > a) {
if (i > a) {
System.out.println("true");
} else if (i < a) {
System.out.println("false");
break;
}
} else if (i == a) {
}
}
}
}
I will not tell you the code as that would not be of much help but I can help with the approach you need to take.
With first 2 inputs assess if the pattern will be increasing or
decreasing.
Then with the pattern check if the number is always = or less/greater than the number
Check for the last value. It must be 0 but it may or may not be according to pattern (in some cases it may come as the correct pattern itself confusing if it is a valid number or end of list)
If last number is not 0 then the output should be false.
I assume 0 can't appear anywhere but at the end.
static boolean ordered(Scanner s)
{
int curr, prev;
curr = prev = s.nextInt();
while(s.hasNextInt() && (curr = s.nextInt()) == prev);
if(curr < prev)
while(s.hasNextInt() && (curr = s.nextInt()) <= prev) prev = curr;
else
while(s.hasNextInt() && (curr = s.nextInt()) >= prev) prev = curr;
return curr == 0;
}
Test:
public static void main(String[] args)
{
test("0");
test("1 0");
test("1 1 0");
test("9 9 8 7 6 6 5 4 3 2 1 0");
test("1 1 2 3 3 3 9 0");
test("1 2 5 5 2 3 0");
test("9 8 7 6 7 8 9 0");
}
static void test(String str)
{
System.out.format("%s : %b%n", str, ordered(new Scanner(str)));
}
Output:
0 : true
1 0 : true
1 1 0 : true
9 9 8 7 6 6 5 4 3 2 1 0 : true
1 1 2 3 3 3 9 0 : true
1 2 5 5 2 3 0 : false
9 8 7 6 7 8 9 0 : false
You need to change your code like this:
Scanner sc = new Scanner(System.in);
int prev = sc.nextInt();
int curr = sc.nextInt();
while (sc.hasNextInt() && prev == curr) {
prev = curr;
curr = sc.nextInt();
}
boolean flag = prev < curr;
while (sc.hasNextInt()) {
prev = curr;
curr = sc.nextInt();
if (prev < curr && flag) {
System.out.println("Ascending");
} else if (prev > curr && !flag) {
System.out.println("Descending");
} else if (prev == curr) {
System.out.println("Equal");
} else {
System.out.println("Not sorted");
break;
}
}
You can put this in a method and return false from the else and return true from the end of the method.
Based on the first two inputs (you will need a counter variable e.g. count), decide whether the remaining numbers should be in ascending order or in descending order. You can use a boolean variable e.g. asc to store this result i.e. if the second number is greater than the first number, the value of asc will be true; otherwise, false.
Once you have decided the value of asc from the first two numbers, you need to check if the next number follows this pattern or not. If the next number doesn't follow the pattern, print false and break the processing.
For every number that the scanner reads, you also need to check if it is 0. If yes, print true and break the processing. Also, since your requirement mentions, "If a number has the same value as the number below, it will not break the order.", simply continue when the number read by the scanner has the same value as the last read number.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean asc = true;
int i = 0, j = 0, count = 0;
while (true) {
if (count < 2) {
i = s.nextInt();
if (i == 0) {
System.out.println(true);
break;
}
count++;
} else {
// Store the last input to `i` and read a new number into `j`
i = j;
j = s.nextInt();
// Continue if the new number has the same value as the last read number
if (i == j) {
continue;
}
if (j == 0) {
System.out.println(true);
break;
}
count++;
}
if (count <= 2) {
j = s.nextInt();
// Continue if the new number has the same value as the last read number
if (i == j) {
continue;
}
if (j == 0) {
System.out.println(true);
break;
}
count++;
}
// Based on the first two inputs decide whether the remaining numbers should be
// in ascending order or in descending order.
if (count == 2 && j < i) {
asc = false;
}
// Check if the next number (i.e. the value of `j`) follows this pattern or not
if ((asc == true && j < i) || (asc == false && j > i)) {
System.out.println(false);
break;
}
}
}
}
A sample run:
9 8 7 6 5 4 3 2 1 0
true
Another sample run:
1 2 3 3 9 0
true
Another sample run:
1 2 5 5 2 3 0
false
Another sample run:
9 9 8 0
true
Another sample run:
5 5 6 0
true
The above code is failing on one test condition.
Test Condition:
4 4 1 2 3 0
Minor change needed in the above code i.e, if (i == j && i<j)
So the code will look like:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean asc = true;
int i = 0, j = 0, count = 0;
while (true) {
if (count < 2) {
i = s.nextInt();
if (i == 0) {
System.out.println(true);
break;
}
count++;
} else {
i = j;
j = s.nextInt();
if (i == j) {
continue;
}
if (j == 0) {
System.out.println(true);
break;
}
count++;
}
if (count <= 2) {
j = s.nextInt();
if (i == j && i < j) {
continue;
}
if (j == 0) {
System.out.println(true);
break;
}
count++;
}
if (count == 2 && j < i) {
asc = false;
}
if ((asc == true && j < i) || (asc == false && j > i)) {
System.out.println(false);
break;
}
}
}
}
Related
This question already has answers here:
Java - AND operator not working
(4 answers)
Closed 4 years ago.
I'm trying to write a code that finds the multiples of 3 and 5 in an array of numbers 1 to 100, the code I have generates the numbers I want but it gives me the multiples of 3, then gives me the multiples of 5 (Example: 3 6 9 12 15,5 10 15) I want them all together (Example 3 5 6 9 10 12 15).
here is the code I have so far
for(int i = 0 ; i < 100; i=i+3){
if(i%3 == 0)
System.out.println(i);
}
for (int i=1; i < 100; i++) {
if (i%5==0) System.out.println(i);}
I also tried
if(i%3 == 0 && i%5==0)
but that only gave me the numbers divisible by both
an explanation after would be helpful thank you
You want numbers that are divisible by 3 OR divisible by 5. Therefore you should use || (OR) instead of && (AND):
for (int i = 0 ; i < 100; i++) {
if(i%3 == 0 || i%5==0) {
System.out.println(i);
}
}
You can try the following to get 3,5,6,9,10,12,15..
for(int i = 0 ; i < 100; i++) { //Loop from 0 to 100
if(i % 3 == 0 || i % 5 == 0 ) //check the number is divisible by 3 or 5
System.out.println(i); //print the number if it is divisible by 3 or 5
}
for(int i=1;i<=100;i++){
if(i%3==0||i%5==0)
System.out.println(i)
}
The above code give you the number which can be divided either by 3 or 5
Didn't put too much time into this but what about just using a variable so it doesn't repeat:
public class main {
public static void main(String[] args) {
boolean printed = false;
for(int i = 0 ; i < 100; i++){
printed = false;
if(i%3 == 0 && printed == false){
System.out.println(i);
printed = true;
}
if (i%5==0 && printed == false) System.out.println(i);}
}
}
public class Multiple {
public static void main(String[] args) {
int i;
for (i = 0; i <= 100; i++) {
if (i % 3 == 0 || i % 5 == 0) {
System.out.println(i);
}
}
}
}
I have a beginner question about generating Fibonacci numbers in Java.
In this java program, the method should print Fibonnacci numbers sequence from BeginIndex to LastIndex, so it should print "Hi" (instead of number) if the number is a multiple of 5, print "I am" if the number is a multiple of 7 and print "Hi I am me" if the number is a multiple of 35. I am not really sure on how to do this.
class FibonacciClass {
public static void main(String args[] ) throws Exception {
generatefibonacci(10, 20);
generatefibonacci(0, 1);
}
private static void generatefibonacci(int BeginIndex, int LastIndex) {
}
Another possibility:
private static void generateFibonacci(int beginIndex, int lastIndex) {
int len = lastIndex + 1;
int[] fib = new int[len];
fib[0] = 0;
fib[1] = 1;
// Building Fibonacci sequence from beginIndex through lastIndex
for (int i = 2; i < len; ++i)
fib[i] = fib[i-1] + fib[i-2];
// Printing
for (int index = beginIndex; index <= lastIndex; ++index) {
if ((fib[index] % 5 == 0) && (fib[index] % 7 == 0)) {
System.out.println("Hi I am me");
}
else if (fib[index] % 5 == 0) {
System.out.println("Hi");
}
else if (fib[index] % 7 == 0) {
System.out.println("I am");
}
else {
System.out.println(fib[index]);
}
}
}
What you're looking for is the modulus operator, %. It will return the remainder of division by its operands. So, receiving a true value from if (x % 5 == 0 && x % 7 == 0) would represent the number x being both multiples of 5 and 7. If this case does not pass, you should use else if statements to individually check for if x is a multiple of 5 and then another for if x is a multiple of 7, with each if branch calling System.out.println("x is a multiple of y");
Everytime generatefibonacci has produced a result you have to check with modulo (%).
Like this:
private static generatefibonnacci(int startIndex, int endIndex){
int result = -1;
//do generating stuff
//and set "result" to generated fibonacci
//and then
if(result%5==0){
System.out.println("Hi");
} else if(result%7==0){
System.out.println("Hi I am me!");
} //and so on
So this is just a little example.
Have fun
private static void generatefibonacci(int BeginIndex, int LastIndex) {
int[] numbers = new int[LastIndex + 2]; //creates an array to put fibonacci numbers in
numbers[0] = 1; numbers[1] = 1;
for(int i = 2; i <= LastIndex; i ++){
numbers[i] = numbers[i - 1] + numbers[i - 2]; //generates the Fibonacci Sequence
}
for(int i = BeginIndex; i <= LastIndex; i ++){
if(numbers[i] % 5 == 0 && numbers[i] % 7 == 0){//if the remainder when the numbers/5 equals 0 and the remainder when the numbers/7 equals 0
System.out.println("Hello I am me");
}
else if(numbers[i] % 5 == 0){ //if the remainder when the numbers/5 equals 0
System.out.println("I am");
}
else if(numbers[i] % 7 == 0){ //if the remainder when the numbers/7 equals 0
System.out.println("Hi");
}
else{ //if none of the above three conditions are true
System.out.println(numbers[i]);
}
}
}
I'm not sure why my ct is not going all the way to 100 even though I clearly set it to go until it reaches 100.
public class PalindromicPrime
{
public static void main(String [] args)
{
int ct = 0;
while(ct < 100)
{
if(isPalindrome(ct) && isPrime(ct))
{
if(ct % 10 != 0)
{
System.out.print(ct + " ");
}
else
{
System.out.print("\n");
}
}
ct++;
}
public static boolean isPalindrome(int p)
{
int palindrome = p;
int reverse = 0;
while (palindrome != 0)
{
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
if (p == reverse)
{
return true;
}
return false;
}
I'm assuming my isPrime code is wrong since I'm getting a 4 in my output. What's wrong with this method?
public static boolean isPrime(int p)
{
for(int i = 2; i < p/2; i++)
{
if(p % i == 0)
{
return false;
}
}
return true;
}
}
First change you should do in your method isPrime() is change this line
for(int i = 2; i < p/2; i++)
to
for(int i = 2; i <= p/2; i++) // you can easily find the reason why it is necessary(=)
And also you are printing palindromic numbers less than 100 which are prime,not first 100 palindrome numbers, if you want to print first 100 palindrome numbers you can take another counter which will keep track of the numbers printed.
You can modify your main method like this:
public static void main(String [] args)
{
int ct = 0,n=0; // n to count numbers printed/found
while(n < 100) // change the condition to check n<100
{
if(isPalindrome(ct) && isPrime(ct))
{
System.out.print(ct + " ");
if(n % 10 == 0)
{
System.out.println();
}
n++; // incementing n after a number is found!
}
ct++;
}
}
Your palindrome method is fine. It's your isPrime method that's not working because to check if a number is prime, you're supposed to test factors up to the square root of the number. So a simple change in the condition should do it,
public static boolean isPrime(int p)
{
for(int i = 2; i <= Math.sqrt(p); i++)
{
if(p % i == 0)
{
return false;
}
}
return true;
}
}
Change your isPrime function to following (replace < with <= as 4/2 is 2 and loop will not run at all for p=4):
public static boolean isPrime(int p) {
for (int i = 2; i <= p / 2; i++) {
if (p % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int ct = 2;
int count = -1;
while (count < 99) {
if (isPalindrome(ct) && isPrime(ct)) {
count++;
if (count % 10 == 0) {
System.out.print("\n" );
}
System.out.print(ct + " ");
}
ct++;
}
}
The only numbers that are palindrome and prime and less than 100 are:
1 2 3 5 7 11
Try changing the value of 100 to 102. Then you get the following output as 101 is the next palindromic prime after 11:
1 2 3 5 7 11 101
Been digging around the internet just about all day looking for a solution to this problem. I've just started programming about 2 weeks ago nearly and the fact that I don't really know the proper java lingo yet could be the reason I'm not finding what I'm looking for.
Anyway the program is supposed to:
print out all the numbers from 1 to 100 and if during the iteration the program comes across a number that's divisible by 3 it has to replace that number with "Crackle", if it comes across a number that's divisible by 5 it has to replace that number with "Pop" and it comes across a number that's divisible by both then it has to replace that number with "CracklePop
I can't get it to print CracklePop for numbers that are divisable by both 3 and 5, but I can get it to print Crackle & Pop but not replace the numbers.
I've tried using:
Integer.toString(); , Integer.valueOf(); , Integer.parseInt();
None of which I can get to work. This is the code so far:
int counter = 0;
int max = 100;
for (int i = 1; i <= max; i++){
if (counter % 3 == 0) {
System.out.println("Crackle");
}
else if(counter % 5 == 0){
System.out.println("Pop");
}
else if (counter % 3 == 0 && counter % 5 == 0){
System.out.println("CracklePop");
}
System.out.println(counter);
counter++;
}
If if you could also suggest a solution that would be the most robust way of writing a program like this that would be good too.
There is no need for the counter variable. You can just use the i counter itself.
Also you should place the check for both being divisible by 3 and 5 first:
int max = 100;
for (int i = 1; i <= max; i++){
if (i % 3 == 0 && i % 5 == 0){
System.out.println("CracklePop");
} else if (i % 3 == 0) {
System.out.println("Crackle");
} else if(i % 5 == 0){
System.out.println("Pop");
} else {
System.out.println(i);
}
}
The last else will make sure you replace the number with the corresponding string if it matches one of the conditions of being divisible by 3 and/or 5.
for counter = 15 your last 2 else if will never get invoked, you need to re order your if else like
if (counter % 5 == 0 && counter % 3 == 0){
System.out.println("CracklePop");
} else if (counter % 3 == 0) {
System.out.println("Crackle");
} else if(counter % 5 == 0){
System.out.println("Pop");
} else{
System.out.println(counter);
}
still you can store result of % in a boolean variable to avoid multiple time calculation in worst case
You need to put your last else/if statement at the beginning. The problem is, when one of your if statements is valid, it will execute that statement and end the if statement. Your last statement will never be executed. Your statement should be:
public class CodeCracklePop {
public static void main(String[] args) {
int counter = 0;
int max = 100;
for (int i = 1; i <= max; i++){
if (counter % 3 == 0 && counter % 5 == 0){
System.out.println("CracklePop");
}
else if (counter % 3 == 0) {
System.out.println("Crackle");
}
else if(counter % 5 == 0){
System.out.println("Pop");
}
System.out.println(counter);
counter++;
}
}
}
The reason the numbers aren't replaced is because the Crackle and Pop are printed, but the numbers are also printed afterwards. CracklePop never is printed because anything that would fulfill it would just print Crackle instead. You need to reorder your logic in order for it to fit your needs.
Replace
System.out.println(counter);
With
else {
System.out.println(counter);
}
In addition, as Jigar pointed out above you should test for 3 and 5 first
Try this one
public static void main(String[] args) {
int max = 100;
for (int i = 0; i < max; i++) {
if (i % 3 == 0 || i % 5 == 0) {
if (i % 3 == 0) {
System.out.print("Crackle");
}
if (i % 5 == 0) {
System.out.print("Pop");
}
} else {
System.out.print(i);
}
System.out.println();
}
}
public class CodeCracklePop {
public static void main(String[] args) {
/* int counter = 0; omit this*/
int max = 100;
boolean flag = false;
for (int i = 1; i <= max; i++){
if(i % 3 == 0){
System.out.print("Crackle");
flag = true;
}
if(i%5==0){
System.out.print("Pop");
flag = true;
}
if(!flag)
System.out.print(""+i);
flag = false;
System.out.println("");
}
}
}
A fun way of getting the job done.
I can't seem to figure out what is wrong here. In the third scenario if (i == n && i % 2 == 0), I only want it to print out 16 once (as given in the main method). But for some odd reason it prints it out 3 times. Can somebody explain why???
public class Foursix {
public static void main(String[] args) {
printEven(1,7);
printEven(21,2);
printEven(16,16);
//main
}
public static void printEven(int i, int n) {
System.out.print("[ ");
//n is greater than i
if (i <= n) {
for (int t = i; t <= n; t++) {
if (t % 2 == 0) {
System.out.print(t + " ");
//if statement inner
}
//for loop
}
//if statement outer
}
//i is greater than n
if (i >= n) {
for (int t = i; t >= n; t--) {
if (t % 2 == 0) {
System.out.print(t + " ");
//if statement inner
}
//for loop
}
//if statement outer
}
//i is the same as n
if (i == n && i % 2 == 0) {
System.out.print(i);
//if statement
}
System.out.print("]");
System.out.println();
//printEven
}
//class
}
You're passing in 16,16, so all three of your if() conditions are TRUE:
if (i <= n) { 16 <= 16 -> TRUE
if (i >= n) { 16 >= 16 -> TRUE
if (i == n && i % 2 == 0) { 16 == 16 && 16 % 2 -> TRUE
Given that you're explicitly testing for all three of greater than/less than/equal, you probably want these:
if (i < n) { ... }
else if (i > n) { ... }
else if (i % 2 == 0) { ... }