0% found this document useful (0 votes)
11 views

write a program to generate a (1)

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

write a program to generate a (1)

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

write a program to generate a triangle or an inverted triangle till 'n' terms based

upon users choice of triangle to be displayed.

Ans:
import java.util.Scanner;

public class KboatPattern


{
public void choosePattern()
{

Scanner in = new Scanner(System.in);


System.out.println("Type 1 for a triangle");
System.out.println("Type 2 for an inverted triangle");

System.out.print("Enter your choice: ");


int ch = in.nextInt();

System.out.print("Enter the number of terms: ");


int n = in.nextInt();

switch (ch) {
case 1:
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
break;

case 2:
for (int i = n; i > 0; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
break;

default:
System.out.println("Incorrect Choice");
}
}
}
THE OUTPUT IS:
The output of this program will depend on the user’s input. For example, if the
user enters 3 for the number of terms and chooses 1 for a triangle, the output will
be:

1
2 2
3 3 3

If the user chooses 2 for an inverted triangle, the output will be:
3 3 3
2 2
1

If the user enters anything other than 1 or 2 for the choice, the program will
print Incorrect Choice.

THE VDT TABLE IS:


Variable Type Initial Value Purpose
in Scanner new Scanner(System.in) To read input from the user
ch int User input The choice of pattern
n int User input The number of terms in the pattern
i int 1 or n Loop counter for the outer loop
j int 1 Loop counter for the inner loop

Q]Write Java program to find the sum of the given series:

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

Ans:
import java.util.Scanner;

public class KboatSeriesSum


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
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);
}
System.out.println("Sum=" + sum);
}
}
THE OUTPUT IS:
The output of the program depends on the user input for n. The program calculates
the sum of the series 1/1! + 1/2! + 1/3! + ... + 1/n!. For example, if the user
enters n = 4, the output will be:

Enter n: 4
Sum=1.6666666666666667

THE VDT TABLE IS:


Variable Type Initial Value Purpose
in Scanner new Scanner(System.in) To read input from the user
n int User input The number of terms in the series
sum double 0.0 To store the sum of the series
i int 1 Loop counter for the outer loop
f long 1 To store the factorial of i
j int 1 Loop counter for the inner loop

You might also like