Que 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 76

INDEX

SERIAL PROGRAMS PAGE


NO. NO.
A special two-digit number is such that when the sum of its
1. digits is added to the product of its digits, the result is equal to
the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Total of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of
its digits to the product of its digits. If the value is equal to the
number input, display the message "Special 2 - digit number"
otherwise, display the message "Not a special two-digit
number".

An air-conditioned bus charges fare from the passengers


based on the distance travelled as per the tariff given below:
2. Distance Travelled Fare
Up to 10 km Fixed charge ₹80
11 km to 20 km ₹6/km
21 km to 30 km ₹5/km
31 km and above ₹4/km
Design a program to input distance travelled by the
passenger. Calculate and display the fare to be paid.

A bank announces new rates for Term Deposit Schemes for


their customers and Senior Citizens as given below:
3. Term Rate of Interest (General) Rate of Interest (Senior
Citizen)
Up to 1 year 7.5% 8.0%
Up to 2 years 8.5% 9.0%
Up to 3 years 9.5% 10.0%
More than 3 years 10.0% 11.0%
The 'senior citizen' rates are applicable to the customers
whose age is 60 years or more. Write a program to accept
the sum (p) in term deposit scheme, age of the customer and
the term. The program displays the information in the
following format:

Amount Deposited Term Age Interest earned


