finals computer practicals EDITED =
finals computer practicals EDITED =
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.*;
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);
}
}
3]write a program in Java to display the first 10 numbers of the fibonacci series:
0,1,1,2,3,................. .
Ans:
___________________________________________________________________________________
___________________________________________________________________________________
_________________________
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
___________________________________________________________________________________
___________________________________________________________________________________
________________________
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;
int count = 0;
int temp = num;
while (temp != 0)
{
temp /= 10;
++count;
}
Enter a number:
The number of digits = 5
The number contains odd number of digits.
___________________________________________________________________________________
___________________________________________________________________________________
_________________________
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;
double sum = 0;
int num = 1;
for (int i = 1; i <= n; i++) {
sum += Math.pow(num, 2) / Math.pow(a, i);
num += 2;
}
___________________________________________________________________________________
___________________________________________________________________________________
_________________________
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;
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.");
}
}
Ans:
import java.util.Scanner;
if (num % sum == 0) {
System.out.println(num + " is a Niven number.");
} else {
System.out.println(num + " is not a Niven number.");
}
}
}
___________________________________________________________________________________
___________________________________________________________________________________
_
Ans:
import java.util.Scanner;
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
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;
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / factorial(i);
}
THE OUTPUT:
Enter the number of terms (n):
The sum of the series is: 1.6666666666666667
_______________________________________________________________XXXXXXX_____________
__________XXXXXXX____________________________________________________________