Beginner(looping statements and conditional statements in java) - java

It shows compilation error:
Compile Message
Solution.java:19: error: unexpected type
if((N%2=0) && (N>=2 && N<=5))
^
required: variable
found: value
1 error
public class Solution {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
if (N % 2 != 0) {
System.out.println("Weird");
}
if ((N % 2 = 0) && (N >= 2 && N <= 5)) {
System.out.println("Not Weird");
}
}
}

N % 2 = 0 is an incorrect assignment because N % 2 isn't a variable. Even if it were a correct expression, it wouldn't return a boolean, so the line would never compile.
You need N % 2 == 0.

Related

why there is no output in java even after using system.out.println function?

class PrimeTernary {
public static void main(String[] args) {
int i, m;
int n = 8;
m = n / 2;
String result;
if (n == 0 || n == 1)
System.out.println("Not prime number");
else
for (i = 3; i <= m; i++) {
result = (n % i == 0) ? "not prime" : "prime";
System.out.println(result);
}
}
}
what is wrong in my code? Can anyone pleased to explain it in brief?
Even if n is 0 or 1 (only then your print statement could ever be reached), then m is necessarily 0. This means, the for loop does not run.
Your if condition is never true since n is always 8.
if (n == 0 || n == 1) is the same as if (8 == 0 || 8 == 1)
So your loop will never execute.

I'm facing an issue with control structures

My problem is with output of my code. When I enter 20, the output must be weird, but I am getting not weird. Same with the value 18.
import java.util.Scanner;
public class conditional {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if(n%2 == 1){
ans = "Weird";
} else {
if(n <= 2 && n >= 5){
ans="Not weird";
} else if(n <= 6 && n >= 20){
ans = "Weird";
} else{
ans = "Not Weird";
}
}
System.out.println(ans);
}
}
the output must be weird,but i am getting not weird
Because, if(n%2 == 1) return false and fall to else block where
if(n <= 2 && n >= 5) is `false`
and
else if(n <= 6 && n >= 20) is also `false`
So, again falls to else block. You probably change
if(n <= 2 && n >= 5)
to
if(n >= 2 && n <= 5)
and
else if(n <= 6 && n >= 20)
to
else if(n >= 6 && n <= 20)
Otherwise, they will never be true and always falls to else.
In your program last else is being executed. Change && (logical AND) to || (logical OR) which will check if number is less than something OR higher than something, instead of checking if something is less or equal 5 AND higher or equal to 20 in the same time as it doesn't have a possibility to evaluate in any case.
I have come up with two solutions and also i see a flaw:
1. if(n%2 == 1) this code can be altered to if(n%2 == 0)
2. The flaw is **(n <= 2 && n >= 5)** . No number can be <2 and >5 at the same time. Try changing that to (n <= 2 || n >= 5) and same goes for (n <= 6 && n >= 20)
import java.util.Scanner;
public class conditional {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if(n%2 == 1){
ans = "Weird";
} else {
if(n <= 6 || n >= 20){
ans="Not weird";
} else if(n <= 2 || n >= 5){
ans = "Weird";
} else{
ans = "Not Weird";
}
}
System.out.println(ans);
}
}

Output Needed in Java

3! = 3*2*1 = 6
WITHOUT USING LOOP
Thank you!
My function below :
public static int factorial(int n)
{
if ((n == 1) || (n == 0))
return 1;
else
return(n * factorial(n-1));
}
You can print the value of the parameter passed in the factorial method like this:
create a function:
public static int factorial(int n) {
System.out.print(n); // here
if (n > 1) System.out.print("*"); // and here
if ((n == 1) || (n == 0))
return 1;
else {
return (n * factorial(n - 1));
}
}
Produces:
3!=3*2*1=6
You can use a global variable (StringBuffer) and append the numbers to it from the factorial method. Use this variable from main method when you print.

java, else if "Error: illegal start of expression"

