prime number program - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

prime number program

yak98yak98 Posts: 4Member
hi
i am new to c.i tried to write a program to find all prime numbers between 2 and any given number and print them.i am attaching the source code.but everytime i run it,garbage value appears.can anyone help me with it?i use microsoft compiler.
[code]
/*Program to find all the prime number from zero to a given number and print them*/
#include
#include
main()
{
int num,m,x=0;
int i=2;
int prm=0;
printf("Enter the number
");
scanf("%d",&num);
x=num;
for(x=num;x>=2;x--)
{
while(i<x-1)
{
m=x%i;
if(m==0)
{
break;
}
i++;
}
if(m!=0)
{
prm=x;
printf("%d ",&prm);
}
}
}
[/code]

Comments

  • globalprogglobalprog Posts: 67Member
  • lalalala USAPosts: 4Member

    I have found solution here globalproganswer.com/materials/92

  • sunlotussunlotus Posts: 55Member
    #include <stdio.h>
    #include <math.h>
    
    
    main()
    {
        int sw = 0;
        int num;
        int i;
        int orig_num;
    
        printf("Enter the number");
        scanf("%d",&num);
    
        orig_num=num;
    
        /*A number is prime if and only if it has no divider between
             {2,...,[sqrt(number)] }                         */
    
        for(i=2; i<=floor(sqrt(num)); i=i+1)
            {
                if(num%i == 0)
                    sw=sw+1; 
            }
    
                if(sw == 0 && num!=1)
                    {
                        printf(" %d is prime ", orig_num);
                    }
                    else
                    {
                        printf(" %d is not prime", orig_num);
                    }
    
        return 0;
    }
    
    1. Either you declare void main(){...} "void": main will not return anything;
      Or you declare main() {....; return 0;} which means that if your function(main) does not return a result, it will return 0. It must return something because you didn't specified "void" in this case. By default it must return an integer. It will return the result or 0 - if it fails.

    2. This code is for 1 number. You can put everything from "main" in another function, for example call it "int prime(int n){...}".
      In "main" read more numbers and save them into a vector. Step over each vector's elements with a "for" and call the "prime" function for every number in the vector. Make the "prime" function return 1 if it's prime and return 0 if it's not. Formal parameter "n" is for each number in the vector from "main".
      Let's say in "main" you read more numbers and add them to vector "v".
      Still in "main", do so:

      for(i=0; i<length_of_v - 1; i=i+1)
      {
       if(prime(v[i]) == 1)
             {
              printf("%d is prime", v[i]);
             }
          else
          {
           printf("%d is not prime", v[i]);
          }
      }
    3. math.h is for the functions "sqrt"(square root), "floor"

Sign In or Register to comment.