Hello, this is my first post on this forum so I'll tell you a little about myself first.
I am pretty new to programming, as im sure you can tell by the sample I've provided. This is just a hobby for me, and I'm hoping after some time and knowledge I can make software or games as a hobby to make some additional cash.
Anyways back to the topic.
This program returns the factor of a number. So if the user were to enter 5, the output would say 120. (1*2*3*4*5)
My program returns a regular number until i enter around 16 or so. After that, it returns negative numbers. Not sure how that is possible. Please enlighten me. :)
I'm going to ask another unrelated question, but I will keep it in this thread to avoid clutter.
This program won't compile correctly. It tells me that I have to have it in a format of "double = sqrt(double)" instead of what I currently have it as, "double = sqrt(int)"
This wont work from my program because you cant use the % operator on a double. However, the book that I am reading requires this.
perhaps since I am using visual studio 2010 and windows xp it isn't up to date on this function?
The program is to tell the user if their number is prime.
#include "stdafx.h"
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int x;
double y;
cout << "Enter the number to test if its prime: ";
cin >> x; cout << endl;
y = sqrt(x);
for(int n = 2; n < y; n++)
{
if(x%n == 0)
{
cout << "This number is not prime \n";
}
else
{
cout << "This is a prime number. \n";
}
}
system("PAUSE");
return 0;
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int x;
double y;
cout << "Enter the number to test if its prime: ";
cin >> x; cout << endl;
y = sqrt(x);
for (int n = 2; n < y; n++)
{
if (x%n == 0)
{
cout << "This number is not prime \n";
}
else
{
cout << "This is a prime number. \n";
}
}
return 0;
}
I havent even gotten to making preventative code for numbers less than 2. I can't even compile what I have written so far.
Also, I just tried compiling the code that you pasted, it doesnt work for me for the same reasons I posted above. it must be because of my old computer and visual studio 2010.