User-defined Methods

Section B

2005

Question 6
Write a program using functions called area() to compute the area of a:-
(i) circle (πr2) where π= 3.14
(ii) square (side*side)
(iii) rectangle (length * breadth )
Display the menu to output the area as per User’s choice.[15]


import java.util.*;
public class Shapes
{
public static void area(double r)
{
double a=3.14*r*r;
System.out.println("Area of the circle = "+a);
}
public static void area(float s)
{
float a=s*s;
System.out.println("Area of the square = "+a);
}
public static void area(double l,double b)
{
double a=l*b;
System.out.println("Area of the rectangle ="+a);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("\t\t\t\tMenu");
System.out.println(“1 Circle”);
System.out.println(“2 Square”);
System.out.println(“3 Rectangle”);
System.out.print(“Enter your choice: ”);
int ch=sc.nextInt();
switch(ch)
{
case 1 : System.out.println("Enter radius of the circle: ");
double r=sc.nextDouble();
area(r);
break;
case 2 : System.out.println("Enter side of the square: ");
float s=sc.nextFloat();
area(s);
break;
case 3 : System.out.println("Enter length and breadth of rectangle: ");
double l=sc.nextDouble();
double b=sc.nextDouble();
area(l,b);
break;
default : System.out.println(“Wrong choice. Try again”);
}
}
}

Variable Description

VariableTypeDescription
r
s
l,b
a
ch
double
float
double
double, float
int
For radius
For side
For length and breadth
For area of circle, square and rectangle
For choice input

2007

Question 8
Using a switch statement, write a menu driven program to convert a given temperature from Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message should be displayed. (HINT: C = 5/9 x (F-32) and F = 1.8 x C + 32) [15]

import java.util.*;
public class Number
{
static Scanner sc=new Scanner(System.in);
public static void toCelsius()
{
System.out.println("Enter temperature in Fahrenheit:");
double F=sc.nextDouble();
double C= 5.0/9*(F-32);
System.out.println("Celsius: "+C);
}
public static void toFahrenheit()
{
System.out.println("Enter temperature in Celsius:");
double C=sc.nextDouble();
double F=1.8*C+32;
System.out.println("Fahrenheit: "+F);
}
public static void main()
{
System.out.println("\t\t\t\tMenu");
System.out.println("1 Convert to Celsius");
System.out.println("2 Convert to Fahrenheit");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: toCelsius();
break;
case 2 : toFahrenheit();
break;
default : System.out.println("Wrong choice. Try again.");
}
}
}

2008

Question 7
Write a menu driven class to accept a number from the user and check whether it is Palindrome or Perfect.
(a) Palindrome number - (a number is a Palindrome when read in reverse order is same as read in the right order)
Example: 11,101,151.
(b) Perfect number - (a number is called Perfect if it is equal to the sum of its factors other than the number itself.)
Example: 6=1+2+3 [15]

import java.util.*;
public class Number
{
static Scanner sc=new Scanner(System.in);
public static void palindrome()
{
System.out.println("Enter a number");
int n=sc.nextInt();
int i=n,d,rev=0;
while(i>0)
{
d = i%10;
rev=rev*10+d;
i=i/10;
}
if(rev==n)
System.out.println("Palindrome number");
else
System.out.println("Not a Palindrome number");
}
public static void perfect()
{
System.out.println("Enter a number");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n/2;i++)
if(n%i==0)
s=s+i;
if(s==n)
System.out.println("Perfect number");
else
System.out.println("Not a Perfect number");
}
public static void main()
{
System.out.println("\t\t\t\tMenu");
System.out.println("1 Palindrome number");
System.out.println("2 Perfect number");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: palindrome();
break;
case 2: perfect();
break;
default:System.out.println("Wrong choice. Try again.");
}
}
}

Question 8
Write a class with name volume using function overloading that computes volume of a cube, sphere and cuboid. [15]
Formula:
volume of a cube: (vc) = s*s*s
volume of a sphere: (vs) = 4/3 π r3 (where π = 3.14 or 22/7)
volume of a cuboid: (vcd) = lbh

