Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the correct code in C? Let's say we want to check whether a number N is a prime number or not. The idea

image text in transcribedWhat is the correct code in C?

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? So, after going out of the loop you need additional condition for the final decision. In this process, you might consider another variable that is initialized to 0 in the beginning. You change it in the loop if your number is not prime and you break from the loop. When you get outside of the loop you check whether the variable changed inside the loop or remained same. It will help you to make the decision. Additionally, a good idea would be create a function is prime() that takes an int and returns 1 or 0 indicating whether the passed number is prime or not. Sample Input/Output 1: Enter your n:20Prime number(s) from 1 to 20 : Sample Input/Output 2: Enter your n:2 Prime number(s) from 1 to 2: Sample Input/Output 3: Enter your n:1Prime number(s) from 1 to 1: Privacy - Term

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

3rd Edition

0760049041, 978-0760049044

More Books

Students also viewed these Databases questions