473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using C++ to determine if a no. is prime or not

2
i wanna use C++ to determine whether a no. is prime or not...
but the thing just doesnt work at all...
would anyone kindly check this for me??
Thanks a lot!
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. //To determine whether a given number is prime or not
  7.  
  8. void main()
  9.     int N;int count;
  10.     cin >> N;
  11.     int k;
  12.     for (k=2;k<N;k+1)
  13.     if (N%k==0)
  14.     {cout << k;
  15.      cout << N/k;
  16.                  cout << count;}
  17.                  if (count==0)
  18.                  cout << " <<N<<"is a prime number."
  19. }
  20.  
Actually I dont udnerstand that "count" thing..
my teacher told me to include this,but i dont know if it is a default function or not.
Sep 26 '07 #1
1 1785
RRick
463 Expert 256MB
The count variable looks like the teacher wants to know how many factors are in the number. In any case, count has to be initialized to 0 before the loop.

Also, take the if ( count == 0) logic out of the loop and place it after the loop. That fix should print the correct message for prime numbers.
Sep 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
6
by: UofFprogrammer | last post by:
For a fun challenge, I am trying to create a C++ program that will determine if a number is prime. I am getting to problems when I reach a large values (over 9 or so digits). How can I increase the...
15
by: halo combat22 | last post by:
I am looking for some help on a program that when you write a number it will determine it if is prime or not. Any help is appreciated.
12
by: electric916 | last post by:
I have a homework assignment i Am totally confused on. I started with a basic code to determine if a number is prime or not, but need guidance from here. I will post assignment details then what I...
14
by: Nathan Pinno | last post by:
How do I factor a number? I mean how do I translate x! into proper Python code, so that it will always do the correct math? Thanks in advance, Nathan P.
6
by: jtgarrison | last post by:
Is there a way to tell if Outlook is open before sending a message and open it if it's not? I have some old code that I have been using: 'Determine If Outlook Is Open. If Not, Open. Set...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.