import java.util.*;
public class volume
{
public static void findVolume(double s)
{
double vc=s*s*s;
System.out.println("Volume of the cube="+vc);
}
public static void findVolume(float r)
{
float vs=4/3f*3.14f*r*r*r;
System.out.println("Volume of the sphere="+vs);
}
public static void findVolume(double l,double b,double h)
{
double vcd=l*b*h;
System.out.println("Volume of the cuboid="+vcd);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter side of the cube");
double s=sc.nextDouble();
findVolume(s);
System.out.println("Enter radius of the sphere");
float r=sc.nextFloat();
findVolume(r);
System.out.println("Enter length, breadth and height of the cuboid");
double l=sc.nextDouble();
double b=sc.nextDouble();
double h=sc.nextDouble();
findVolume(l,b,h);
}
}

Question 9
Write a program to calculate and print the sum of each of the following series: [15]
(a) Sum(S) = 2 – 4 + 6 – 8 + … - 20
(b) Sum(S) = x/2 + x/5 + x/8+ x/11 + ….+ x/20
(Value of x to be input by the user)

import java.util.*;
public class SumSeries
{
public static void Sum(int S)
{
int sign=1;
for(int i=2;i<=20;i+=2)
{
S=S+i*sign;
sign=-sign;
}
System.out.println("Sum of first series: "+S);
}
public static void Sum(double S)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x");
double x=sc.nextDouble();
for(int i=2;i<=20;i+=3)
S=S+x/i;
System.out.println("Sum of second series: "+S);
}
public static void main()
{
int S1=0;
double S2=0.0;
Sum(S1);
Sum(S2);
}
}

2009

Question 5
Write a program to generate a triangle or an inverted triangle till n terms based upon the user’s choice:[15]
Example 1
Input:
Type 1 for a triangle and
type 2 for an inverted triangle
1
Enter the number of terms : 5
Output :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Example 2
Input:
Type 1 for a triangle and
type 2 for an inverted triangle
2
Enter the number of terms: 6
Output :
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

import java.util.*;
public class Triangle
{
static Scanner sc=new Scanner(System.in);
public static void triangle()
{
System.out.print( “Enter the number of terms: ”);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
System.out.print(i+" ");
System.out.println();
}
}
public static void invertedTriangle()
{
System.out.print( “Enter the number of terms: ”);
int n=sc.nextInt();
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
System.out.print(i+" ");
System.out.println();
}
}
public static void main()
{
System.out.println("Type 1 for a triangle and");
System.out.println("type 2 for an inverted triangle");
int ch=sc.nextInt();
switch(ch)
{
case 1 : triangle();
break;
case 2 : invertedTriangle();
break;
default : System.out.println("Wrong choice");
}
}
}

Question 7
Design a class to overload a function num_calc() as follows:
(a) void num_calc(int num, char ch) with one integer argument and one character argument, computes the square of integer argument if choice ch is ‘s’ otherwise finds its cube.
(b) void num_calc(int a, int b, char ch) with two integer arguments and one character argument. It computes the product of integer arguments if ch is ‘p’ else adds the integers.
(c) void num_calc(String s1,String s2) two string arguments, which prints whether strings are equal or not. [15]

import java.util.*;
public class Number
{
public static void num_calc(int num, char ch)
{
int sq,cu;
if(ch=='s')
{
sq=num*num;
System.out.println("Square: "+sq);
}
else
{
cu= num*num*num;
System.out.println("Cube: "+cu);
}
}
public static void num_calc(int a, int b, char ch)
{
int p,s;
if(ch=='p')
{
p=a*b;
System.out.println("Product: "+p);
}
else
{
s=a+b;
System.out.println("Sum: "+s);
}
}
public static void num_calc(String s1,String s2)
{
if(s1.equals(s2))
System.out.println("Strings are equal");
else
System.out.println("Strings are not equal");
}
public static void main()
{
Scanner sc=new Scanner(System.in);
//For first function
System.out.println("Enter an integer and a character");
int num=sc.nextInt();
char ch1=sc.next().charAt(0);
num_calc(num,ch1);
//For second function
System.out.println("Enter two integers and a character ");
int a=sc.nextInt();
int b=sc.nextInt();
char ch2=sc.next().charAt(0);
num_calc(a,b,ch2);
//For third function
System.out.println("Enter two strings to check equality");
String s1=sc.nextLine();
String s2=sc.nextLine();
num_calc(s1,s2);
}
}

