Please check my code

I'm trying to find all the primes numbers in my given range...

Page 1 of 1

8 Replies - 900 Views - Last Post: 04 April 2010 - 11:47 AM Rate Topic: -----

#1 linkpedia  Icon User is offline

  • New D.I.C Head

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

Please check my code

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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Please check my code

Posted 03 April 2010 - 06:41 AM

please post your code inside code tags.
for summing the numbers, better have a variable, int sum = 0, and just add each prime number you find to that sum.
However, as Tapas Bose said, better provide more information about what that program should do.

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

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • 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.

:code:
Was This Post Helpful? 0
  • +
  • -

#5 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 linkpedia  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 linkpedia  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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
Was This Post Helpful? -1
  • +
  • -

#9 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1