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
katrin [286]
4 years ago
9

Let's say we want to check whether a number N is a prime number or not. The idea to solve this problem is to iterate through all

the numbers starting from 2 to N using a loop and for every number check if it divides N. If you find any number that divides, we conclude that N is not prime (you should break from the loop as you don't need check further for this number). If we did not find any number between 2 and N which divides N then it means that N is prime. But one challenge could arise when you need to know the reason why your loop breaks. Is it because we managed to divide or is it that the loop completed?

English
2 answers:
riadik2000 [5.3K]4 years ago
7 0

Answer:

I am writing a Python program.    

N = int(input("Enter a number: "))      # prompts user to enter a number

if N > 1:  #if the input number is greater than 1

  for k in range(2,N):   # loop starts which iterates from 2 to input number

      if (N % k) == 0:   # if input number is divisible by value in k

          print(N,"is not a Prime Number")  #displays input no. is not prime

          break   #terminates the loop

  else:   # if input numbers is not divisible by any value in k

      print(N,"is a Prime Number")     # displays that input number is prime

else:   # if the input number is less than or equal to 1

     print(N,"is not greater than 1 and is not a prime number")  

Explanation:

Lets take a value of N to understand the above program.

Suppose the user enters the value 5.

This value is stored in N variable.

If condition checks if the value entered by the user is greater than 1. This condition evaluates to true because 5>1

So the program flow enters the loop. The loop has a range() function is used for the iteration purpose in the loop. It will iterate generating the numbers in the given range which means starting from 2 and ending at N (5 here).

If condition in the loop checks if the input number N which is 5 is completely divisible by any number in the given range i.e. from 2 until it reaches N. Mod operator % is used here because if the N is divisible by any number in the range then its remainder will be 0.  If  this condition evaluates to true this means that N is not a prime number as the prime number is only divisible by 1 and itself. So if this condition is true then the line 5 is not a Prime Number is displayed in the output.

At every iteration the mod of number 5 and each number in the given range is computed and is checked if the result is 0 e.g 5%2 = 1 , 5%3 = 2, 5%4 = 1 and none of the result is equal to 0 so this means 5 is a prime number.

If the condition evaluates to False this means that the number N is prime as it is not divisible by any number in the given range. So the line 5 is a Prime Number is printed on the screen as output.

The last else part is executed when the first outer if condition evaluates to false which means if N is less than 1 and this line 5 is not greater than 1 and is not a prime number is printed as result.

Bess [88]4 years ago
6 0

Answer:

1) its because we managed to divide the answer so it is not a prime number.

2)

Code:

#include <stdio.h>

int main()

{

   int i, j, n, isPrime; // isPrime is used as flag variable

   /* Input upper limit to print prime */

   printf("Enter your n : ");

   scanf("%d", &n);

   printf("Prime numbers from 1 to %d:\n", n);

   /* Find all Prime numbers between 1 to n */

   for(i=2; i<=n; i++)

   {

       /* Assume that the current number is Prime */

       isPrime = 1;  

       /* Check if the current number i is prime or not */

       for(j=2; j<=i/2; j++)

       {

           /*

            * If i is divisible by any number other than 1 and self

            * then it is not prime number

            */

           if(i%j==0)

           {

               isPrime = 0;

               break;

           }

       }

       /* If the number is prime then print */

       if(isPrime==1)

       {

           printf("%d,\n ", i);

       }

   }

   return 0;

}

You might be interested in
Theme and Form in Early American Poetry
lana [24]
I think the theme is political and the form is of british customs
8 0
4 years ago
To avoid sounding too forward, you should avoid addressing the reader by name in a cover letter.
Pavel [41]
I believe this is true, because addressing the reader's name is sort of informal and not suitable in a cover letter.
4 0
3 years ago
Read 2 more answers
I need help fixing my paragraph can someone help me please where it say in dark is what I need to fix in that area
mixer [17]

Once I had finished my Determine Knowledge, I had figured out a few things about myself. [I don't actually know what "Determine Knowledge" is-- I am assuming it is some sort of test? Unfortunately I cannot therefore give an explanation within the paragraph] One thing that I learned about myself is that I try my best to figure things out. I worked this out because I struggled the most with the Decision-Making Styles. Also, it took me many tries to get this Determine Knowledge right. I believe I am weak in this subject because of the way I tried constantly. But I tried very hard to think things through and find the right answers, which was my strength. The management approach that I chose was active listening. The reason I chose this one is that I think it will be a good fit for me. There are a lot of other reasons why I think this, like how I have a good way of listening to others. This is relevant because when it comes to management, you have to decide how to communicate and listen to people. Therefore, there are different ways of showing you how to indicate that you need something in life.The management approach that I want my boss or leader to use would be to be professional. In order to become a professional, you must gain work experience and earn a bachelor's degree. Those are some tasks that will help you to have goals in your life. Also, there are many different ways in which you can gain work experience. One way is to find a job and stick with that job so that you can build up your work experience within the field you want to work in. Therefore, a way you can earn your bachelor's degree is by completing your associate degree, then going back to school for your bachelor's degree. The reason why I think that being a manager is important is that you can become a leader, communicate and motivate. A way you can become a leader is by having other people work for you throughout. Also, you can communicate with employees to lead them in the right direction. In this way, you can motivate them by showing them how things are done on the job right away.

8 0
3 years ago
6/Identify what make the following thesis statement weak : School lunches are vital to many students who may not get enough food
Kazeer [188]

Answer:

A (or C, but I think A)

Explanation:

I would say A because a good thesis statement gets to the point and is short. For example, "Kids should get allowances." The thesis statement we are given is very long and has reasons in it, which is not what a thesis statement should be.

3 0
2 years ago
The Monkey’s Paw
FrozenT [24]
I think the answer is D
4 0
4 years ago
Read 2 more answers
Other questions:
  • Which word does not describe the overall mood of the
    13·1 answer
  • Lucille Fletcher "The Hitchhiker"
    9·1 answer
  • What is holiday spirit?<br><br><br> (amaphuzu wamahhala)
    7·2 answers
  • These starks examples clarify grammatical ideas; however they fail to..........
    15·2 answers
  • What are the three common used question patterns
    7·1 answer
  • What part of a conflict is described in the conclusion of a narrative?
    10·2 answers
  • But I fancy I hear some one of my audience say, it is just in this circumstance that you and your brother abolitionists fail to
    14·1 answer
  • Read the passage from the article..
    13·1 answer
  • Match the term to the definition.
    13·1 answer
  • 1.He was boring.<br>2.He was stupid and would never succeed in life.<br>3.He was a freak.​
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!