Question 8
Write a menu driven program to accept a number from the user and check whether it is a ‘BUZZ’ number or to accept any two numbers and print the ‘GCD’ of them.
(a) A BUZZ number is number which either ends with 7 or divisible by 7.
(b) GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the larger number by the smaller; the remainder then divides the previous divisor. The process is repeated till the remainder is zero. The divisor then results the GCD. [15]

import java.util.*;
public class Numbers
{
static Scanner sc=new Scanner(System.in);
public static void checkBUZZ()
{
System.out.println("Enter the number");
int n=sc.nextInt();
if(n%10==7||n%7==0)
System.out.println("BUZZ Number");
else
System.out.println("Not a BUZZ Number");
}
public static void findGCD()
{
System.out.println("Enter two numbers");
int a=sc.nextInt();
int b=sc.nextInt();
if(a>b)
{
dividend=a;
divisor=b;
}
else
{
dividend=b;
divisor=a;
}
remainder=dividend%divisor;
while(remainder>0)
{
dividend=divisor;
divisor=remainder;
remainder=dividend%divisor;
}
System.out.println("GCD="+divisor);
}
public static void main()
{
int ch;
System.out.println("1 BUZZ Number check");
System.out.println("2 GCD find");
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1: checkBUZZ();
break;
case 2: findGCD();
break;
default:System.out.println("Wrong choice");
}
}
}

2010

Question 7
Write a menu driven program to accept a number and check and display whether it is prime number or not OR an automorphic number or not. (Use switch case statement.)
(a) Prime number: A number is said to be prime number if it is divisible only by 1 and itself and not by any other number. E.g.: 3,5,7,11,13, etc.
(b) Automorphic number: An automorphic number is the number which is contained in the last digit(s)of its square. E.g.: 25 is an automorphic number as its square is 625 and 25 is present as last two digits. [15]

import java.util.*;
public class MenuDriven
{
static Scanner sc=new Scanner(System.in);
public static void prime()
{
System.out.println("Enter number");
int n=sc.nextInt();
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
public static void automorphic()
{
System.out.println("Enter number");
int n=sc.nextInt();
int i=n, p=1;
while(i>0)
{
i/=10;
p*=10;
}
if(n== n*n%p)
System.out.println("Automorphic");
else
System.out.println("Not Automorphic");
}
public static void main()
{
int ch;
System.out.println("1 Prime");
System.out.println("2 Automorphic");
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1: prime();
break;
case 2: automorphic();
break;
default: System.out.println("Wrong choice");
}
}
}

2011

Question 8
Design a class to overload a function compare() as follows:
(a) void compare(int, int) - to compare two integers and print the greater of the two integers.
(b) void compare(char, char) - to compare the numeric value of two characters and print the character with higher numeric value.
(c) void compare(String, String) - to compare the length of the two strings and print longer of them. [15]

import java.util.*;
public class OverLoad
{
public static void compare(int x, int y)
{
if(x>y)
System.out.println(x+“ is greater”);
else
System.out.println(y+“ is greater”);
}
public static void compare(char x, char y)
{
if(x>y)
System.out.println(x+“ is higher”);
else
System.out.println(y+“ is higher”);
}
public static void compare(String x, String y)
{
if(x.length()>y.length())
System.out.println(x+“ is longer”);
else
System.out.println(y+“ is longer”);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println( “Enter two integers”);
int n1=sc.nextInt();
int n2=sc.nextInt();
compare(n1,n2);
System.out.println( “Enter two characters”);
char c1=sc.next().charAt(0);
char c2=sc.next().charAt(0);
compare(c1,c2);
System.out.println( “Enter two strings”);
String s1=sc.nextLine();
String s2=sc.nextLine();
compare(s1,s2);
}
}

