finals computer practicals EDITED
finals computer practicals EDITED
finals computer practicals EDITED
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,................. .
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 cheks wheather 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.
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;
}
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.");
}
}
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;
}
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.");
}
}
}
THE OUTPUT IS:
Enter a number:
18 is 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);
}
System.out.println("The sum of the series is: " + sum);
}
THE OUTPUT:
Enter the number of terms (n):
The sum of the series is: 1.6666666666666667