Showing posts with label Logical Programming. Show all posts
Showing posts with label Logical Programming. Show all posts
Tuesday, August 25, 2009
Program to find HCF and LCM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static int hcf(int min, int max)
{
int rem;
if (min < max)
{
int temp = min;
min = max;
max = temp;
}
while (true)
{
rem = max % min;
if (rem == 0)
return min;
else
{
max = min;
min = rem;
}
}
}
static int lcm(int one, int two)
{
int lcm = one * two / hcf(one, two);
return lcm;
}
static void Main(string[] args)
{
int element, value, choice;
int[] array;
do
{
Console.WriteLine("1.HCF\n2.LCM\n3.exit\nchoice:");
choice = Convert.ToInt16(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("No. of elements:");
element = Convert.ToInt32(Console.ReadLine());
array = new int[element];
Console.WriteLine("Elements:\n");
for (int i = 0; i < element; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
value = array[0];
for (int i = 1; i < element; i++)
{
value = hcf(value , array[i]);
}
Console.WriteLine("HCF:" + value);
break;
case 2:
Console.WriteLine("No. of elements:");
element = Convert.ToInt32(Console.ReadLine());
array = new int[element];
Console.WriteLine("Elements:\n");
for (int i = 0; i < element; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
value = array[0];
for (int i = 1; i < element; i++)
{
value = lcm(value, array[i]);
}
Console.WriteLine("LCM:" + value);
break;
case 3:
break;
default:
Console.WriteLine("\nwrong choice");
break;
}
} while (choice != 3) ;
}
}
}
Highest Prime Number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public class PrimeNumber {
/*
* Given a number, finds the highest prime number.
*/
public static int highestPrime(int n) {
int value = 1;
for (int i = 2; i <= n; i++)
{
System.Console.WriteLine("n: " + n + " i: " + i + " mod: " + n % i);
if (i == n) { value = n; break;
}
if (n % i == 0)
{
n--;
}
}
return value;
}
static void Main(string[] args)
{
int value = highestPrime(100);
System.Console.WriteLine("Prime:" + value.ToString());
System.Console.Read();
}
}
}
}
Thursday, August 20, 2009
Find prime Numbers between numbers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num ,count;
bool stat=false;
Console.WriteLine("Enter the number");
num = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= num; i++)
{
count = 0;
for (int j = 1; j <= i; j++)
{
if ((i % j) == 0)
stat = true;
if (stat)
{
count++;
stat = false;
}
}
if(count==2)
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num ,count;
bool stat=false;
Console.WriteLine("Enter the number");
num = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= num; i++)
{
count = 0;
for (int j = 1; j <= i; j++)
{
if ((i % j) == 0)
stat = true;
if (stat)
{
count++;
stat = false;
}
}
if(count==2)
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
Subscribe to:
Posts (Atom)