Question 9
Write a menu driven program to perform the following: Use switch case.
(a) To print series 0, 3, 8, 15, 24 … n terms (value of n is to be input by user).
(b) To find the sum of the series: S=1/2 + 3/4 + 5/6 + … + 19/20 [15]

import java.util.*;
public class Series
{
static Scanner sc=new Scanner(System.in);
public static void printSeries()
{
System.out.println( “Enter number of terms”);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
System.out.print((i*i-1)+“, ”);
}
public static void findSum()
{
double s=0.0,a=2.0;
for(int i=1;i<=19;i+=2)
{
s=s+i/a;
a+=2;
}
System.out.println(“Sum=”+s);
}
public static void main()
{
System.out.println(“\t\t\t\tMenu”);
System.out.println(“1 Print series”);
System.out.println(“2 Find sum”);
System.out.println( “Enter your choice”);
int ch=sc.nextInt();
switch(ch)
{
case 1: printSeries();
break;
case 2: findSum();
break;
default: System.out.println( “Wrong choice. Try again”);
}
}
}

2012

Question 7
Design a class to overload a function polygon() as follows:
(i) void polygon(int n, char ch) with one integer argument and one character argument that draws a filled square of side n using the character stored in ch
(ii) void polygon(int x, int y) with two integer arguments that draws a filled rectangle of length x and breadth y using the symbol ‘@’
(iii) void polygon() with no argument that draws a filled triangle shown below. [15]

Example
(i) Input value of n=2,ch= ‘O’
Output:
OO
OO
(ii) Input value of x=2, y=5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***

import java.util.*;
public class Shape
{
public static void polygon(int n,char ch)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
System.out.print(ch);
System.out.println();
}
}
public static void polygon(int x,int y)
{
for(int i=1;i<=x;i++)
{
for(int j=1;j<=y;j++)
System.out.print('@');
System.out.println();
}
}
public static void polygon()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=i;j++)
System.out.print('*');
System.out.println();
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of square and a character");
int n=sc.nextInt();
char ch=sc.next().charAt(0);
polygon(n,ch);
System.out.println("Enter length and breadth of rectangle");
int x=sc.nextInt();
int y=sc.nextInt();
polygon(x,y);
polygon();
}
}

Question 8
Using the switch statement write a menu driven program to:
(i) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5 …
The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
(ii) Find the sum of the digits of an integer that is input.
Sample input: 15390 Sample output: Sum of digits=18
For an incorrect choice an appropriate error message should be displayed. [15]

import java.util.*;
public class Number
{
static Scanner sc=new Scanner(System.in);
public static void fibonacci()
{
int a=0,b=1,c,i;
for(i=1;i<=10;i++)
{
System.out.print(a “, “);
c=a+b;
a=b;
b=c;
}
System.out.println();
}
public static void sumOfDigits()
{
System.out.println("Enter number");
int n=sc.nextInt();
int i=n,s=0,d;
while(i>0)
{
d=i%10;
s+=d;
i/=10;
}
System.out.println("Sum of digits="+s);
}
public static void main()
{
int ch;
System.out.println("1 fibonacci series");
System.out.println("2 sum of digits");
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1: fibonacci();
break;
case 2: sumOfDigits();
break;
default:System.out.println("Wrong choice. Try again");
}
}
}

2013

Question 8
Design a class to overload a function series() as follows:
(i) double Series(double n) with one double argument and returns the sum of the series,
Sum = 1/1 + 1/2 + 1/3 +…….1/n
(ii) double Series(double a, double n) with two double arguments and returns the sum of the series,
Sum = 1/a2 + 4/a5 + 7/a8 +10/a11 +……….to n terms [15]

import java.util.*;
public class Sum
{
public static double series(double n)
{
double Sum=0.0;
for(int i=1;i<=n;i++)
Sum+=1.0/i;
return Sum;
}
public static double series(double a, double n)
{
double Sum=0.0,x=1,y=2;
for(int i=1;i<=n;i++)
{
Sum+=x/Math.pow(a,y);
x+=3; y+=3;
}
return Sum;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for n for the first series");
double n1=sc.nextDouble();
double s1=series(n1);
System.out.println("Sum of series 1"+s1);
System.out.println("Enter value for a and n for the second series");
double a=sc.nextDouble();
double n2=sc.nextDouble();
double s2=series(a,n2);
System.out.println("Sum of series 2"+s2);
}
}

