0% found this document useful (0 votes)
18 views11 pages

Java Programs for Practical Exercises

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)
18 views11 pages

Java Programs for Practical Exercises

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

*QUESTIONS FOR COMPUTER PRACTICALS*

1]Write a Program in java to find and display the diagonals of a square taking side
of the square as an input.

Ans:
// A program to find the diagonal of square.
import [Link].*;

public class Main


{
public static void main(String[] args)
{
Scanner in = new Scanner([Link]);
int a;
double d;
[Link]("Enter the side of the square:");
a=[Link]();
d=[Link](2)*a;;
[Link]("The diagonal of the square is: " +d);
}
}

-THE OUTPUT IS:


Enter the side of the square:
5
The diagonal of the square is: 7.0710678118654755

-THE VDT TABLE IS:

Variable Data Type Value

a int 5
d double 7.0710678118654755

___________________________________________________________________________________
_______

2]Write a program to find and display the sum of the given series:
s=1-a+a^2-a^3+ ................+a^10

Ans:
//program to find and display the sum of series
import [Link].*;
public class Series
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i,a;
double s=0;
[Link]("Enter the value of a");
a=[Link]();
for(i=0;i<=10;i++)
{
if(i%2==0)
s=s+[Link](a,i);
else
s=[Link](a,i);
}
[Link]("The sum of series ="+s);
}
}

-THE OUTPUT IS:


Enter the value of a
5
The sum of series = -0.6666666666666667

-THE VDT TABEL IS:

Variable Data Type Value

i int 11
a int 5
s double -0.6666666666666667
___________________________________________________________________________________
_______

3]write a program in Java to display the first 10 numbers of the fibonacci series:
0,1,1,2,3,................. .

Ans:

//A program to display first 10 numbers of fibonacci series


public class Fibonacci
{
public static void main(String args[])
{
int a,b,c,n;a=0;b=1;c=0;n=3;
[Link]("The Fibonacci series is ");
[Link](a);
[Link](b);
do
{
c=a+b;
[Link](c);
a=b;
b=c;
n=n+1;
}
while(n<=10);
}
}

-THE OUTPUT IS:


The Fibonacci series is
0
1
1
2
3
5
8
13
21
34

-THE VDT TABLE IS:

Variable Data Type Value

a int 21
b int 34
c int 55
n int 11

___________________________________________________________________________________
_______

4]write a program in java to display all the buzz numbers between p and q (where
p<q).A buzz number is a number which ends with 7 or is divisible by 7

public class Main


{
public static void main(String[] args)
{
int p = 10; // start range
int q = 100; // end range

[Link]("Buzz numbers between " + p + " and " + q + " are:");


for (int i = p; i <= q; i++)
{
if (i % 10 == 7 || i % 7 == 0)
{
[Link](i);
}
}
}
}

-THE OUTPUT IS:


Buzz numbers between 10 and 100 are:
10
14
17
20
21
27
28
30
35
37
40
42
49
50
56
57
60
63
70
77
80
84
87
90
91
98
100

-THE VDT TABLE IS:

Variable Type Initial Value Purpose

p int 10 To define the start range for the “Buzz”


numbers
q int 100 To define the end range for the “Buzz”
numbers
i int - To iterate from p to q in the for loop
___________________________________________________________________________________
_______

5]write a program to input a number and count the number of digits .the program
further checks weather the number contains odd number of digits or even number of
digits and displays the output accordingly
sample input :749
sample output : the nuber of digits = 3
the number contains odd no of digits

ans:
import [Link];

public class Main


{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number:");
int num = [Link]();

int count = 0;
int temp = num;
while (temp != 0)
{
temp /= 10;
++count;
}

[Link]("The number of digits = " + count);


if (count % 2 == 0)
{
[Link]("The number contains even number of digits.");
} else
{
[Link]("The number contains odd number of digits.");
}
}
}
-THE OUTPUT IS:
if the user enters 12345, the output will be:

Enter a number:
The number of digits = 5
The number contains odd number of digits.

-THE VDT TABLE IS:

Variable Type Initial Value Purpose

scanner Scanner new Scanner([Link]) To read the input from the user
num int User Input The number entered by the user
count int 0 To count the number of digits in num
temp int num A temporary variable used to perform the
digit count operation

