[ create a new paste ] login | about

Link: http://codepad.org/s0vM6NqY    [ raw code | output | fork ]

C, pasted on Dec 19:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int isPrime(long n)
{
	if(n < 2)
	{
		return 0;
	}
	else if(n > 2)
	{
		if(n % 2 == 0)
		{
			return 0;
		}
		for(int i = 3; i <= sqrt((float)n); i += 2)
		{
			if(n % i == 0)
			{
				return 0;
			}
		}
	}
	return 1;
}
void selectPrime(FILE* fpIn, FILE* fpOut)
{
	long p;
	while(fscanf(fpIn, "%ld", &p) > 0)
	{
		if(isPrime(p))
		{
			fprintf(fpOut, "%ld\t", p);
		}
	}
}
void main()
{
	int n; 
	FILE* fpIn = fopen("Numbers.txt", "rt");
	FILE* fpOut = fopen("Primes.txt", "wt");

	if(fpIn != NULL)
	{
		if(fpOut != NULL)
		{
			selectPrime(fpIn, fpOut);
			fclose(fpOut);
		}
		fclose(fpIn);
	}
	getch();
}


Output:
1
2
3
4
5
Line 17: error: conio.h: No such file or directory
In function 'isPrime':
Line 16: error: 'for' loop initial declaration used outside C99 mode
In function 'main':
Line 38: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: