Page 1 of 1

## 8 Replies - 1853 Views - Last Post: 04 April 2010 - 11:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=165886&amp;s=6504a57c1647e6c443baf2708dc197ec&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: -2
• Posts: 21
• Joined: 31-July 09

Posted 03 April 2010 - 06:31 AM

```#include <stdio.h>

int main ()
{
int n,i,count=0;

clrscr ();

printf ("Enter number: ");
scanf ("%d",&n);

while (n>1)
{
for (i=1; i<=n; i++)
{
if ((n%i)==0)
count++;
}
if (count==2)
printf ("\n%d ",n);

n--;
}
getch ();
return (0);
}
```

I also want to add the the primes numbers and then print the final answer... How to do it??
Is This A Good Question/Topic? 0

## Replies To: Please check my code

### #2 Tapas Bose

• D.I.C Regular

Reputation: 23
• Posts: 472
• Joined: 09-December 09

## Re: Please check my code

Posted 03 April 2010 - 06:34 AM

Do you want to find prime numbers within a range? What are you trying to find with that code?

This post has been edited by Tapas Bose: 03 April 2010 - 06:36 AM

### #3 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: Please check my code

Posted 03 April 2010 - 06:41 AM

for summing the numbers, better have a variable, int sum = 0, and just add each prime number you find to that sum.

This post has been edited by japanir: 03 April 2010 - 06:41 AM

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Please check my code

Posted 03 April 2010 - 06:42 AM

You've been here for more than 6 months...you would think you'd have a grasp on code tags.

### #5 Tapas Bose

• D.I.C Regular

Reputation: 23
• Posts: 472
• Joined: 09-December 09

## Re: Please check my code

Posted 03 April 2010 - 06:53 AM

As you say you want to add primes lies within a range, I am giving you a code whose job is to check whether a given number is prime or not.
```#include <stdio.h>
#include <math.h> // for sqrt() function

int main() {
int number, i, lim_i, flag = 1;

printf("\n  Enter a number : ");
scanf("%d", &number); // take input from user and store in into number

lim_i = sqrt(number); // find the square root of number, this will be the upper limit of i

for (i = 2; i <= lim_i; i++) {
// initialize i by 2, increment i by 1, upper limit of i is the
// square root of number
if (number % i == 0) {
// check if number is divisible by i or not
// if it is divisible then moduler division return 0
// note : number % i return the remainder of the division of number by i
flag = 0; // then set flag into 0
break; // break from the loop
}
}

if (flag) {
// this will check whether flag is 1 or not
// if 1 that means flag never change its value in for loop
printf("\n  %d is a prime.\n", number);
// print it is a prime
}
else {
// otherwise
printf("\n  %d is not a prime.\n", number);
// print it is not a prime
}

return 0;
}

```

If you have any question you can ask here. We are here to help you. Hope now you can find the sum of prime within a range.

This post has been edited by Tapas Bose: 03 April 2010 - 07:03 AM

Reputation: -2
• Posts: 21
• Joined: 31-July 09

## Re: Please check my code

Posted 04 April 2010 - 04:54 AM

The code that I have posted isn't giving any output... Means when I run the program and enter the number, the output is none...

Basically I want to find all the prime numbers of 1000 numbers like 2,3,5,7,11 & so on till 1000, so I have made the logic but its not working, so please correct my code if I have done any mistake in it... I need urgent help please!!

Finally after finding all the prime numbers I want to add all the prime numbers & print the final result of the sum.... Please provide me help in this section...

Thanks

### #7 japanir

• jaVanir

Reputation: 1014
• Posts: 3,025
• Joined: 20-August 09

## Re: Please check my code

Posted 04 April 2010 - 05:12 AM

there are a lot of snippets demonstrating how to determine if a number is prime.
in the previous post, Tapas Bose posted a code that does exactly that. take a look.
as for the sum, have a variable called, int sum = 0;
then for every prime number you find, add it to the sum.

Reputation: -2
• Posts: 21
• Joined: 31-July 09

## Re: Please check my code

Posted 04 April 2010 - 11:06 AM

Well please tell me mistake in my code so it will be better

### #9 sarmanu

• D.I.C Lover

Reputation: 967
• Posts: 2,362
• Joined: 04-December 09

## Re: Please check my code

Posted 04 April 2010 - 11:47 AM

I think that you have been given everything you need to solve your problem. Sorry, but there isn't too much to tell anymore.

This post has been edited by sarmanu: 04 April 2010 - 11:48 AM