___________________________________________________________________________________
_______

6]write a program in java to find and display the sum of the followong series :
S = 1^2/a + 3^2/a^2 + 5^2/a^3 + ..........................................to n
terms

Ans:
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the value for a:");
double a = [Link]();
[Link]("Enter the number of terms (n):");
int n = [Link]();

double sum = 0;
int num = 1;
for (int i = 1; i <= n; i++) {
sum += [Link](num, 2) / [Link](a, i);
num += 2;
}

[Link]("The sum of the series is: " + sum);


}
}

-THE OUTPUT IS :
Enter the value for a:
Enter the number of terms (n):
The sum of the series is: 2.375

-THE VDT TABLE IS:

Variable Type Initial Value Purpose


scanner Scanner new Scanner([Link]) To read the input from the
user
a double User Input The real number entered by the user
n int User Input The number of terms in the series
sum double 0 To store the sum of the series
num int 1 To generate the odd numbers in the
series
i int - To iterate from 1 to n in the for
loop
___________________________________________________________________________________
_______

7]write a menu driven program to accept a number from the user . check and display
whether it is a prime number or an automorphic number.
(a) Prime number: (A number is said to be prime ,if it is only divisible by 1and
itself)
EXAMPLE: 3,5,7,11,...............
(b) Automorphic number : (An automorphic number is a number which is contained in
the last digit(s) of its square.)
EXAMPLE: 25 is an automorphic number as its square is 625 and 25 is present as the
last two digits.

Ans:
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number:");
int num = [Link]();

[Link]("Choose an option:");
[Link]("1. Check if the number is prime");
[Link]("2. Check if the number is automorphic");
int option = [Link]();

switch (option) {
case 1:
if (isPrime(num)) {
[Link](num + " is a prime number.");
} else {
[Link](num + " is not a prime number.");
}
break;
case 2:
if (isAutomorphic(num)) {
[Link](num + " is an automorphic number.");
} else {
[Link](num + " is not an automorphic number.");
}
break;
default:
[Link]("Invalid option.");
}
}

public static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

public static boolean isAutomorphic(int num) {


int square = num * num;
while (num > 0) {
if (num % 10 != square % 10) {
return false;
}
num /= 10;
square /= 10;
}
return true;
}
}

-THE OUTPUT IS:


Enter a number:
Choose an option:
1. Check if the number is prime
2. Check if the number is automorphic
7 is a prime number.

-THE VDT TABLE IS:

Variable Type Initial Value Purpose

scanner Scanner new Scanner([Link]) To read the input from the


user
num int User Input The number entered by the user
option int User Input The option chosen by the user
i int - To iterate from 2 to num in the isPrime
method
square int num * num To store the square of num in the
isAutomorphic method

___________________________________________________________________________________
_______

8]write a program to input a number . check and display weather it is a niven


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

Ans:
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number:");
int num = [Link]();

int temp = num;


int sum = 0;
while (temp != 0) {
sum += temp % 10;
temp /= 10;
}

if (num % sum == 0) {
[Link](num + " is a Niven number.");
} else {
[Link](num + " is not a Niven number.");
}
}
}

-THE OUTPUT IS:


Enter a number:
18 is a Niven number.

-THE VDT TABLE IS:

Variable Type Initial Value Purpose

scanner Scanner new Scanner([Link]) To read the input from


the user
num int User Input The number entered by the user
temp int num A temporary variable used to
perform the digit sum operation
sum int 0 To store the sum of the digits of
num

___________________________________________________________________________________
_______

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


based upon users choice of triangle to be displayed
EXAMPLE 1 : EXAMPLE 2:
input: Type 1 for a triangle input:Type 2 for
an inverted triangle
Enter your choice: 1 enter your
choice:2
Enter the number of terms: 5 Enter the number
of terms:6
Sample output: Sample output :
1 666666
22 55555
333 4444
4444 333
55555 22
1
Ans:
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Type 1 for a triangle, Type 2 for an inverted
triangle");
[Link]("Enter your choice:");
int choice = [Link]();
[Link]("Enter the number of terms:");
int n = [Link]();

