hey...i am new to this site and also new using visual c++...and i need help in a project!

i am asked to find prime numbers using for loop ! i cannot figure out how to write the program. please help sumone! i desparately need it within 4 hrs !! please.

but there is another problem...i hav to make a program that can find prime numbers from any number that will be inputed!! like its not from 1 to 500.... from any given number....like 4 to 67...or 5 to 70 !!

to be more accurate...when i execute the program...then type any number(ex. 67)...prime numbers till 67 will come.

i hope i could make it clear!

plz help!!

thank u

>>i desparately need it within 4 hrs !
I see in my crystle ball that you won't make it :) Your quickest solution is to search this site for other similar questions because that has been discussed several times.

well i am extremely sorry for the time barrier!! i can surely ask for more time...from my teacher! but please give a solution. i am still trying but cant figure out

and i cant find the exact thread! it would be of great help if u could direct me to it.

for(i = initValue; i <= finalValue; i++)
{
    for(j = 0; j < i; j++)
    {
        if(i%j == 0)
            prime[i] = true;
    }
}

I wrote that in two seconds and didn't test it so it probably won't work, but that is the basic idea. Also, you may want to start doing your own homework or you'll be screwed come exam time.

prime is a boolean array. And, yes you do have to declare it as well as the rest of the variables. The code I wrote was just an excerpt, not the whole program.
At the end of the program you will have an array telling you whether each number is prime or not.

this is basically what i could come up with...and i guess its wrong...cause its not working!

#include<stdio.h>
main(){
int k,j;
scanf("%d",&k);
for (j=4;j<=k;j++,k--)
{
if(k%j==3);
}
printf("%d,k");
}

Edited 3 Years Ago by happygeek: fixed formatting

In order to write a code that able to find which number is a prime number, you need to know the definition of the prime number and write your code follow the definition. You'll able to find solution.

The way that code is written there will only be one value of k printed. You need to put the print statement after the if statement otherwise you won't get what you want. Also, I don't understand what you are trying to do with the k%j. Try replacing your for loop with my for loop and see what happens. Instead of creating an array you can just put in a printf.

Edited 3 Years Ago by happygeek: fixed formatting

well patrick....this was the basic idea shown by a member here....but i donot understand the meaning of 'res'...please help a little more

int userNum;
int div;


for(i=2;i<userNum;i++)

{

for(div=userNum/2; div>1;div--)

{

res=userNum%div;

if(res!=0)

printf("%d", res);

}

}

well patrick..taking help from that members idea...i came up with this...but still not working!

i would ask a favor..please correct the program if possible for you...cause i have only an hour to submit my first assignment!

#include<stdio.h>
main(){
int k;
int j;
int i;
int a;


for (i=2;i<k;i++)
{
for (j=k/2; j>1;j--)
{
a=k%j;


if(a!=0)
printf("%d", a);


}
}
}

Edited 3 Years Ago by happygeek: fixed formatting

res is a variable to hold the result, so you'll need to declare that as an int above. Other than that, this code looks good to me. You might want to add a line in between the last two brackets such as

if(res == 0)
    printf("%d is not a prime number", userNum)
else
   printf("%d is a prime number", userNum)

what about this program i wrote...is it alright??..i guess u missed out my last post!!....please if theres a mistake...please correct me!!

thnx

Edited 3 Years Ago by happygeek: fixed formatting

Sorry, I did miss your earlier post. The immediate problem I see is that you didn't define k. Try setting that to a number and test it.

This article has been dead for over six months. Start a new discussion instead.