Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How do I get program to identify prime number?

$
0
0
Here is my code thus far:
#include <math.h>
#include <algorithm>

int main()
{
	int a, b, c, d, e, f, g, h, i, j, s, x;
	int largest, smallest;

	printf("Enter 10 digits\n");
	scanf("%d %d %d %d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j);

	if (a > b && a > c && a > d && a > e && a > f && a > g && a > h && a > i && a > j)
		largest = a;
	else if (b > a && b > c && b > d && b > e && b > f && b > g && b > h && b > i && b > j){
		largest = b;}
	else if (c > a && c > b && c > d && c > e && c > f && c > g && c > h && c > i && c > j){
		largest = c;}
	else if (d > a && d > b && d > c && d > e && d > f && d > g && d > h && d > i && d > j){
		largest = d;}
	else if (e > a && e > b && e > c && e > d && e > f && e > g && e > h && e > i && e > j){
		largest = e;}
	else if (f > a && f > b && f > c && f > d && f > e && f > g && f > h && f > i && f > j){
		largest = f;}
	else if (g > a && g > b && g > c && g > d && g > e && g > f && g > h && g > i && g > j){
		largest = g;}
	else if (h > a && h > b && h > c && h > d && h > e && h > f && h > g && h > i && h > j){
		largest = h;}
	else if (i > a && i > b && i > c && i > d && i > e && i > f && i > g && i > h && i > j){
		largest = i;}
	else if (j > a && j > b && j > c && j > d && j > e && j > f && j > g && j > h && j > i){
		largest = j;}
	
	
	if (a < b && a < c && a < d && a < e && a < f && a < g && a < h && a < i && a < j)
		smallest = a;
	else if (b < a && b < c && b < d && b < e && b < f && b < g && b < h && b < i && b < j){
		smallest = b;}
	else if (c < a && c < b && c < d && c < e && c < f && c < g && c < h && c < i && c < j){
		smallest = c;}
	else if (d < a && d < b && d < c && d < e && d < f && d < g && d < h && d < i && d < j){
		smallest = d;}
	else if (e < a && e < b && e < c && e < d && e < f && e < g && e < h && e < i && e < j){
		smallest = e;}
	else if (f < a && f < b && f < c && f < d && f < e && f < g && f < h && f < i && f < j){
		smallest = f;}
	else if (g < a && g < b && g < c && g < d && g < e && g < f && g < h && g < i && g < j){
		smallest = g;}
	else if (h < a && h < b && h < c && h < d && h < e && h < f && h < g && h < i && h < j){
		smallest = h;}
	else if (i < a && i < b && i < c && i < d && i < e && i < f && i < g && i < h && i < j){
		smallest = i;}
	else if (j < a && j < b && j < c && j < d && j < e && j < f && j < g && j < h && j < i){
		smallest = j;}

	s = sqrt(double(smallest));
	
	printf("Your smallest number is: %d\n", smallest);
		for(int x = 2; x <= s + 1; x++)
			if(!(smallest % x)){
				printf("%d is prime", smallest);}

	printf("Your largest number is: %d", largest);

	getch();
	return(0);
}


This outputs:
Enter 10 digits
blah blah entering 10 digits
Your smallest number is: (gives correct smallest number)
Your largest number is: (gives correct largest number).

So this basically works... I need it to sort through 10 randomly entered number for largest and smallest and it does this, but I also need it to detect if the largest and smallest are prime or not, and if they are prime, a message must say so...

After some digging online I found to do this:
s = sqrt(double(smallest));

printf("Your smallest number is: %d\n", smallest);
for(int x = 2; x <= s + 1; x++)
if(!(smallest % x)){
printf("%d is prime", smallest);}

but... No print screen ever comes up between "your smallest number is:" and "your largest number is:"

Is that even remotely correct to detect a prime number? and if so how do I get the printf to pop up?

Viewing all articles
Browse latest Browse all 51036

Trending Articles