1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
andrew11 [14]
3 years ago
15

A prime number is a number that is divisible only by itself and 1. Write a program that asks a user for an integer value and the

n displays all prime numbers less than or equal to that number. For example, if the user enters 17, the program should display: Prime numbers less than or equal to 17:
Computers and Technology
1 answer:
Rashid [163]3 years ago
8 0

Answer:

The Solution Code is written in Java.

  1. public class Main {
  2.    public static void main(String[] args) {
  3.        System.out.print("Please enter an integer: ");
  4.        Scanner input = new Scanner(System.in);
  5.        int number = input.nextInt();
  6.        System.out.print("Prime numbers less than or equal to " + number + " : ");
  7.        for(int i=2; i <= number; i++){
  8.            if(checkPrime(i)){
  9.                System.out.print(i + " ");
  10.            }
  11.        }
  12.    }
  13.    public static Boolean checkPrime(int num){
  14.        for(int i=2; i < num; i++)
  15.        {
  16.            if(num % i == 0){
  17.                return false;
  18.            }
  19.        }
  20.        return true;
  21.    }
  22. }

Explanation:

Firstly, we create a function to check if a number is prime (Line 18 - 27).

  • This function will take one input value which is an integer, num.
  • To check if the input num is a prime, we can use modulus operator, %, to confirm if the num is divisible by any number starting from 2 to num - 1 (Line 19 - 24).
  • If the num is divisible by any number expect 1 and itself, it should equal to zero and return false to indicate it is not a prime number.
  • If all the num (except 1 and itself) is not divisible, the function will return true (Line 25).

Next, in our main program part (Line 3 - 16)

  • Prompt the user to input a number (Line 5 - 7)
  • Using a for-loop, we can keep calling the checkPrime() function by passing a current number (starting from 2 to input number) as argument (Line 12). The checkPrime() function will run and return true it the current number is prime, return false if it is not prime.
  • If the checkPrime() function return true, it will print the current number before proceed to the iteration of the for-loop to repeat the same check prime procedure (Line 13)
  • At the end all the prime numbers less than or equal to the input number will be printed out in the terminal
You might be interested in
Free Write Friday 2/26/21
Bad White [126]
Write about art bc thags always so cool
4 0
3 years ago
Write the pseudo-code of an algorithm that returns the sum of all integers in a list of integers. You may use the Pascal-like ps
Arlecino [84]

Answer:

//variable integer_list to hold a list of integers

DECLARE integer_list

ASSIGN values to integer_list

//variable sum to hold the sum of the elements in the list

DECLARE sum

ASSIGN zero to sum

//loop through the integer_list and sum all it's elements together.

for(int i=0; i<integer_list.size(); i++){

sum += interger_list.get(i)

}

//Show the result of the addition from the for loop

DISPLAY "The sum is " + sum

Explanation:

The above code uses some hypothetical programming language syntax. The second and third lines declare an arbitrary integer list and assign a set of values to it respectively.  

The fifth line declares a variable "sum" which will hold the result of summing all the elements in the list. The sixth line initializes "sum" to zero.

The for loop shown iterates through the integer list cumulatively summing its elements.

The result is displayed as written on line 12.

Hope it helps!  

3 0
4 years ago
g You need to create a program that will read the scores from the CSV file, pass the scores as a list to the finalGrade function
Goshia [24]

Answer:

def finalGrade(scoresList):

""" Function that finds the final grade """

hw1_weighted = float(scoresList[0])/10 * 0.05

hw2_weighted = float(scoresList[1])/10 * 0.05

mid_weighted = float(scoresList[2])/100 * 0.40

fin_weighted = float(scoresList[3])/100 * 0.50

final_grade = (hw1_weighted + hw2_weighted + mid_weighted + fin_weighted) * 100

return final_grade

if __name__ == '__main__':

''' Reading and processing scores '''

# Reading names from text file

names = []

# Opening text file

with open("d:\\Python\\names.txt", "r") as fp:

# Reading data

for line in fp:

# Stripping new line

line = line.strip()

# Adding to list

names.append(line)

# Reading scores

finalGrades = []

# Reading scores

with open("d:\\Python\\scores.csv", "r") as fp:

# Reading data

for line in fp:

# Stripping new line

line = line.strip()

# Scores list

scores = []

# Iterating over scores

for score in line.split(','):

# Adding to list

scores.append(score)

# Finding grade

grade = finalGrade(scores)

# Adding to list

finalGrades.append(grade)

# Printing result

for i in range(len(names)):

print("\n%s earned %.1f%%"%(names[i], finalGrades[i]))

Explanation:

This program was implemented with python programming language.

This program will read the scores from the CSV file, pass the scores as a list to the finalGrade function, and display the resulting final grade, along with the student's name.

See attachment for screenshots

6 0
3 years ago
Identify the type of error.<br> print "hello"<br><br> num = 5 / 0
horrorfan [7]

Answer:

Syntax error

Explanation:

This is a type of error that occurs when there is a problem with the code that makes it unable to compile and execute.

For example, making a conditional statement without using the correct parameters will result in a syntax error.

4 0
3 years ago
Read 2 more answers
Soa is a set of​ self-contained web services that communicate with each other to create a working software application.
melisa1 [442]
The answer of the question was true
6 0
4 years ago
Other questions:
  • Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the g
    12·2 answers
  • A step commonly used for Internet vulnerability assessment includes __________, which occurs when the penetration test engine is
    9·1 answer
  • A methods and measurements analyst for Digital Devices needs to develop a time standard for the task of assembling a computer mo
    15·1 answer
  • A vehicle equipped with an electronically shifted transaxle stalls whenever slowing to a stop after being driven over 20 miles (
    15·1 answer
  • 3.6 code practice (edhesive)
    8·1 answer
  • Which of the following are features of the HTTPS protocol?
    12·1 answer
  • State 2 advantages of laptops over desktop computers
    11·2 answers
  • Philip took pictures with his smartphone and save them into his computer unless you delete the photos from the computer they wil
    12·1 answer
  • Problem Statement − Suppose the problem statement at hand is to contain the attrition that happens in companies worldwide. High
    8·1 answer
  • Why is experience in their own factory setting
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!