Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

write a program in C This problem will be discussed in Lab6. In Lab6, the TAs will also discuss about the solution of Function Problem

write a program in C

This problem will be discussed in Lab6. In Lab6, the TAs will also discuss about the solution of Function Problem 1 that required you to write several functions. Additionally, the last week's lab problems on loop will be discussed too, if you need more help on them. See the deadline. You should try your best to complete it within the lab time.

First n prime numbers:

Write a program that takes a positive integer n as input and prints all the prime numbers from 1 to n.

Hint: If you are not sure about prime number and how to approach the problem:

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, .}

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 :

2

3

5

7

11

13

17

19

Sample Input/Output 2: Enter your n:2Prime number(s) from 1 to 2 :

2

Sample Input/Output 3: Enter your n:1Prime number(s) from 1 to 1 :

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_2

Step: 3

blur-text-image_3

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

Build It For The Real World A Database Workbook

Authors: Wilson, Susan, Hoferek, Mary J.

1st Edition

0073197599, 9780073197593

More Books

Students explore these related Databases questions