0% found this document useful (0 votes)
20 views

finals computer practicals EDITED =

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

finals computer practicals EDITED =

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

*QUESTIONS*

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 java.util.*;

public class Main


{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a;
double d;
System.out.println("Enter the side of the square:");
a=in.nextInt();
d=Math.sqrt(2)*a;;
System.out.println("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 java.util.*;
public class Series
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i,a;
double s=0;
System.out.println("Enter the value of a");
a=in.nextInt();
for(i=0;i<=10;i++)
{
if(i%2==0)
s=s+Math.pow(a,i);
else
s=s-Math.pow(a,i);
}
System.out.pintln("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;
System.out.println("The Fibonacci series is ");
System.out.println(a);
System.out.println(b);
do
{
c=a+b;
System.out.println(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

System.out.println("Buzz numbers between " + p + " and " + q + " are:");


for (int i = p; i <= q; i++)
{
if (i % 10 == 7 || i % 7 == 0)
{
System.out.println(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 java.util.Scanner;

public class Main


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
int num = scanner.nextInt();

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

System.out.println("The number of digits = " + count);


if (count % 2 == 0)
{
System.out.println("The number contains even number of digits.");
} else
{
System.out.println("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(System.in) |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 java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the value for a:");
double a = scanner.nextDouble();
System.out.println("Enter the number of terms (n):");
int n = scanner.nextInt();

double sum = 0;
int num = 1;
for (int i = 1; i <= n; i++) {
sum += Math.pow(num, 2) / Math.pow(a, i);
num += 2;
}

System.out.println("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(System.in)| 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 java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
int num = scanner.nextInt();

System.out.println("Choose an option:");
System.out.println("1. Check if the number is prime");
System.out.println("2. Check if the number is automorphic");
int option = scanner.nextInt();

switch (option) {
case 1:
if (isPrime(num)) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
break;
case 2:
if (isAutomorphic(num)) {
System.out.println(num + " is an automorphic number.");
} else {
System.out.println(num + " is not an automorphic number.");
}
break;
default:
System.out.println("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(System.in)|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 wheather it is a niven


number or not. (A number is said to be Niven whenit 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 java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
int num = scanner.nextInt();

int temp = num;


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

if (num % sum == 0) {
System.out.println(num + " is a Niven number.");
} else {
System.out.println(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(System.in) | 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 java.util.Scanner;

public class Main {


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

switch (choice) {
case 1:
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}
break;
case 2:
for (int i = n; i >= 1; i--) {
for (int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}
break;
default:
System.out.println("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(System.in) | 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 java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of terms (n):");
int n = scanner.nextInt();

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

System.out.println("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(System.in) | 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_____________
__________XXXXXXX____________________________________________________________

You might also like