switch (choice) {
case 1:
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
[Link](i);
}
[Link]();
}
break;
case 2:
for (int i = n; i >= 1; i--) {
for (int j = 0; j < i; j++) {
[Link](i);
}
[Link]();
}
break;
default:
[Link]("Invalid choice.");
}
}
}

-THE OUTPUT:
Type 1 for a triangle, Type 2 for an inverted triangle
Enter your choice:
Enter the number of terms:
1
11
111

-THE VDT TABLE:

Variable Type Initial Value Purpose

scanner Scanner new Scanner([Link]) To read the input from


the user
choice int User Input The choice entered by the
user
n int User Input The number of terms in the
triangle
i int - To iterate from 1 to n or from n to
1 in the for loop
j int - To iterate from 0 to i in the
nested for loop
___________________________________________________________________________________
_______

10]Write program to find and display the sum of the given series:
1+1/2!+1/3!+1/4!+...........................................+1/n!

Ans:
import [Link];

public class Main


{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of terms (n):");
int n = [Link]();

double sum = 0;
for (int i = 1; i <= n; i++)
{
sum += 1.0 / factorial(i);
}

[Link]("The sum of the series is: " + sum);


}

public static long factorial(int num)


{
long fact = 1;
for (int i = 2; i <= num; i++)
{
fact *= i;
}
return fact;
}
}

-THE OUTPUT:
Enter the number of terms (n):
The sum of the series is: 1.6666666666666667

-THE VDT TABLE:

Variable Type Initial Value Purpose

scanner Scanner new Scanner([Link]) To read the input from


the user
n int User Input The number of terms in the
series
sum double 0 To store the sum of the
series
i int - To iterate from 1 to n in the for
loop
num int - The number for which the factorial
is calculated in the factorial method
fact long 1 To store the factorial of num
in the factorial method
______XXXXXXX_______

Common questions

Powered by AI

The program initially defines the first two terms of the series as 0 and 1. Then, it uses a loop to calculate the next term as the sum of the previous two terms and prints each term consecutively until reaching the specified count of terms .

The program follows the pattern of adding and subtracting powers of a given number \(a\) up to \(a^{10}\). It sums even powers (e.g., \(a^0, a^2, a^4, ...\)) and subtracts odd powers (e.g., \(a^1, a^3, a^5, ...\)), effectively alternating addition and subtraction for each subsequent power up to the 10th .

A Niven number is an integer that is divisible by the sum of its digits. The Java program calculates the sum by iterating through each digit of the number and adding it to a sum variable. Then, it checks if the original number is divisible by this sum. If it is, the number is classified as a Niven number .

The switch statement directs the program to one of two specific checks based on user input: (1) to check if the number is prime using a loop to verify if it is divisible only by 1 and itself, or (2) to check if it is automorphic, as previously described. Each case leads to a respective function that returns a boolean, dictating the printed output of whether the number fits the specified category .

The program uses nested loops where the outer loop manages the number of rows and the inner loop controls the number of repeated print statements per row. For a regular triangle, the outer loop increments from 1 to \(n\), and for each iteration, the inner loop repeatedly prints the row number. For an inverted pattern, the outer loop decrements from \(n\) to 1, producing the respective shapes .

The program first calculates the square of the given number. It then compares each digit of the original number starting from the least significant digit with the corresponding digit in the square, also starting from the least significant digit. If all these digits match, the number is classified as automorphic .

The program computes the diagonal of a square using the formula \(d = a \times \sqrt{2}\), where \(a\) is the side length of the square. It uses Java's Math.sqrt function to calculate the square root, and multiplies by the side length input by the user. This results in the output for the diagonal length .

The factorial function recursively calculates the factorial of a given integer, which is then used to compute the reciprocal for each term in the series. The program iterates through each term, calculates its factorial, and adds the reciprocal of this factorial to a cumulative sum to obtain the total of the series .

The Java program iterates through every integer between two user-defined limits, \(p\) and \(q\). For each integer, it checks if the number ends in 7 (i.e., \(i \% 10 == 7\)) or is divisible by 7 (i.e., \(i \% 7 == 0\)). If either condition is true, the number is identified as a Buzz number .

The program counts the number of digits in the input number using a loop that repeatedly divides the number by ten. Once the digit count is computed, it uses the modulo operation \(count \% 2\) to determine if the count is even or odd and displays a message accordingly .

You might also like