Amount Paid
xxx xxx xxx xxx xxx
4. A courier company charges differently for 'Ordinary' booking
and 'Express' booking based on the weight of the parcel as
per the tariff given below:
Weight of parcel Ordinary booking Express booking
Up to 100 gm ₹80 ₹100
101 to 500 gm ₹150 ₹200
501 gm to 1000 gm ₹210 ₹250
More than 1000 gm ₹250 ₹300
Write a program to input weight of a parcel and type of
booking (`O' for ordinary and 'E' for express). Calculate and
print the charges accordingly.
Write a menu driven program to calculate:
1. Area of a circle = p*r2, where p = (22/7)
5. 2. Area of a square = side*side
3. Area of a rectangle = length*breadth
Enter 'c' to calculate area of circle, 's' to calculate area of
square and 'r' to calculate area of rectangle.
Write a program using switch case to find the volume of a
cube, a sphere and a cuboid.
6. For an incorrect choice, an appropriate error message should
be displayed.
1. Volume of a cube = s * s *s
2. Volume of a sphere = (4/3) * π * r * r * r (π = (22/7))
3. Volume of a cuboid = l*b*h
Write the program in Java to display the first ten terms of the
7. following series:
0, 1, 2, 3, 6…..

Write the program in Java to display the first ten terms of the
8. following series:
1, 11, 111, 1111,
Write the program in Java to display the first ten terms of the
9. following series:
1, -3, 5, -7, 9,
Write the program in Java to find the sum of the following
10. series:
S = 1 + 1 + 2 + 3 + 5 + ....... to n terms
Write the program in Java to find the sum of the following
11. series:
S = 2 - 4 + 6 - 8 + ....... to n
Write the program in Java to find the sum of the following
12. series:
S = (a+1) + (a+2) + (a+3) + ....... + (a+n)
Write the program in Java to find the sum of the following
13. series:
S = (1/a) + (2/a2) + (3/a3) + ....... to n
Write the program in Java to find the sum of the following
14. series:
S = a - a3 + a5 - a7 + ....... to n
Write the program in Java to find the sum of the following
series:
15. S = (a/2) + (a/5) + (a/8) + (a/11) + ....... + (a/20)
Write a program to input a number. Display the product of the
16. successors of even digits of the number entered by user.
Input: 2745
Output: 15
[Hint: The even digits are: 2 and 4
The product of successor of even digits is: 3*5= 15]

Write a program to input a number and check and print


whether it is a Pronic number or not. [Pronic number is the
17. number which is the product of two consecutive integers.]
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
A prime number is said to be 'Twisted Prime', if the new
18. number obtained after reversing the digits is also a prime
number. Write a program to accept a number and check
whether the number is 'Twisted Prime' or not.
Sample Input: 167
Sample Output: 761
167 is a 'Twisted Prime'.

Write a program to input a number. Check and display


19. whether it is a Niven number or not. (A number is said to be
Niven which is divisible by the sum of its digits).
Example: Sample Input 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.

Write a program to accept a number and check whether it is a


20. 'Spy Number' or not. (A number is spy if the sum of its digits
equals the product of its digits.)
Example: Sample Input: 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1*1*2*4 = 8

Using a switch statement, write a menu driven program to:


21. (a) 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.
(b) Find the sum of the digits of an integer that is input by the
user.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should
be displayed.
Write program to find the sum of the following series:
22. S = 1 + (3/2!) + (5/3!) + (7/4!) + ....... to n
[HINT- use nested loops]
Write program to find the sum of the following series:
23. S = a - (a/2!) + (a/3!) - (a/4!) + ....... to n
Write program to find the sum of the following series:
24. S = (2/a) + (3/a2) + (5/a3) + (7/a4) + ....... to n
Write a program to input two numbers and check whether
25. 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.

Write a program to input a number and check whether it is a


26. prime number or not. If it is not a prime number then display
the next number that is prime.
Sample Input: 14
Sample Output: 17

Write the programs to display the following patterns:


27. (a)
1
31
531
7531
97531

(b)
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
Write the programs to display the following patterns:
28.
(a)
1
10
101
1010
10101

(b)
*
*#
*#*
*#*#
*#*#*
Write a program to generate a triangle or an inverted triangle
till n terms based upon the user's choice.
29.
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output:

1
22
333
4444
55555

Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter the number of terms 6
Sample Output:
666666
55555
4444
333
22
1

Write a menu driven program to display the following


patterns:

(a)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

(b)
11111
22222
33333
44444
QUE 1. A special two-digit number is such that when the sum of its
digits is added to the product of its digits, the result is equal to the
original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Total of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its
digits to the product of its digits. If the value is equal to the number
input, display the message "Special 2 - digit number" otherwise,
display the message "Not a special two-digit number".

import java.util.*;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
int Num=0, count=0, digit=0, dS = 0, dP = 1;
System.out.print("Enter a 2 digit number: ");
Num = sc.nextInt();
int numb = Num;
while (numb != 0) {
digit = numb % 10;
numb /= 10;
dS = digit+digit;
dP = digit*digit;
count++;
}
if (count != 2)
System.out.println("Invalid input, please enter a 2-digit number");
else if ((dS + dP) == Num)
System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit number");

}
}
OUTPUT:
Enter a 2 digit number: 64
Not a special 2-digit number
Enter a 2 digit number: 59
Special 2-digit number

Variable Name Data Type Description


Num int To store entered two
digit number
count int To count
digit int Two digit number
numb int Num=numb
dS int To store digit sum
dP int To store digit product
QUE 2. An air-conditioned bus charges fare from the passengers
based on the distance travelled as per the tariff given below:
Distance Travelled Fare
Up to 10 km Fixed charge ₹80
11 km to 20 km ₹6/km
21 km to 30 km ₹5/km
31 km and above ₹4/km
Design a program to input distance travelled by the passenger.
Calculate and display the fare to be paid.

import java.util.*;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
Int dist=0, fare = 0;
System.out.print("Enter distance travelled: ");
dist = sc.nextInt();

if (dist <= 0)
fare = 0;
else if (dist <= 10)
fare = 80;
else if (dist <= 20)
fare = 80 + (dist - 10) * 6;
else if (dist <= 30)
fare = 80 + 60 + (dist - 20) * 5;
else if (dist > 30)
fare = 80 + 60 + 50 + (dist - 30) * 4;

System.out.println("Fare = " + fare);


}
}

OUTPUT:
Enter distance travelled: 12
Fare = 92

Variable Name Data Type Description


dist int To store entered
distance travelled
fare int To store total fare
QUE 3. A bank announces new rates for Term Deposit Schemes for
their customers and Senior Citizens as given below:
Term Rate of Interest (General) Rate of Interest (Senior Citizen)
Up to 1 year 7.5% 8.0%
Up to 2 years 8.5% 9.0%
Up to 3 years 9.5% 10.0%
More than 3 years 10.0% 11.0%
The 'senior citizen' rates are applicable to the customers whose
age is 60 years or more. Write a program to accept the sum (p) in
term deposit scheme, age of the customer and the term. The
program displays the information in the following format:

Amount Deposited Term Age Interest earned Amount Paid


xxx xxx xxx xxx xxx

import java.util.*;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
double sum=0.0d, term=0.0d, si=0.0d, amt=0.0d;
int age=0;
System.out.print("Enter sum: ");
sum = sc.nextDouble();

System.out.print("Enter age: ");


age = sc.nextInt();
System.out.print("Enter term: ");
term = sc.nextDouble();

if (term <= 1 && age < 60)


si = (sum * 7.5 * term) / 100;
else if (term <= 1 && age >= 60)
si = (sum * 8.0 * term) / 100;
else if (term <= 2 && age < 60)
si = (sum * 8.5 * term) / 100;
else if (term <= 2 && age >= 60)
si = (sum * 9.0 * term) / 100;
else if (term <= 3 && age < 60)
si = (sum * 9.5 * term) / 100;
else if (term <= 3 && age >= 60)
si = (sum * 10.0 * term) / 100;
else if (term > 3 && age < 60)
si = (sum * 10.0 * term) / 100;
else if (term > 3 && age >= 60)
si = (sum * 11.0 * term) / 100;

amt = sum + si;

System.out.println("Amount Deposited: " + sum);


System.out.println("Term: " + term);
System.out.println("Age: " + age);
System.out.println("Interest Earned: " + si);
System.out.println("Amount Paid: " + amt);
}
}

OUTPUT:
Enter sum: 400
Enter age: 30
Enter term: 2
Amount Deposited: 400.0
Term: 2.0
Age:30
Interest Earned:68.0
Amount Paid:468.0

Variable Name Data Type Description


sum double To store the value of
entered sum
term double To store entered term
si double To store the calculated
value of simple interest
amt double To store the total
amount
age int To store entered age
QUE 4. A courier company charges differently for 'Ordinary'
booking and 'Express' booking based on the weight of the parcel
as per the tariff given below:
Weight of parcel Ordinary booking Express booking
Up to 100 gm ₹80 ₹100
101 to 500 gm ₹150 ₹200
501 gm to 1000 gm ₹210 ₹250
More than 1000 gm ₹250 ₹300
Write a program to input weight of a parcel and type of booking (`O'
for ordinary and 'E' for express). Calculate and print the charges
accordingly.

