Prime Number Program In Tcl

  1. Prime Number Program In C
  2. Prime Number Programm In C++
  3. Prime Number Program In Tcl
  4. Tcl Contact Number
  5. Tcl Technical Support Phone Number

C Program to Check Whether a Number is Prime or Not Example to check whether an integer (entered by the user) is a prime number or not using for loop and if.else statement. To understand this example, you should have the knowledge of following C programming topics. Prime numbers between 1 to 100 in C Programming Language. Ask Question -4. I want to print prime numbers between 1 to 100, I write my code like the following but when I run it, it starts printing 3,7,11,17.91 Why not the code print 2? Prime Number Check 1-100 (C)-2. Unexpectedly crash in program for finding prime numbers.

Active5 years, 10 months ago

I want to make a program in C language which will take the user input and I would not be able to understand the logic of the loop.

The program code is given below:-

I have used the for loop which will end up the statement of n - 1 in for loop. If I will give the input 11 then it will end up on 11 - 1 = 10 then how it will give up the logic of if(c n) { printf('%d', n);?

Grijesh Chauhan
47.1k14 gold badges104 silver badges167 bronze badges

Prime Number Program In C

Tayyab Gulsher VohraTayyab Gulsher Vohra
3031 gold badge4 silver badges20 bronze badges
Prime Number Program In Tcl

migrated from programmers.stackexchange.comOct 16 '13 at 13:21

This question came from our site for professionals, academics, and students working within the systems development life cycle.

4 Answers

If I will give the input 11 then it will end up on 11 - 1 = 10 then how it will give up the logic of if(c n) { printf('%d', n);?

Now correctly understand your for loop condition:

According to for loop condition c <= n - 1, loop breaks when c value becomes equals to n. So if n is equals to 11 loop condition is true for c = 2 to c = 10, in each iteration c increments by one (using c++ increment) when c becomes 11 (ot say n) then condition c <= n - 1 become false and loop breaks.

In if condition (after for loop) c value compared with n. that is:

for n = 11 it becomes and c = 11 if condition evaluates true and printf() associated with if executes.

It is also important to understand that the for-loop only terminates for c = n when n is a prime number, but if suppose n is a non-prime number then for-loop will break for c value less then n - 1 due to break; statement in nested if block in for-loop.

For example if n = 8 then in very first iteration of for-loop with value c = 2 if condition if(n % c 0) that evaluates as if(8 % 2 0)if( 0 0) = True and break; statement inside if-block moves control outside for-loop(as shown in figure).

Because this time for loop not terminated due to c <= n - 1 condition but braked because of if(n % c 0) so out-side for-loop c value is less than n hence if (c n) evaluates as False.

Grijesh ChauhanGrijesh ChauhanPrime
47.1k14 gold badges104 silver badges167 bronze badges

The for loop loops from c = 2 to c = n - 1 except it hit's the break statement. If it does, it will jump out of the loop. If you never break then your c will actually be nafter the loop.

And here is why. The loop works like this:

  1. initialize the for loop with c = 2
  2. Check for condition (c <= n - 1)if true: execute loop bodyif false: jump past loop
  3. increment c by one
  4. goto 2.

Prime Number Programm In C++

Example: Suppose your n is 3.

  1. we set c to 2, now c 2 and n 3
  2. 2 <= 3 - 1 is true, so the loop body will be executed
  3. we increment c by 1, now c 3 and n 3
  4. we go back to 2. in the description
  5. 3 <= 3 - 1 is false, so we don't execute the loop body now and jump out of the loop
  6. after we left the loop c 3 and n 3 so c n

So if we never hit the break statement c will be equal to n after the loop. We hit the break statement if n is not prime. If we break c will miss at least one increment and thus c < n after the loop. Now c n will evaluate to false and the if statements body of if ( c n ) will not be executed.

Now to the if ( n%c 0 ). n%cmeans n modulo c, so the remainder of the division of n by c. If this remainder is 0 then c is an integer divisor of n. So in the loop you are testing n for any divisor bigger than 1 and smaller than n.

If there is a divisor of n except 1 and itself, n can't be prime. So if you hit any c with 1 < c < n that makes n%cequal to 0 n can't be prime.

Hint: You don't have to test for divisors bigger than √n.

TheMorphTheMorph

Your assumption that c ends up being n - 1 is incorrect. If you step through your program with the debugger you should see that for n 11, c 11 at the end of the loop.

If we don't break early then the last time the loop body executes is indeed when c n - 1 however c is then incremented and the loop invariant test fails so after the loop c n.

Grijesh Chauhan

Prime Number Program In Tcl

47.1k14 gold badges104 silver badges167 bronze badges
jk.jk.
11.9k5 gold badges30 silver badges48 bronze badges

Consider what it means when you say that a number is prime.A number p is prime when for every n>1, n<p, the remainder r = 0 for p/n = r.

So, this loop runs through each value (c) the range [2..p-1], testing for the remainder.

Rather than test that the number is prime by checking a side effect of the loop counter, why not set a flag before the loop, and then test it as the decision for prime at the end? The result is code that is less fragile, error-prone, and clearer.

Want to save about half the work? Once you test n%2, we know that there is no number k such that k*2=n, right? so check the range [2..p/2], and change your loop to,

Can you think of a number smaller than n/2 that works?

An interesting algorithm for finding multiple primes is the Sieve of Erastosthenes.

ChuckCottrillChuckCottrill
3,5362 gold badges16 silver badges28 bronze badges

Not the answer you're looking for? Browse other questions tagged cnumbers or ask your own question.

What is a Prime Number?

A prime number is a number that is only divisible by 1 or itself. For example, 11 is only divisible by 1 or itself. Other Prime numbers 2, 3, 5, 7, 11, 13, 17....

Note: 0 and 1 are not prime numbers. 2 is the only even prime number.

Java Program to check whether number is prime or not

Program Logic:

Tcl Contact Number

  • We need to divide an input number, say 17 from values 2 to 17 and check the remainder. If remainder is 0 number is not prime.
  • No number is divisible by more than half of itself. So we need to loop through just numberToCheck/2 . If input is 17, half is 8.5 and the loop will iterate through values 2 to 8
  • If a numberToCheck is completely divisible by other number, flag isPrime is set to true and the loop is exited.

Output:

Tcl Technical Support Phone Number

Check our program to Find Prime Numbers from 1 to 100