Professional Documents
Culture Documents
Index: Topic No. Questions Page No
Index: Topic No. Questions Page No
S=1 + X + X2 + X3 + …………………..+
XN
2. The Volume of solids such as cuboid, 9-18
cylinder and cone can be calculated by the
formula
1
(a) v=l*b*h (b) v=π∗r2 *h and (c) v= 3 * π
∗r2 *h
Write program using Switch case with a
proper menu to display the options. As per
choice entered display the result.
3. Write a program to generate the following 18-26
pattern using the loop statement.
a. b.
A 1
AA 12
AAA 123
AAAA 1234
AAAAA 12345
Question 1
Write a program to Enter value of X and N to calculate the Sum of the
given series: S=1 + X + X2 + X3 + …………………..+ Xn.
Functions used:-
Name of the Function Purpose
input() To take input from user
calculateDisplay() To make calculations and display output
Project Creation:-
Class creation window :-
Output window:-
Program code:-
/**
* This program prints the sum of a series
*
* @author (Anshul Gupta)
* @version (20.7.2021)
*/
import java.io.*;
public class SumOfSeries
{
int n,x;
double sum;
InputStreamReader In=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(In);
public void input() throws IOException
{
System.out.print("Enter a number:");
n=Integer.parseInt(br.readLine());
System.out.print("Enter the limit of the series:");
x=Integer.parseInt(br.readLine());
}
public void calculateDisplay()
{
sum=0.0;
for(int i=0;i<=x;i++)
{
sum+=Math.pow(n,i);
}
System.out.println("The sum of the series is "+sum);
}
}
Question 2
The Volume of solids such as cuboid, cylinder and cone can
be calculated by the formula
1
(a) v=l*b*h (b) v=π∗r2 *h and (c) v= 3 * π∗r2 *h
Write program using Switch case with a proper menu to
display the options. As per choice entered display the
result.
Variable List and other information regarding execution of the
program:-
Variable List
Name of the variable Data type Description
n int Switch case variable
length double Length of the cuboid
breadth double Breadth of the cuboid
height double Height of the solid
radius double Radius of the solid
volume double Volume of the solid
Methods used:-
Output window:-
Case 2:
Input window:-
Output window:-
Case 3:
Input window:-
Output window:-
Default case:-
Input window:-
Output window:-
Program code:-
/**
* This program calculates the volume of different solids depending
upon user's choice
*
* @author (Anshul Gupta)
* @version (20.7.2021)
*/
import java.io.*;
public class Volume
{
int n;
double length,breadth,height,radius,volume;
InputStreamReader In=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(In);
public void input() throws IOException
{
System.out.println("Enter:-");
System.out.println("1.Calculate the volume of cuboid");
System.out.println("2.Calculate the volume of cylinder ");
System.out.println("3.Calculate the volume of cone");
n=Integer.parseInt(br.readLine());
}
public void switchCase() throws IOException
{
switch(n)
{
case 1:
System.out.print("Enter length of the cuboid:");
length=Double.parseDouble(br.readLine());
System.out.print("Enter breadth of the cuboid:");
breadth=Double.parseDouble(br.readLine());
System.out.print("Enter height of the cuboid:");
height=Double.parseDouble(br.readLine());
volume=length*breadth*height;
break;
case 2:
System.out.print("Enter radius of the cylinder:");
radius=Double.parseDouble(br.readLine());
System.out.print("Enter height of the cylinder:");
height=Double.parseDouble(br.readLine());
volume=(22/7.0)*Math.pow(radius,2)*height;
break;
case 3:
System.out.print("Enter radius of the cone:");
radius=Double.parseDouble(br.readLine());
System.out.print("Enter height of the cone:");
height=Double.parseDouble(br.readLine());
volume=(1/3.0)*(22/7.0)*Math.pow(radius,2)*height;
break;
default:
System.out.println("Invalid input");
}
}
public void ouput()
{
System.out.println("The volume of the solid is "+volume);
}
}
Question 3(a)
Write a program to generate the following pattern using the loop
statement.
A
AA
AAA
AAAA
AAAAA
Variable List and other information regarding the execution
of the program:-
Variable List
Name of the variable Data type Description
i int Looping control variable
j int Looping control variable
Methods used:-
Name of the function Purpose
void loopDisplay() To display the pattern by using loop
Program code:-
/**
* This program displays the following pattern on the screen
*A
*AA
*AAA
*AAAA
*AAAAA
*
* @author (Anshul Gupta)
* @version (20.7.21)
*/
public class Pattern1
{
public void loopDisplay()
{
for(int i= 1;i<= 5 ;i++)
{
for(int j= 1;j<= i;j++)
{
System.out.print("A ");
}
System.out.println();
}
}
}
Question 3(b)
Write a program to generate the following pattern using the loop
statement.
1
12
123
1234
12345
Variable list and other information regarding the execution of
the program:-
Variable List
Methods used:-
/**
* Write a program to generate the following pattern using the loop statement.
*1
*12
*123
*1234
*12345
*
* @author (Anshul Gupta)
* @version (20.7.21)
*/
public class Pattern2
{
public void loopDisplay()
{
for(int i= 1 ;i<= 5 ;i++)
{
for(int j= 1 ;j<= i ;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}