C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 23rd, 2004, 06:34 PM
Aperculum Aperculum is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Varjakka city
Posts: 3 Aperculum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Aperculum Send a message via MSN to Aperculum
Prime number checker

This is my first c program I have done totally by myself and prime point
of this program is to check if number(which user enter) is prime number or not.
I have done this kind of program before with Visual Basic but it's so slow language
so I decided to make one with c. Problem I have is that it always says that it's not
prime number. Here's the source:
************************CODE*******************
#include <iostream.h>
void main()
{
int a, i, o;
o=0;
cout << "Give number\n";
cin >> a;
for (i = 1; i<a; i++)
{
if ((a % i) == 0)
{
o=1;
break;
}
}
if (o==1)
cout << "Not prime number\n";
else
cout << "prime number\n";
}

********************END OF CODE***************

I can't figure out the reason, please help me.

Reply With Quote
  #2  
Old June 24th, 2004, 03:54 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
This is happening because the loop goes from 1 to a. In the first iteration with i = 1 the if statement will read if (their_number % 1) == 0) which will always be true regardless of the value of their_number.

To avoid this problem run it from 2 instead of 1.

-KM-

Reply With Quote
  #3  
Old June 24th, 2004, 04:12 AM
Aperculum Aperculum is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Varjakka city
Posts: 3 Aperculum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Aperculum Send a message via MSN to Aperculum
Thank you!

Now it works thank you!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Prime number checker


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway