Question

Complete the Java code. The code should print “x is in numbers” if the integer x...

Complete the Java code. The code should print “x is in numbers” if the integer x is one of the values stored in numbers. If x is not in numbers, your code should print “x is not in numbers”

public static void main(String[] args){

                        int[] numbers = <some array values>;

                        int x = <some value>;

            }

Homework Answers

Answer #1

The code is done in Java and it is simply understandable and for more clarity it is comments have been added.

CODE:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // array consisting numbers
       int[] numbers = { 12, 11, 13, 5, 6 };
       //finding the length of array
       int n = numbers.length;
       //number to be searched
       int x = 13;
       //variable to keep track of number present in array or not
       int flag=0;
       //traversing the array from index 0 to n-1
       for(int i=0;i<n;i++){
           //checking the condition
           if(numbers[i]==x){
               System.out.print(x + " is in numbers ");
               flag=1;
               break;
           }
       }
       //checking the condition of element not present in array
       if(flag==0){
           System.out.print(x + " is not in numbers ");
       }
   }
}

OUTPUT:

Success #stdin #stdout 0.08s 33884KB

13 is in numbers 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete the Java ocode. The code should print how many times integer x appears in numbers....
Complete the Java ocode. The code should print how many times integer x appears in numbers. For example, if numbers = {2,2,1,3,2,2} and x = 2, your code should print the number 4, because x appears 4 times in the array public static void main(String[] args){                         int[] numbers = <some array values>;                         int x = <some value>;             }
The following code should print X is greater than 0. However, the code has errors. Fix...
The following code should print X is greater than 0. However, the code has errors. Fix the code so that it compiles and runs correctly. (Rewrite the program with all the fixes) public class Test1 { public static void main(String[] args) { int x = 3; if (x > 0 System.out.println("x is greater than 0") else System.out.println(x is less than or equal 0"); } } Must be in java and easy to understand grade 11 course
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...
This is Java programing.Modify this Java code,so that each franchise can assign and print their own...
This is Java programing.Modify this Java code,so that each franchise can assign and print their own burger price.The programing have at least three franchises. (Use abstract) class newBurger { public newBurger() { } public void salmonBurger(){    System.out.println("salmonBurger $5.99");    System.out.println("Kcal: 294"); } public void clamBurger(){    System.out.println("clamBurger $4.99");    System.out.println("Kcal: 200"); } public void oysterBurger(){    System.out.println("oysterBurger $3.50");    System.out.println("Kcal: 125"); } } class franchise1 extends newBurger { String name = "franchise #1"; public franchise1() { } } public...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over...
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. code below import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class ReadingFiles { public static void main(String[] args) throws FileNotFoundException { System.out.println(totalEven); } }
IN JAVA Print "userNum1 is negative." if userNum1 is less than 0. End with newline. Convert...
IN JAVA Print "userNum1 is negative." if userNum1 is less than 0. End with newline. Convert userNum2 to 0 if userNum2 is greater than 10. Otherwise, print "userNum2 is less than or equal to 10.". End with newline. public class UserNums { public static void main (String [] args) { int userNum1; int userNum2; userNum1 = 0; userNum2 = 7; *insert* System.out.println("userNum2 is " + userNum2); } }
I need to come up with the pseudocode for a min and max value finder for...
I need to come up with the pseudocode for a min and max value finder for an array, uses recursion. first I started writing a java program to get a better understanding but it seams not to work properly. also Im having trouble calculating the time efficiency for the code. public class Max_min {     /**      * @param args the command line arguments      */     static int Max;     static int Min;     static int index;     static...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Please use my template import java.util.Scanner; public class...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...