15.
Find and display the sum of the series in the following
(a) S = a + (a/2!) + (a/3!) + (a/4!) + ....... + (a/n!)
import [Link];
public class Series
{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;
for (int i = 1; i <= n; i++) {
double f = 1;
for (int j = 1; j <= i; j++) {
f *= j;
}
sum += a / f;
}
[Link]("Sum=" + sum);
}
}
(b) S = a - (a/2!) + (a/3!) - (a/4!) + ....... to n
import [Link];
public class Series
{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;
for (int i = 1; i <= n; i++) {
double f = 1;
for (int j = 1; j <= i; j++) {
f *= j;
}
if (i % 2 == 0)
sum -= a / f;
else
sum += a / f;
}
[Link]("Sum=" + sum)
(c) S = (a/2!) - (a/3!) + (a/4!) - (a/5!) + ....... + (a/10!)
import [Link];
public class Series
{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
double sum = 0;
for (int i = 2; i <= 10; i++) {
double f = 1;
for (int j = 1; j <= i; j++) {
f *= j;
}
if (i % 2 == 0)
sum += a / f;
else
sum -= a / f;
}
[Link]("Sum=" + sum);
}
(e) S = (2/a) + (3/a 2) + (5/a3) + (7/a4) + ....... to n
import [Link];
public class Series
{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;
int lastPrime = 1;
for (int i = 1; i <= n; i++) {
for (int j = lastPrime + 1; j <= Integer.MAX_VALUE; j++) {
boolean isPrime = true;
for (int k = 2; k <= j / 2; k++) {
if (j % k == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
sum += j / [Link](a, i);
lastPrime = j;
break;
}
}
}
[Link]("Sum=" + sum);
}
Question 16
Write a program to input two numbers and check whether they are twin prime numbers or not.
Hint: Twin prime numbers are the prime numbers whose difference is 2.
For example: (5,7), (11,13), ....... and so on.
import [Link];
public class TwinPrime
{
public void twinPrimeCheck() {
Scanner in = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
boolean isAPrime = true;
for (int i = 2; i <= a / 2; i++) {
if (a % i == 0) {
isAPrime = false;
break;
}
if (isAPrime && [Link](a - b) == 2) {
boolean isBPrime = true;
for (int i = 2; i <= b / 2; i++) {
if (b % i == 0) {
isBPrime = false;
break;
}
if (isBPrime)
[Link](a + " and " + b + " are twin prime");
else
[Link](a + " and " + b + " are not twin prime");
}
else
[Link](a + " and " + b + " are not twin prime");
}
}
Question 17
Write a program to display all the numbers between 100 and 200 which don't contain zeros at any position.
For example: 111, 112, 113, ....... , 199
public class NoZero
{
public void display() {
int count = 0;
for (int i = 100; i <= 200; i++) {
boolean isNoZero = true;
int t = i;
while (t > 0) {
if (t % 10 == 0) {
isNoZero = false;
break;
}
t /= 10;
}
if (isNoZero) {
[Link](i + " ");
count++;
}
//This will print 10 numbers per line
if (count == 10) {
[Link]();
count = 0;
}
}
}
}
Question 18
Write a program to display all prime palindrome numbers between 10 and 1000.
[Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome' number.]
For example: 11, 101, 131, 151,
public class PrimePalindrome
{
public void displayPrimePalindrome() {
int count = 0;
for (int i = 10; i <= 1000; i++) {
int num = i, revNum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == i) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
[Link](i + " ");
count++;
if (count == 10) {
[Link]();
count = 0;
}
}
}
}
}
}
Question 19
Write the programs to display the following patterns:
(a)
1
31
531
7531
97531
public class Pattern
{
public void displayPattern() {
for (int i = 1; i < 10; i = i + 2) {
for (int j = i; j > 0; j = j - 2) {
[Link](j + " ");
}
[Link]();
}
}
}
Output
(b)
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
public class attern
{
public void displayPattern() {
int a = 1;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
[Link](a++ + "\t");
}
[Link]();
}
}
}
(c)
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
public class Pattern
{
public void displayPattern() {
int a = 15;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
[Link](a-- + "\t");
}
[Link]();
}
}
}
(d)
1
10
101
1010
10101
public class Pattern
{
public void displayPattern() {
int a = 1, b = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
[Link](b + " ");
else
[Link](a + " ");
}
[Link]();
}
}
}
Question 20
Using the switch statement, write a menu driven program for the following:
(a) To print the Floyd's triangle:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(b) To display the following pattern:
I
IC
ICS
ICSE
For an incorrect option, an appropriate error message should be displayed.
import [Link];
public class Pattern
{
public void choosePattern() {
Scanner in = new Scanner([Link]);
[Link]("Type 1 for Floyd's triangle");
[Link]("Type 2 for an ICSE pattern");
[Link]("Enter your choice: ");
int ch = [Link]();
switch (ch) {
case 1:
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
[Link](a++ + "\t");
}
[Link]();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < [Link](); i++) {
for (int j = 0; j <= i; j++) {
[Link]([Link](j) + " ");
}
[Link]();
}
break;
default:
[Link]("Incorrect Choice");
}
}
}