You are not logged in.

#1 2010-02-09 14:10:42

YZMSQ
Member
From: Hangzhou,Zhejiang,China
Registered: 2009-11-18
Posts: 97

I wanna get the largest prime within 0 and 100,but.......

Hi there:
I just wrote a tiny program in C to realize the function to get the largest prime within 0 and 100,and the code is here:

#include <stdio.h>
#include <stdbool.h>
int main(void)
{
    long num;
    long div;
    bool numIsPrime,flag;
    const long big=100;
    for (num=big,flag=true;flag==true&&num>=3;num--)
    {
        for (div=2,numIsPrime=true;(div*div)<=num;div++)
        {
            if(num%div==0)
            {
                numIsPrime=false;
                flag=true;
            }
        }
        if (numIsPrime)
            printf("%ld is the largest prime within %ld.\n",num,big);
            flag=false;
    }
    return 0;
}

But when I executed it,I got nothing........So did I do anything wrong?I need your help,please.:(

Last edited by YZMSQ (2010-02-09 14:13:04)


This is the way the world ends
Not with a bang but a whimper
                            -------T·S·Eliot

Offline

#2 2010-02-09 14:21:16

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: I wanna get the largest prime within 0 and 100,but.......

This sounds like a homework question, which we generally don't allow on the forums (the point of homework is to figure it out yourself and learn, after all).

I'll give you a hint: your problem is here:

        if (numIsPrime)
            printf("%ld is the largest prime within %ld.\n",num,big);
            flag=false;

Good luck.  If you can convince me it's not a homework assignment, I may reopen the thread. smile

Offline

Board footer

Powered by FluxBB