Professional Documents
Culture Documents
Que 1
Que 1
Que 1
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]
(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
(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
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;
OUTPUT:
Enter distance travelled: 12
Fare = 92
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();
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
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;
import java.util.*;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
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
import java.util.*;
class ANUSHKAJOSHIXA
{ // CLASS OPEN
public static void main()
{
Scanner sc = new Scanner(System.in);
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
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;
OUTPUT:
0, 1, 2, 3, 6, 11, 20, 37, 68, 1250, 1, 2, 3, 6, 11, 20, 37, 68, 125
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:
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);
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;
Enter n: 12
Sum=376
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
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
Enter a: 11
Sum=4.046674107142856
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;
System.out.println("Sum=" + sum);
}
}
OUTPUT:
Enter a: 22
Enter n: 5
Sum=5143006
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;
Enter a: 20
Sum=21.923223834988537
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:
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:
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
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();
if (sum == prod)
System.out.println(Num + " is Spy Number");
else
System.out.println(Num + " is not Spy Number");
}
}
OUTPUT:
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
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
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;
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();
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)
{
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:
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