0% found this document useful (0 votes)
8 views12 pages

Java Programs for Pattern and Series Sums

The document contains Java programs that generate various patterns and calculate sums of specific series. Each section includes a program that demonstrates a different pattern or series calculation, such as generating stars, numbers, or characters in a specific format. Additionally, it provides solutions for summing series involving factorials, cumulative sums, and prime reciprocals.

Uploaded by

salvi.arpita
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Java Programs for Pattern and Series Sums

The document contains Java programs that generate various patterns and calculate sums of specific series. Each section includes a program that demonstrates a different pattern or series calculation, such as generating stars, numbers, or characters in a specific format. Additionally, it provides solutions for summing series involving factorials, cumulative sums, and prime reciprocals.

Uploaded by

salvi.arpita
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1: Write a program to generate the following output.

1.

2.

3.

4.
5.

6.

7.

8.
9.

Answers:
1.
public class KboatPattern
{
public static void main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
[Link]("# ");
else
[Link]("* ");
}
[Link]();
}
}
}
2.
public class KboatPattern
{
public static void main(String args[]) {
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
[Link](a++ + "\t");
}
[Link]();
}
}
}
3.
public class KboatPattern
{
public static void main(String args[]) {
char ch = '*';
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 ; j++) {
if(i == j)
[Link](i + " ");
else
[Link](ch + " ");
}
[Link]();
}
}
}
4.
public class KboatPattern
{
public static void main(String args[]) {
char ch1 = '#', ch2 = '*';
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (i % 2 == 0)
[Link](ch2 + " ");
else
[Link](ch1 + " ");
}
[Link]();
}
}
}
5.
public class KboatPattern
{
public static void main(String args[]) {

for (int i = 1; i <= 5; i++) {


for (int j = 1; j <= i; j++) {
[Link](i);
}

for (int k = 1; k <= 5 - i; k++) {


[Link]('*');
}
[Link]();
}
}
}

6.
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 69; i >= 65; i--) {
for (int j = 65; j <= i; j++) {
[Link]((char)j);
}
[Link]();
}
}
}
7.
public class KboatPattern
{
public static void main(String args[]) {
String str = "JAVA";
int len = [Link]();
for(int i = 0; i < len; i++) {
for(int j = 0; j <= i; j++) {
[Link]([Link](j) + " ");
}
[Link]();
}
}
}
8.
public class KboatPattern
{
public static void main(String args[]) {
for(int i = 5; i >= 1; i--) {
for(int j = 1; j <= i; j++)
[Link](j + " ");
[Link]();
}
for(int i = 2; i <= 5; i++) {
for(int j = 1; j <= i; j++)
[Link](j + " ");
[Link]();
}
}
}

9.
public class KboatPattern
{
public static void main(String args[]) {

for(int i = 1; i <= 5; i++) {


for (int j = 1; j < i; j++) {
[Link](" ");
}
for (int k = i; k <= 5; k++) {
[Link](k + " ");
}
[Link]();
}
for(int i = 4; i >= 1 ; i--) {
for (int j = 1; j < i; j++) {
[Link](" ");
}
for (int k = i; k <= 5; k++) {
[Link](k + " ");
}
[Link]();
}
}
}

Q2:Write programs to find the sum of the given series:

1.​ 1 + (1/2!) + (1/3!) + (1/4!) + .......... + (1/n!)


2.​ 1 + (1+2) + (1+2+3) + .......... + (1+2+3+ ...... + n)
3.​ 1 + (1*2) + (1*2*3) + .......... + (1*2*3* ...... * n)
4.​ 1 + 1 / (1+2) + 1 / (1+2+3) + .......... + 1 / (1+2+3+.....+n)
5.​ (1/2) + (1/3) + (1/5) + (1/7) + (1/11) + .......... + (1/29)

Answers:

1 ans :
import [Link];

public class SeriesSum

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter n: ");

int n = [Link]();

double sum = 0.0;

for (int i = 1; i <= n; i++) {

long f = 1;
for (int j = 1; j <= i; j++) {

f *= j;

sum += (1.0 / f);

[Link]("Sum=" + sum);

}
2 ans:

import [Link];

public class SeriesSum

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter n: ");

int n = [Link]();

long sum = 0;

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= i; j++) {

sum += j;

[Link]("Sum=" + sum);

}
3 ans:

import [Link];

public class SeriesSum

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter n: ");

int n = [Link]();

long sum = 0;

for (int i = 1; i <= n; i++) {

int p = 1;
for (int j = 1; j <= i; j++) {

p *= j;

sum += p;

[Link]("Sum=" + sum);

}
4 ans:

import [Link];

public class SeriesSum

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter n: ");

int n = [Link]();

double sum = 0.0;

for (int i = 1; i <= n; i++) {


long term = 0;

for (int j = 1; j <= i; j++) {

term += j;

sum += (1.0 / term);

[Link]("Sum=" + sum);

}
5 ans:

public class SeriesSum

public static void main(String args[]) {

double sum = 0.0;

for (int i = 2; i < 30; i++) {

boolean isPrime = true;

for (int j = 2; j <= i / 2; j++) {

if (i % j == 0) {
isPrime = false;

break;

if (isPrime)

sum += 1.0 / i;

[Link]("Sum=" + sum);

You might also like