import java.util.*;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
Double wt=0.0d, charge=0.0d;
System.out.print("Enter O for ordinary");
System.out.print("Enter E for express");
char Type = sc.nextChar();
System.out.print("Enter weight of parcel: ");
wt = sc.nextDouble();

if (wt <= 0)
charge = 0;
else if (wt <= 100 && type == 'O')
charge = 80;
else if (wt <= 100 && type == 'E')
charge = 100;
else if (wt <= 500 && type == 'O')
charge = 150;
else if (wt <= 500 && type == 'E')
charge = 200;
else if (wt <= 1000 && type == 'O')
charge = 210;
else if (wt <= 1000 && type == 'E')
charge = 250;
else if (wt > 1000 && type == 'O')
charge = 250;
else if (wt > 1000 && type == 'E')
charge = 300;

System.out.println("Parcel charges = " + charge);


}
}
OUTPUT:
Enter weight of parcel: 80
Enter type of booking: E
Parcel charges = 100.0

Variable Data Type Description


wt double To store the entered
value of weight
type char For the choice
charge double For the given vale of
charge
QUE 5. Write a menu driven program to calculate:
1. Area of a circle = p*r2, where p = (22/7)
2. Area of a square = side*side
3. Area of a rectangle = length*breadth
Enter 'c' to calculate area of circle, 's' to calculate area of square
and 'r' to calculate area of rectangle.

import java.util.*;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter c to calculate area of circle");


System.out.println("Enter s to calculate area of square");
System.out.println("Enter r to calculate area of rectangle");
System.out.print("Enter your choice: ");
char choice = sc.next().charAt(0);

