Large Numbers show up as negatives.

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. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
	
	
	int x;
	int y = 1;
	int lc = 0;


	//  //  //  //  //  //  //  // //  //  //  //  //  //  //  //  

	while(true)
	{
	cout << "Enter the number to be factored: ";
	cin >> x; cout << endl;

	if (x <= 0)
	{
		cout << "Enter a positive number. \n";
	}

	else if (x >= 16)
	{
		cout << "That number is too large to be computed \n";
	}

	else
	{
		break;
	}

	//  //  //  //  //  //  //  //  //  //  //  //  //  //  //  //  //  

	}
	
	while(true)
	{
		
		lc ++;
			if(lc == x)
			{
			break;
			}
		y = y*(lc + 1);
	}

	cout << y << endl;

	system("PAUSE");
	return 0;
}
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "stdafx.h"

#include <iostream>
#include <cmath>
using namespace 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;
}

This compiled and ran fine for me. But you need to rethink your for loop the output is kinda fun the larger of a number you put in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cmath>
using namespace 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;
}
Last edited on
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.
Topic archived. No new replies allowed.