Question 9
Using the switch statement, write a menu driven program:
(i) To check and display whether a number input by the user is a composite number or not. (A number is said to be a composite, if it has one or more than one factor excluding 1 and the number itself.
Example : 4,6,8,9…).
(ii) To find the smallest digit of an integer that is input.
Sample input: 6524
Sample output: smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed. [15]

import java.util.*;
public class Number
{
static Scanner sc=new Scanner(System.in);
//Function to check composite number
public static void composite()
{
System.out.println(“Enter number”);
int n=sc.nextInt();
int c=0;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
c++;
}
if(c>=1)
System.out.println("Composite number");
else
System.out.println("Not a composite number ");
}
//Function to find smallest digit
public static void smallDigit()
{
System.out.println(“Enter number”);
int n=sc.nextInt();
int i=n,d,small=9;
while(i>0)
{
d=i%10;
if(d < small)
small=d;
i/=10;
}
System.out.println(“Smallest digit = ”+small);
}
public static void main()
{
int ch;
System.out.println(“\t\t\t\tMenu”);
System.out.println(“1 Check composite”);
System.out.println(“2 Find smallest digit”);
System.out.println( “Enter your choice”);
ch=sc.nextInt();
switch(ch)
{
case 1: composite();
break;
case 2: smallDigit();
break;
default: System.out.println( “Wrong choice. Try again”);
}
}
}

2014

Question 7
Design a class to overload a function area() as follows:
(i) double area(double a, double b, double c) with three double argument and returns the area of the scalene triangle using the formula:
area= √( s(s-a)(s-b)(s-c) )
where s = (a+b+c)/2
(ii) double area(int a, int b, int height) with three integer arguments and returns the area of a trapezium using the formula:
area = ½ height(a+b)
(iii) double area(double diagonal1, double diagonal2) with two double arguments and returns the area of a rhombus using the formula:
area = ½ (diagonal1 x diagonal2) [15]

import java.util.*;
class shapes
{
public static double area(double a, double b, double c)
{
double s = (a+b+c)/2.0;
double ar = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}
public static double area(int a, int b, int height)
{
double ar = 1/2.0*height*(a+b);
return ar;
}
public static double area(double diagonal1, double diagonal2)
{
double a = 1/2.0*(diagonal1* diagonal2);
return a;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter three sides of the triangle");
double a1= sc.nextDouble();
double b1= sc.nextDouble();
double c1= sc.nextDouble();
double ar1=area(a1,b1,c1);
System.out.println("Area of triangle = "+ar1);
System.out.println("Enter three sides of the trapezium ");
int a2=sc.nextInt();
int b2=sc.nextInt();
int h= sc.nextInt();
double ar2=area(a2,b2,h);
System.out.println("Area of trapezium = "+ar2);
System.out.println("Enter diagonals of the rhombus ");
double d1= sc.nextDouble();
double d2= sc.nextDouble();
double ar3=area(d1,d2);
System.out.println("Area of rhombus = "+ar3);
}
}

Question 9
Using the switch statement, write a menu driven program to calculate the maturity amount of a Bank Deposit: The user is given the following options:
(i) Term Deposit (ii) Recurring Deposit
For option (i) accept principal (P), rate of interest (r) and the time period in years (n). Calculate and output the maturity amount (A) receivable using the formula A = P(1+ r/100)n
For option (ii) accept monthly installment (P), rate of interest (r) and the time period in months (n). Calculate and output the maturity amount (A) receivable using the formula A = P x n + P x n(n+1)/2 x r/100 x 1/12
For an incorrect option, an appropriate error message should be displayed [15]

