Practical 3 and 4
Pactical-3
Find out Prime-Palindrome numbers between 1 to 10,000.
public class PalindromicPrime
{
public static void
main(String[] args)
{
final
int total = 100;
final
int displayPerLine = 10;
int
count = 1;
int
number = 2;
while
(count <= total)
{
if (isPrime(number) &&
isPalindrome(number))
{
System.out.print(number + " ");
if (count % displayPerLine == 0)
System.out.println();
count++;
}
number++;
}
}
public static boolean isPrime(int num)
{
if (num == 2)
return true;
for (int divisor = 2; divisor <=
num/2; divisor++)
{
if (num % divisor ==
0)
return false;
}
return true;
}
public static int
reverse(int num)
{
int test = 0;
while (num != 0)
{
int lastdigit = num % 10;
test = test * 10 + lastdigit;
num = num / 10;
}
return test;
}
public static
boolean isPalindrome(int num)
{
return num == reverse(num);
}
}
Practical-4
Display output of series 1/1!-1/2! +1/3!-1/4!+1/5!...
class series2
{
public static void
main(String args[])
{
int n,i;
n=5;
double
S=0,f=0;
for(i=1;i<n;i++)
{ if(i%2==0)
{
f=fact(i);
S=S-(1/f);
}
{
f=fact(i);
S=S-(1/f);
}
f=fact(i); S=S+(1/f);
} System.out.println("The sum
="+S);
}
public static
double fact(int num)
{
int f=1,j;
for(j=1;j<=num;j++)
{
f=f*j;
}
return(f);
} }
Comments
Post a Comment