switch(choice) {
case 'c':
System.out.print("Enter radius of circle: ");
double r = sc.nextDouble();
double ca = (22 / 7.0) * r * r;
System.out.println("Area of circle = " + ca);
break;
case 's':
System.out.print("Enter side of square: ");
double side = sc.nextDouble();
double sa = side * side;
System.out.println("Area of square = " + sa);
break;

case 'r':
System.out.print("Enter length of rectangle: ");
double l = sc.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b = sc.nextDouble();
double ra = l * b;
System.out.println("Area of rectangle = " + ra);
break;

default:
System.out.println("Wrong choice!");
}
}
}
OUTPUT:
Enter c to calculate area of circle
Enter s to calculate area of square
Enter r to calculate area of rectangle
Enter your choice: c
Enter radius of circle: 42
Area of circle = 5544.0

Variable Data Type Description


choice char For the choice
r double To store the entered
radius
ca double To store the area of
circle
side double To store the entered
side
sa double To store the area of
square
l double To store the entered
length
b double To store the entered
breadth
ra double To store the area of
rectangle
QUE 6. Write a program using switch case to find the volume of a
cube, a sphere and a cuboid.
For an incorrect choice, an appropriate error message should be
displayed.
1. Volume of a cube = s * s *s
2. Volume of a sphere = (4/3) * π * r * r * r (π = (22/7))
3. Volume of a cuboid = l*b*h

import java.util.*;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);

System.out.println("1. Volume of Cube");


System.out.println("2. Volume of Sphere");
System.out.println("3. Volume of Cuboid");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();

switch(choice)
{
case 1:
System.out.print("Enter side of cube: ");
double cs = sc.nextDouble();
double cv = Math.pow(cs, 3);
System.out.println("Volume of cube = " + cv);
break;

case 2:
System.out.print("Enter radius of sphere: ");
double r = sc.nextDouble();
double sa = (4 / 3.0) * (22 / 7.0) * Math.pow(r, 3);
System.out.println("Volume of sphere = " + sa);
break;

case 3:
System.out.print("Enter length of cuboid: ");
double l = sc.nextDouble();
System.out.print("Enter breadth of cuboid: ");
double b = sc.nextDouble();
System.out.print("Enter height of cuboid: ");
double h = sc.nextDouble();
double vol = l * b * h;
System.out.println("Volume of cuboid = " + vol);
break;
default:
System.out.println("Wrong choice! Please select from 1 or 2 or 3.");
}
}
}
OUTPUT:
1. Volume of Cube
2. Volume of Sphere
3. Volume of Cuboid
Enter your choice: 2
Enter radius of sphere: 49
Volume of sphere = 493005.33333333326

Variable Data Type Description


choice char For the choice
cs double To store the entered
the side of cube
cv double To store the volume of
cube
r double To store the entered
radius of sphere
sa double To store the volume of
sphere
l double To store the entered
length
b double To store the entered
breadth
h double To store the entered
height
vol double To store the volume of
cuboid
QUE 7. Write the program in Java to display the first ten terms of
the following series:
0, 1, 2, 3, 6….. .

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);

int a = 0, b = 1, c = 2;

System.out.print(a + ", " + b + ", " + c);

for (int i = 0; i < 7; i++)


{
int n = a + b + c;
System.out.print(", " + n);
a = b;
b = c;
c = n;
}
}
}

OUTPUT:
0, 1, 2, 3, 6, 11, 20, 37, 68, 1250, 1, 2, 3, 6, 11, 20, 37, 68, 125

Variable Data Type Description


a int Variable
b int Variable
c int Variable
i int Loop control variable
n int Number of iteration
QUE 8. Write the program in Java to display the first ten terms of
the following series:
1, 11, 111, 1111, .

import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);

int n = 1;
for (int i = 1; i <= 10; i++)
{
System.out.print(term + ", ");
n = n * 10 + 1;
}
}
}

OUTPUT:

1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111,


1111111111,

Variable Data Type Description


n int Multiply by 10 and
increases the value by 1
i int Loop control variable
QUE 9. Write the program in Java to display the first ten terms of
the following series:
1, -3, 5, -7, 9,

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);

int n = 1;
System.out.print(term);

for (int i = 2; i <= 10; i++)


{
n= n+ 2;
if (i % 2 == 0)
System.out.print(", " + (-term));
else
System.out.print(", " + term);
}
}
}
OUTPUT:

1, -3, 5, -7, 9, -11, 13, -15, 17, -19

Variable Data Type Description


n int Increases the value
by 2
i int Loop control variable
QUE 10. Write the program in Java to find the sum of the following
series:
S = 1 + 1 + 2 + 3 + 5 + ....... to n terms.

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter n: ");
int x = sc.nextInt();

int a = 1, b = 1;
int sum = a + b;

for (int i = 3; i <= n; i++)


{
int x = a + b;
sum = sum +n;
a = b;
b = x;
}
System.out.println("Sum=" + sum);
}
}
OUTPUT:

Enter n: 12
Sum=376

Variable Data Type Description


n int Number of iteration
a int Variable
b int Variable
sum int To store sum of series
x int To store the sum of a
and b
i int Loop control variable
QUE 11. Write the program in Java to find the sum of the following
series:
S = 2 - 4 + 6 - 8 + ....... to n .

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
int sum = 0;
for (int i = 1, j = 2; i <= n; i++, j = j + 2)
{
if (i % 2 == 0)
{
sum = sum - j;
}
else
{
sum = sum + j;
}
}
System.out.println("Sum=" + sum);
}
}
OUTPUT:

Enter n: 44
Sum=-44

Variable Data Type Description


sum int To store Addition or
subtraction of series
according to the
condition
i int Loop control variable
j int Inner loop control
variable
n int Number of iteration
QUE 12. Write the program in Java to find the sum of the following
series:
S = (a+1) + (a+2) + (a+3) + ....... + (a+n).

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter n: ");
int n = sc.nextInt();
double sum = 0;
for (int i = 1; i <= n; i++)
{
double f = 1;
for (int j = 1; j <= i; j++)
{
f = f * j;
}
sum = sum + a / f;
}
System.out.println("Sum=" + sum);
}
}
OUTPUT:

Enter a: 56
Enter n: 4
Sum=95.66666666666666

Variable Data Type Description


a int To store entered
number
n int Number of iteration
sum double To store sum of series
i int Loop control variable
j int Inner loop control
variable
f double To store the
multiplication of f and j
QUE 13. Write the program in Java to find the sum of the following
series:
S = (1/a) + (2/a2) + (3/a3) + ....... to n.
import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
double sum = 0;
for (int i = 2; i <= 10; i++)
{
double f = 1;
for (int j = 1; j <= i; j++)
{
f = f* j;
}
if (i % 2 == 0)
sum = sum + a/f;
else
sum = sum – a/f;
}
System.out.println("Sum=" + sum);
}
OUTPUT:

Enter a: 11
Sum=4.046674107142856

Variable Data Type Description


a int To store entered
number
sum double To store sum of series
i int Loop control variable
j int Inner loop control
variable
f double To store the
multiplication of f and j
QUE 14. Write the program in Java to find the sum of the following
series:
S = a - a3 + a5 - a7 + ....... to n.

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter n: ");
int n = sc.nextInt();
int sum = 0;

for (int i = 1, j = 1; i <= n; i = i + 2, j++)


{
if (j % 2 == 0)
sum = sum - Math.pow(a, i);
else
sum = sum+ Math.pow(a, i);
}

System.out.println("Sum=" + sum);
}
}
OUTPUT:

Enter a: 22
Enter n: 5
Sum=5143006

Variable Data Type Description


a int To store entered
number
n int Number of iteration
sum int To store sum of series
i int Loop control variable
j int Inner loop control
variable
QUE 15. Write the program in Java to find the sum of the following
series:
S = (a/2) + (a/5) + (a/8) + (a/11) + ....... + (a/20)

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
double sum = 0;

for (int i = 2; i <= 20; i = i + 3) {


sum = sum+a / (double)i;
}
System.out.println("Sum=" + sum);
}
}
OUTPUT:

Enter a: 20
Sum=21.923223834988537

Variable Data Type Description


a To store entered
number
sum To store sum of series
i Loop control variable
QUE 16. Write a program to input a number. Display the product of
the successors of even digits of the number entered by user.
Input: 2745
Output: 15
[Hint: The even digits are: 2 and 4
The product of successor of even digits is: 3*5= 15]

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
int prod = 1;