//the idea of the code if to see is point is in a rectangle. The input is a 6 digit number (abcdef). the top left corner of the rectangle has coordinates (a,b), the right lower (c,d) and the point (e,f)
enter codeimport java.util.Scanner;
public class Rectangle{
Scanner sc = new Scanner(System.in);
public static void run() {
int object;
System.out.println("input:");
object = sc.nextInt();
if(object/100000 >= (object/1000)%10 || (object/10000)%10 <= (object/100)%10){
System.out.print("inside");
}else if (object/100000 <= (object/10)%10 && (object/10)%10 <= (object/1000)%10 && (object/100)%10) <= object%10 && object%10 <= (object/10000)%10){
System.out.print("inside");
}else {
System.out.print("outside");
}
public static void main(String[] args) {
( new Rectangle()).run();
}
}
Your brackets are wrong, please use the formatting of your code tool. I post the fixed code below. Note that the condition must be wrapped between () brackets.
if (condition) { ... }
// In case there are complete calculations within condition
if ((condition) && (condition) && (condition)) { ... }
Moreover it surely says:
non-static method cannot be referenced from a static context
This error should be fixed by removing the static keyword since you use the instance of Scanner that's not static as well.
Scanner sc = new Scanner(System.in);
public void run() {
int object;
System.out.println("input:");
object = sc.nextInt();
if (object / 100000 >= (object / 1000) % 10 || (object / 10000) % 10 <= (object / 100) % 10) {
System.out.print("inside");
} else if (((object / 100000 <= (object / 10) % 10) &&
((object / 10) % 10 <= (object / 1000) % 10) &&
((object / 100) % 10) <= object % 10) &&
(object % 10 <= (object / 10000) % 10))
{
System.out.print("inside");
} else {
System.out.print("outside");
}
}
public static void main(String[] args) {
new Rectangle().run();
}
This is because you have extra ) in third condition in else if statement.
else if (object/100000 <= (object/10)%10 &&
(object/10)%10 <= (object/1000)%10 &&
(object/100)%10 remove it--->) <= object%10 &&
object%10 <= (object/10000)%10){
Also you cannot reference non static 'Scanner sc' in static method, make Scanner sc as static or run() method non static.

Output is not getting displayed

I am writing a simple java program to find the smallest number which is divisible by all the numbers from 1 to 20.
I have written the following code:
package smallmultiple;
public class SmallMultiple {
public static void main(String[] args) {
int sml = 0;
outerloop:
for (int i = 40; i < 100000; i++) {
int j=1;
do {
if(i%j==0)
j++;
} while(j<21);
if(j==20) {
sml=i;
break outerloop;
}
}
System.out.println(sml);
}
}
It is not giving any error but it is not giving any output.
You can try this which is a bit faster than yours solution:-
for(int i = 190; ; i += 190) {
if(i % 3 == 0
&& i % 4 == 0
&& i % 6 == 0
&& i % 7 == 0
&& i % 8 == 0
&& i % 9 == 0
&& i % 11 == 0
&& i % 12 == 0
&& i % 13 == 0
&& i % 14 == 0
&& i % 15 == 0
&& i % 16 == 0
&& i % 17 == 0
&& i % 18 == 0
&& i % 20 == 0) {
System.out.println(i);
break;
}
}
You can also check out this article.
You are incrementing j only if i is perfectly divisible by j. Shouldn't you break or exit the do.. while loop of atleast one number is not divisible? Not doing so is causing infinite loop I believe. It should be something like
if(i%j==0) {
j++;
}
else {
break;
}
its simple. Let me explain your loop. First, i = 40 and j = 1, its ok. Then j++.Next i = 40 and j = 2, its still correctly. Then j++ again. Now i = 40, j = 3 and i%j !=0 => j cannot ++ and j still equal 3. And you see, j = 3 is still satisfy your loop ( j < 21) then it loop and loop forever. This is the reason why you cant get any output. You can use your IDE debugging to find this mistake. Sorry my English not good.
In java, to respect the object oriented best practise, it's not advised to user labels, try this :
public class NumberTool {
public static void main(String[] args) {
System.out.println("Smallest number is : " + getSmallestNumberDividedByOneToTwnety());
}
public static int getSmallestNumberDividedByOneToTwnety() {
for ( int i = 40; i <= 2147483647; i++) {
if (isNumberDivdedByOneToTwenty(i)) {
return i;
}
}
return 0;
}
public static boolean isNumberDivdedByOneToTwenty(int numberToTest) {
for (int i = 1; i <= 20; i++) {
if (numberToTest % i != 0) {
return false;
}
}
return true;
}
}
Output is:
Smallest number is : 232792560

Categories