import java.util.*;
public class Bank
{
public static void termDeposit(double P, double n, double r)
{
double A = P * Math.pow(1+(r/100),n);
System.out.println("Amount of term deposit: "+A);
}
public static void recurringDeposit(double P, double n, double r)
{
double A = P * n + P * (n*(n+1))/2.0 * (r/100.0) * (1/12.0);
System.out.println("Amount of recurring deposit: "+A);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch;
double P,r,n;
System.out.println("\t\t\tMenu");
System.out.println(“1 Term Deposit”);
System.out.println(“2 Recurring Deposit”);
System.out.println( “Enter your choice”);
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter amount, rate and number of years");
P= sc.nextDouble();
r= sc.nextDouble();
n= sc.nextDouble();
termDeposit(P,r,n);
break;
case 2: System.out.println("Enter amount, rate and number of months");
P= sc.nextDouble();
r= sc.nextDouble();
n= sc.nextDouble();
recurringDeposit(P,r,n);
break;
default: System.out.println( “Wrong choice. Try again”);
}
}
}

2015

Question 7
Design a class to overload a function Joystring() as follows:
(i) void Joystring(String s,char ch1, char ch2) with one string argument and two character arguments that replaces the character argument ch1 with the character argument ch2 in the given string s and prints the new string. (Use library functions)
Example: Input value of s=”TECHNALAGY”, ch1=’A’, ch2=’O’
Output: “TECHNOLOGY”
(ii) void Joystring(String s) with one string argument that prints the position of the first space and the last space of the given string s.
Example: Input value of s=”Cloud computing means Internet based computing”
Output:
First index : 5
Last index : 36
(iii) void Joystring(String s1, String s2) with two string arguments that combines the two strings with a space between them and prints the resultant string.
Example: Input value of s1=”COMMON WEALTH”, Input value of s2=”GAMES”
Output: COMMON WEALTH GAMES [15]

import java.util.*;
public class OverLoad
{
public static void Joystring(String s, char ch1, char ch2)
{
s=s.replace(ch1,ch2);
System.out.println(s);
}
public static void Joystring(String s)
{
int a=s.indexOf(' ');
int b=s.lastIndexOf(' ');
System.out.println("First Index: "+a);
System.out.println("Last Index: "+b);
}
public static void Joystring(String s1, String s2)
{
String s3=s1.concat(" ".concat(s2));
System.out.println(s3);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string and two characters for replacement");
String s1= sc.nextLine();
char ch1=sc.next().charAt(0);
char ch2=sc.next().charAt(0);
Joystring(s1,ch1,ch2);
System.out.println("Enter a string to find first and last indices of space");
String s2= sc.nextLine();
Joystring(s2);
System.out.println("Enter two strings to combine");
String s3= sc.nextLine();
String s4= sc.nextLine();
Joystring(s3,s4);
}
}