while (n != 0)
{
int digit = n % 10;
n = n/10;

if (digit % 2 == 0)
prod = prod * (digit + 1);
}

if (prod == 1)
System.out.println("No even digits in " + Num);
else
System.out.println("Product of even digits successors is "
+ prod);
}
}

OUTPUT:

Enter the number: 42


Product of even digits successors is 15

Variable Data Type Description


digit int The number
n int To store the entered
number
prod int To store the product
of series
QUE 17. Write a program to input a number and check and print
whether it is a Pronic number or not. [Pronic number is the number
which is the product of two consecutive integers.]
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int n = sc.nextInt();
for (int i = 1; i <= n - 1; i++)
{
if (i * (i + 1) == n)
System.out.println( n + " is a pronic number");
else
System.out.println (n + " is not a pronic number");
}
}
OUTPUT:

Enter the number to check: 42


42 is a pronic number

Variable Data Type Description


i int Loop control variable
n int Number of iteration
QUE 18. A prime number is said to be 'Twisted Prime', if the new
number obtained after reversing the digits is also a prime number.
Write a program to accept a number and check whether the number
is 'Twisted Prime' or not.
Sample Input: 167
Sample Output: 761
167 is a 'Twisted Prime'.

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int n = sc.nextInt();
if (n == 1)
{
System.out.println(n + " is not a twisted prime number");
}
Else
{
System.out.println(“ A Prime Number ” );
for (int i = 2; i <= n / 2; i++)
int c++;
{
if (n % i == 0)
System.out.println(“ Not a Prime Number” );
}
if (c=2)
{
int t = n;
int n= 0;
while (t != 0)
{
int digit = t % 10;
t /= 10;
n= n* 10 + digit;
c++;
}
for (int i = 2; i n/ 2; i++)
{
if (n% i == 0)
System.out.println(“ Not a Prime Number” );
}
}
}
if (c=2)
System.out.println(n + " is a twisted prime number");
else
System.out.println(n + " is not a twisted prime number");
}
}
}
OUTPUT:

Enter number: 64
64 is not a twisted prime number

Variable Data Type Description


i int Loop control variable
n int Number of iteration
c int To count
digit int The number
QUE 19. Write a program to input a number. Check and display
whether it is a Niven number or not. (A number is said to be Niven
which is divisible by the sum of its digits).
Example: Sample Input 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.
import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
int dS = 0;
while (num != 0)
{
int digit = num % 10;
num /= 10;
dS = dS+digit;
}
if (dS != 0 && n% dS == 0)
System.out.println(n+ " is a Niven number");
else
System.out.println(n+ " is not a Niven number");
}
}
OUTPUT:

Enter number: 100


100 is a Niven number

Variable Data Type Description


num int To store the entered
number
dS int To store the series of
sum
digit int The number
QUE 20. Write a program to accept a number and check whether it
is a 'Spy Number' or not. (A number is spy if the sum of its digits
equals the product of its digits.)
Example: Sample Input: 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1*1*2*4 = 8

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
System.out.print("Enter Number: ");
int num = sc.nextInt();

int digit, sum = 0;


int prod = 1;
while (num > 0) {
digit = num % 10;
sum = sum+digit;
prod = prod*digit;
num = 10;
}

if (sum == prod)
System.out.println(Num + " is Spy Number");
else
System.out.println(Num + " is not Spy Number");
}
}

OUTPUT:

Enter number: Enter Number: 1245


1245 is not Spy Number

Variable Data Type Description


Num int To store the entered
number
digit int The number
sum int To store the sum of
series
prod int To store the product
of series
QUE 21. Using a switch statement, write a menu driven program to:
(a) 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.
(b) Find the sum of the digits of an integer that is input by the user.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should be
displayed.

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("1. Fibonacci Series");
System.out.println("2. Sum of digits");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();

switch (ch)
{
case 1:
int a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 3; i <= 10; i++) {
int x = a + b;
System.out.print(" " + term);
a = b;
b=x
}
break;

case 2:
System.out.print("Enter number: ");
int num = sc.nextInt();
int sum = 0;
while (num != 0)
{
sum = sum+num % 10;
num = 10;
}
System.out.println("Sum of Digits " + " = " + sum);
break;

default:
System.out.println("Incorrect choice");
break;
}
}
}
OUTPUT:

1. Fibonacci Series
2. Sum of digits
Enter your choice: 42
Incorrect choice

1. Fibonacci Series
2. Sum of digits
Enter your choice: 1
0 1 1 2 3 5 8 13 21 341. Fibonacci Series
2. Sum of digits

Enter your choice: 2


Enter number: 4
Sum of Digits = 4

Variable Data Type Description


ch int To store the entered
the choice
a int variable
b int variable
sum int To store the sum of
series
x int variable
Num int To store the entered
number
QUE 22. Write program to find the sum of the following series:
S = 1 + (3/2!) + (5/3!) + (7/4!) + ....... to n
[HINT- use nested loops]

import java.util.Scanner;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
double sum = 0.0;
for (int i = 1, j = 1; i <= n; i++, j = j + 2)
{
double f = 1;
for (int k = 1; k <= i; k++) {
f = f*k;
}
sum = sum+j / f;
}
System.out.println("Sum=" + sum);
}
}
OUTPUT:

Enter n: 5
Sum=3.7

Variable Data Type Description


n int To store the entered
number
sum double To store the sum of
series
i int Loop control variable
k int Inner loop control
variable
j int Variable
f double To store the product
od series
QUE 23. Write program to find the sum of the following series:
S = a - (a/2!) + (a/3!) - (a/4!) + ....... to n

import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter n: ");
int n = sc.nextInt();
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;
}
System.out.println("Sum=" + sum);
}
}

OUTPUT:

Enter a: 42
Enter n: 5
Sum=72.1
QUE 24. Write program to find the sum of the following series:
S = (2/a) + (3/a2) + (5/a3) + (7/a4) + ....... to n

import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
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;
}
System.out.println("Sum=" + sum);
}
}

OUTPUT:

Enter a: 72
Sum=26.487321428571423
QUE 25. 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 java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int n = sc.nextInt();

System.out.print("Enter second number: ");


int b = sc.nextInt();

if( n=2)
{
System.out.println(“ Is A Prime Number “)
for (int i = 2; i <= n / 2; i++)
{

if (n % i == 0)
{
System.out.println(“ Is Not A Prime Number “)

break;
}

}
if( n!=2)
{

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

if (b % i == 0)
{
break;
}

}
if (isBPrime)
System.out.println(n + " and " + b + " are twin prime");
else
System.out.println(n + " and " + b + " are not twin prime");
}
else
System.out.println(n + " and " + b + " are not twin prime");
}
}

OUTPUT:

Enter first number: 21


Enter second number: 23
21 and 23 are not twin prime
QUE 26. Write a program to input a number and check whether it is
a prime number or not. If it is not a prime number then display the
next number that is prime.
Sample Input: 14
Sample Output: 17
QUE 27. Write the programs to display the following patterns:
(a)
1
31
531
7531
97531

import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i < 10; i = i + 2) {
for (int j = i; j > 0; j = j - 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

OUTPUT:

1
31
531
7531
97531
b)
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int a = 1, j=1 ;
for (int i = 1; i <= j ; i++) {
for ( j = 1; j <= 5; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
}
}

OUTPUT:

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
QUE 28. Write the programs to display the following patterns:

(a)
1
10
101
1010
10101
import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int a = 1, b = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print(b + " ");
else
System.out.print(a + " ");
}
System.out.println();
}
}
}

OUTPUT:

1
10
101
1010
10101
(b)
*
*#
*#*
*#*#
*#*#*

import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int a = 1, b = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print("#");
else
System.out.print("*");
}
System.out.println();
}
}
}

OUTPUT:

*
*#
*#*
*#*#
*#*#*
QUE 29. Write a program to generate a triangle or an inverted
triangle till n terms based upon the user's choice.

Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output:

1
22
333
4444
55555
Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter the number of terms 6
Sample Output:
666666
55555
4444
333
22
1
Write a menu driven program to display the following patterns:

(a)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
}
}

OUTPUT:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(b)
11111
22222
33333
44444

import java.util.Scanner;
class ANUSHKAJOSHIXA
{
public static void main()
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}

OUTPUT:

1111
2222
3333
4444

You might also like