Question

In: Computer Science

Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):...

Convert this code written in Python to Java:

# Definition of a function isprime().

def isprime(num):

    count=2;

    flag=0;

    # Loop to check the divisors of a number.

    while(count<num and flag==0):

        if(num%count!=0):

            # Put flag=0 if the number has no divisor.

            flag=0

        else:

            # Put flag=1 if the number has divisor.

            flag=1

        # Increment the count.

        count=count+1

    # Return flag value.

    return flag

# Intialize list.

list=[]

#Prompt the user to enter Start and end numbers.

st=int(input("Start number:"))

en=int(input("End number:"))

while(st>en):

    print ("End number must be greater than start number. Try again.")

    st=int(input("Start number:"))

    en=int(input("End number:"))

while(st<0 or en<0):

    print ("Start and end must be positive. Try again.")

    st=int(input("Start number:"))

    en=int(input("End number:"))

while(st<=en):

    # Call the function.

    a = isprime(st);

    if(a==0):

        # Append the prime number to a list.

        list.append(st)

    st=st+1

for i in range(0, len(list), 10):

    # Print 10 numbers per line.

    print(*list[i:i + 10])

Solutions

Expert Solution

CODE:

import java.util.*;
import java.lang.*;
public class Main
{
   public static void main(String[] args) {
   Scanner input=new Scanner(System.in);
   int st,en,a,len=0,i;
   //Prompt the user to enter Start and end numbers.
   System.out.print("Start number:");
   st=input.nextInt();
   System.out.print("End number:");
   en=input.nextInt();
   int list[]=new int[en];

while(st>en){
  
System.out.println("End number must be greater than start number. Try again.");
   System.out.print("Start number:");
   st=input.nextInt();
   System.out.print("End number:");
   en=input.nextInt();
}
while(st<0 || en<0){
System.out.println("Start and end must be positive. Try again.");
   System.out.print("Start number:");
   st=input.nextInt();
   System.out.print("End number:");
   en=input.nextInt();
}
while(st<=en){
// Call the function.
a = isprime(st);
if(a==0)
list[len++]=st; // Append the prime number to a list.
st=st+1;
}
for (i=0;i<len;i++){
//Print 10 numbers per line.
System.out.printf("%d ",list[i]);
if(i%10==9)
System.out.println();
}
   }
  
   //Definition of a function isprime().
public static int isprime(int num){
int count=2,flag=0;
// Loop to check the divisors of a number.
while(count<num && flag==0){
if(num%count!=0) // Put flag=0 if the number has no divisor.
flag=0;
else // Put flag=1 if the number has divisor.
flag=1;
count=count+1; // Increment the count.
}
return flag; // Return flag value.
}
}

OUTPUT:


Related Solutions

Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
Convert this code written in Python to Java: students = int(input("How many students are in your...
Convert this code written in Python to Java: students = int(input("How many students are in your class?" )) while students<0:     print("Invalid # of students, try again.")     students = int(input("How many students are in your class?" )) tests = int(input("How many tests in this class? ")) while tests<0:     print("Invalid # of tests, try again.")     tests = int(input("How many tests in this class? ")) print("Here we go!") # Here we are creating a list called class_average to store average of all students....
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like...
Python ONLY ONE STATEMENT ALLOWED PER FUNCTION (ONE RETURN STATEMENT ALLOWED) def plsfollowrule(num): return num like this. 1) create a function popular. The dictionary called database contains each people's hobby and their excitement level. This function searches the dictionary and returns a list of that most popular hobby to least popular hobby. If multiple hobbies have the same number of popularity, follow alphabetical order. #What is popular means? The dictionary contains each people's hobby. The programmer should search the dictionary...
Can you convert this code (Python) to C++ def decrypt(message, key): decryptedText = "" for i...
Can you convert this code (Python) to C++ def decrypt(message, key): decryptedText = "" for i in message: M = ord(i) k = int(key, 16) xor = M ^ k decryptedText += chr(xor) return decryptedText def encrypt(message, key): encryptedText = "" for i in message: M = ord(i) k = int(key, 16) xor = M ^ k encryptedText += chr(xor) return encryptedText # main function userText = input("Enter text: ") userKey = str(input("Enter a key: ")) encryptedMessage = encrypt(userText, userKey)...
In simple python code 1) Write a function to convert the image to grayscale. 2Write a...
In simple python code 1) Write a function to convert the image to grayscale. 2Write a function to convert an image to black and white 3)Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows: newR = (R × 0.393 + G × 0.769 + B × 0.189) newG = (R × 0.349 + G × 0.686 + B × 0.168) newB = (R ×...
Exercise: Write a program named factorial.py that contains the following two functions: def while_factorial(num) def for_factorial(num)...
Exercise: Write a program named factorial.py that contains the following two functions: def while_factorial(num) def for_factorial(num) These should calculate the factorial of a given number represented by the argument num using a while loop and a for loop respectively.
Code with Java and no imports, Method sandwiched returns true if num is in the element...
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num sandwiched([4,5,4,6,7,3], 4) returns true sandwiched([2,1,2], 2) returns true sandwiched([3,3,3], 3) returns false sandwiched([4,5,6,4], 4) returns false sandwiched([1,1,2,3,1,4,1], 1) returns true @param nums Integer ArrayList @param num integer @return true if a single number is between elements equal to num */ public static boolean sandwiched(ArrayList nums, int num) { return false; }//end sandwiched
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an...
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an empty list [] if base < 2 or num <= 0 """ # Write your code here return [] def digit_sum(num, base): """ Return the sum of all digits for a num with given base Your implementation should use num_to_digit_rec() function """ # Write your code here return 0 def digit_str(num, base): """ Given a number and a base, for base between [2, 36]...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating the average sightings from the Total Sightings array float calcAverage(float totalSightings[],int n) {    int i;    float sum=0.0;    for(i=0;i<n;i++)    sum=sum+totalSightings[i];    return sum/n; } int main() {    // n is no. of bird watchers    //flag , flag2 and flag3 are for validating using while loops    int n,i,flag,flag2,flag3;       //ch also helps in validating    char ch;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT