hi.. i'v just started my c programming and i have a problem to solve after 2 lessons.
i'm so bad at this.. i need to creat a program that askes for a number
0 < n <= 80
and the result is- n lines of *
each line is equal to a prime number like if you enterd 13:
you will see
please can someone cam give me a clue to how to start
i know it has someting to do with loops of for or while.. and that i need to check every number till its squre
please help me
One approach is to start by writing a program that has similiar requirements for input, but which prints out a list of prime numbers.
Not up to it's square! Up to it's square root.
Things like this are usually done either by calling a function, or using a pair of nexted loops.
If you just save the primes you find, then you can draw your lines in another function, which helps to break up the problem into more manageable pieces.
handle 2 here, then
for each odd number higher than 2
get the next odd number
for each number from 3 to square root of the number
assume the number is prime
test it to see if it's able to divide evenly into the number being tested
(mod == 0 is a common test for that)
if any number divides evenly into the number being tested, then
set the variable to not prime, and break out of this loop.
if the test variable is still good here, the number is prime, so save it
and or print your lines, etc.
Sit down and think about the problem. As tater likes to say, "until you understand what the problem is there is no way for you to come up with a solution".
Originally Posted by bvsa
Some items you may want to look over:
Originally Posted by bvsa
1. Lesson 3: Loops
2. Lesson 4: Functions
Do the program in incremental steps:
- Start with simple program that asks user for a number "n" and writes that number back out to the screen.
- Modify above to write all numbers up through "n" to the screen using a loop.
- Create an IsPrime function (this will likely involve more loops) that returns true if a number passed to the function is prime (return false if the passed in number is not prime).
- Modify the program to write out the numbers to the screen like before but now also write whether each number is a prime or not (call your IsPrime function).
- Modify the program to only write the number to the screen if it is a prime.
- Now modify the program to write out correct amount of asterisks for each prime number instead of the number itself (this is another loop).
A big problem beginners have is they see the whole problem written down at once and get overwhelmed... then they give up because it is too hard. If you take things one step at a time, break the one big/hard problem into more (but smaller/easier) problems, then the task becomes much easier. This is Problem Solving 101.