Question 9
Using a switch statement write a menu driven program to:
(i) To find and display all the factors of a number input by the user (including 1 and excluding the number itself. Sample input: n=15
Sample output: 1, 3, 5
(ii) To find and display the factorial of a number input by the user (the factorial of a non-negative integer n, denoted by n!, is the product of all integer less than or equal to n.
Sample input: n=5
Sample output: 5! = 1x2x3x4x5 = 120
For an incorrect choice an appropriate error message should be displayed. [15]

import java.util.*;
public class MenuDriven
{
static Scanner sc=new Scanner(System.in);
public static void factors()
{
System.out.println("Enter number");
int n= sc.nextInt();
System.out.println("Factors:");
for(int i=1;i<=n/2;i++)
{
if(n%i==0)
System.out.print(i+", ");
}
}
public static void factorial()
{
System.out.println("Enter number");
int n= sc.nextInt();
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("Factorial: "+f);
}
public static void main()
{
int ch;
System.out.println("Menu");
System.out.println("1 Factors");
System.out.println("2 Factorial");
System.out.println("Enter your choice");
ch= sc.nextInt();
switch(ch)
{
case 1: factors();
break;
case 2: factorial();
break;
default: System.out.println("Wrong entry. Try again");
}
}
}

2016

Question 5
Using the switch statement, write a menu driven program for the following:
(i) To print the Floyd’s triangle given below
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(ii) To display the following pattern:
I
I C
I C S
I C S E
For an incorrect option, an appropriate error message should be displayed. [15]

import java.util.*;
public class Triangles
{
public static void FloydTriangle()
{
int k=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
System.out.print(k++ +" ");
System.out.println();
}
}
public static void pattern()
{
String s=“ICSE”;
for(int i=0;i < s.length();i++)
{
for(int j=0;j <= i;j++)
System.out.print(s.charAt(j)+” “);
System.out.println();
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch;
System.out.println(“Menu”);
System.out.println(“1 FloydTriangle “);
System.out.println(“2 CharacterTriangle “);
System.out.println(“Enter your choice”);
ch= sc.nextInt();
switch(ch)
{
case 1: FloydTriangle();
break;
case 2: CharacterTriangle();
break;
default: System.out.println(“Wrong choice. Try again!”);
}
}
}

Question 7
Design a class to overload a function SumSeries() as follows:
(i) void SumSeries(int n, double x) – with one integer argument and one double argument to find and display the sum of the series given below:
s=x/1 – x/2 + x/3- x/4 + x/5 … … … n terms
(ii) void SumSeries() – to find and display the sum of the series given below:
s=1+(1x2) + (1x2x3) + … … … + (1x2x3x4x … …. … x20) [15]

import java.util.*;
public class SumOfSeries
{
public static void SumSeries(int n, double x)
{
int sign=1;
double s=0.0;
for(int i=1;i<=n;i++)
{
s=s+x/i*sign;
sign=-sign;
}
System.out.println("Sum of series 1: "+s);
}
public static void SumSeries()
{
int s=0,p;
for(int i=1;i<=20;i++)
{
p=1;
for(int j=1;j<=i;j++)
p=p*j;
s=s+p;
}
System.out.println("Sum of series 2: "+s);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
double x= sc.nextDouble();
int n= sc.nextInt();
SumSeries(x,n);
SumSeries();
}
}

2017

Question 6
Using switch statement, write a menu driven program for the following:
(i) To find and display the sum of the series given below:
S=x1 - x2 + x3 - x4 + x5 ……. - x20 (where x=2)
(ii) To display the following series:
1 11 111 1111 11111
For an incorrect option an appropriate error message should be displayed [15]

import java.util.*;
public class MenuDriven
{
public static void sumOfSeries()
{
int x=2,s=0,sign=1;
for(int i=1;i<=20;i++)
{
s+=Math.pow(x,i);*sign;
sign=-sign;
}
System.out.println("Sum: "+s);
}
public static void printSeries()
{
for(int i=1;i<=5;i=i*10+1)
System.out.println(i+" ");
}
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch;
System.out.println("\t\t\t\tMenu");
System.out.println("1 Sum of series");
System.out.println("2 Print series");
System.out.println("enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1: sumOfSeries();
break;
case 2: printSeries();
break;
default: System.out.println("Wrong choice. Tryt again!");
}
}
}

Question 8
Design a class to overload a function check() as follows:
(i) void check(String str, char ch) – to find and print the frequency of a character in a string.
Example: Input: str=“success”, ch= ‘s’,
Output Number of s present is = 3
(ii) void check(String s1) – to display only vowels from string s1, after converting it to lower case
Example: Input: s1= “computer”
Output: o u e [15]

public class Strings
{
public static void check(String str, char ch)
{
str=str.toLowerCase();
int c=0;
for(int i=0;i < str.length();i++)
{
if(str.charAt(i)==ch)
c++;
}
System.out.println(“Number of ”+ch+” present is = "+c);
}
public static void check(String s1)
{
s1=s1.toLowerCase();
for(int i=0;i < s1.length();i++)
{
char ch= s1.charAt(i);
if(ch==’a’|| ch==’e’|| ch==’i’|| ch==’o’|| ch==’u’)
System.out.print(ch+" ");
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string and a character");
String s1=sc.next();
char c=sc.next().charAt(0);
check(s1,c);
System.out.println("Enter a string");
String s2=sc.next();
check(s2);
}
}

2018

Question 7
Design a class to overload a function volume() as follows:
(i) double volume(double R) – with radius(R) as an argument, returns the volume of sphere using the formula: V=4/3 x 22/7 x R3
(ii) double volume(double H, double R) – with height(H) and radius(R) as the arguments, returns the volume of a cylinder using the formula: V=22/7 x R2 x H
(iii) double volume(double L, double B, double H) – with length(L), breadth(B) and height(H) as the arguments, returns the volume of a cuboid using the formula: V=L x B x H [15]

import java.util.*;
public class Overload
{
public static double volume(double R)
{
double V=4/3.0 * 22/7.0 * R*R*R;
return V;
}
public static double volume(double H, double R)
{
double V= 22/7.0 * R*R*H;
return V;
}
public static double volume(double L, double B, double H)
{
double V= L*B*H;
return V;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter radius of sphere");
double R=sc.nextDouble();
double V1=volume(R);
System.out.println("Volume of sphere: "+V1);
System.out.println("Enter height and radius of cylinder");
double H=sc.nextDouble();
double R=sc.nextDouble();
double V2=volume(H,R);
System.out.println("Volume of cylider: "+V2);
System.out.println("Enter length, breadth and height of cuboid");
double L=sc.nextDouble();
double B=sc.nextDouble();
double H=sc.nextDouble();
double V3=volume(L,B,H);
System.out.println("Volume of cuboid: "+V3);
}
}

2019

Question 5
Using switch statement write a menu driven program to do the following:
(a) To generate and print Letters from A to Z and their Unicode
LettersUnicode
A 65
B 66
.. ..
Z 90
(b) Display the following pattern using iteration statement:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 [15]

import java.util.*;
public class MenuDriven
{
public static void unicode()
{
System.out.println("Letters\tUnicode");
for(char i='A';i<='Z';i++)
System.out.println(i+"\t"+(int)i);
}
public static void pattern()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j+"\t");
System.out.println();
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch;
System.out.println("\t\t\t\tMenu");
System.out.println("1 Unicode");
System.out.println("2 Pattern");
System.out.println("enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1: sumOfSeries();
break;
case 2: printSeries();
break;
default: System.out.println("Wrong choice. Tryt again!");
}
}
}

Question 7
Design a class to overload a function series() as follows
(a) void series(int x,int n) – To display the sum of the series given below:
x1 + x2 + x3 + …. xn terms
(b) void series(int p) – To display the following series:
0, 7, 26, 63 … p terms
(c) void series() – To display sum of series:
1/2 + 1/3 + 1/4 … 1/10 [15]

import java.util.*;
public class Overload
{
public static void series(int x,int n)
{
double s=0;
for(int i=1;i<=n;i++)
s+=Math.pow(x,i);
System.out.println("Sum of first series: "+s);
}
public static void series(int p)
{
for(int i=1;i<=p;i++)
System.out.print((i*i*i-1)+", ");
}
public static void series()
{
double s=0;
for(int i=2;i<=10;i++)
s+=1.0/i;
System.out.println("Sum of third series: "+s);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value for x and n");
int x=sc.nextInt();
int n=sc.nextInt();
series(x,n);
System.out.println("Enter value for p");
int p=sc.nextInt();
series(p);
series();
}
}

2020

Question 7
Design a class to overload a method Number( ) as follows:
(i) void Number (int num , int d) - To count and display the frequency of a digit in a number.
Example:
num = 2565685
d = 5
Frequency of digit 5 = 3
(ii) void Number (int n1) - To find and display the sum of even digits of a number.
Example:
n1 = 29865
Sum of even digits = 16
Write a main method to create an object and invoke the above methods. [15]

import java.util.*;
public class Overload
{
public void Number(int num,int d)
{
int i=num,ld,c=0;
while(i>0)
{
ld=i%10;
if(ld==d)
c++;
i/=10;
}
System.out.println("Frequency of digit "+d+" = "+c);
}
public void Number(int n1)
{
int i=n1,ld,s=0;
while(i>0)
{
ld=i%10;
if(ld%2==0)
s+=ld;
i/=10;
}
System.out.println(("Sum of even digits = "+s);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
Overload ob=new Overload();
System.out.println("Enter number and digit to be counted");
int num=sc.nextInt();
int d=sc.nextInt();
ob.Number(num,d);
System.out.println("Enter number to find sum of even digits");
int n1=sc.nextInt();
ob.Number(n1);
}
}