Determine if a number is prime. If it is not,
then display its largest factor
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*
Determine if a number is prime. If it is not,
then display its largest factor.
*/
using System;
public class FindPrimes {
public static void Main() {
int num;
int i;
int factor;
bool isprime;
for(num = 2; num < 20; num++) {
isprime = true;
factor = 0;
// see if num is evenly divisible
for(i=2; i <= num/2; i++) {
if((num % i) == 0) {
// num is evenly divisible -- not prime
isprime = false;
factor = i;
}
}
if(isprime)
Console.WriteLine(num + " is prime.");
else
Console.WriteLine("Largest factor of " + num +
" is " + factor);
}
}
}
Related examples in the same category