Computer Practical Project
Computer Practical Project
import java.util.Scanner;
public class question1
{
public static void main()
{
Scanner s = new Scanner(System.in);
System.out.println("1. Automorphic number");
System.out.println("2. Palindrome number");
System.out.print("Enter your choice: ");
int c = s.nextInt();
System.out.print("Enter number: ");
int n = s.nextInt();
switch (c)
{
case 1:int o = n;
int q = n * n;
int d = 0;
while (n > 0)
{
d++;
n /= 10;
}
int l = (int)(q % Math.pow(10, d));
if (l == o)
System.out.println(o + " is automorphic");
else
System.out.println(o + " is not automorphic");
break;
case 2:
int a = n;
int b = 0;
while (n > 0) {
int p = n % 10;
b = b * 10 + p;
n = n / 10;
}
if (a == b) {
System.out.println(a + " is a Palindrome number.");
} else {
System.out.println(a + " is not a Palindrome number.");
}
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
OUTPUT;-
c Stores the user's choice for int Inside main Used to control the flow of
Variable Purpose Type Scope Notes
checking Automorphic or
method the switch statement
Palindrome number
Counts the number of digits Inside case Used to extract the last d
d int
in n 1 digits from n^2
Stores the last digit of n in Inside case Used to reverse the digits
p int
Palindrome check 2 of the number n
Question 2:-
import java.util.Scanner;
public class Question2
{
public static void main()
{
Scanner scanner = new Scanner(System.in);
System.out.println("1. Find the sum of the series X1 - X2 + X3 - X4 + ... - X20 (X = 2)");
System.out.println("2. Display the series 1, 11, 111, 1111, 11111");
System.out.print("Enter your choice (1/2): ");
int z = scanner.nextInt();
switch (z)
{
case 1:
int x = 2;
int sum = 0;
for (int i = 1; i <= 20; i++)
{
int r = (int)Math.pow(x, i);
if (i % 2 == 0)
{
sum -= r;
} else
{
sum += r;
}
}
System.out.println("Sum of the series: " + sum);
break;
case 2:
int num = 1;
for (int i = 0; i < 5; i++) {
System.out.print(num + " ");
num = num * 10 + 1;
}
System.out.println();
break;
default:
System.out.println("Invalid option. Please choose a valid option (1/2/3).");
}
}
}
OUTPUT;-
Local variable in
scanner Scanner Used to read user input from the console.
main() method
x int Local variable in Stores the base value of the series (X = 2).
Variable
Type Scope Description
Name
case 1 block
Question 3:-
import java.util.Scanner;
double s = 0;
s += (double) a / b;
}
OUTPUT;-
Local variable in
sc Scanner Used to take input from the user.
main() method
Local variable in
s double Holds the cumulative sum of the series.
main() method
Question 4:-
import java.util.*;
public class question4
{
public static void main()
{
Scanner in= new Scanner( System.in);
System.out.println("Enter a number:");
int x= in.nextInt();
int i ,j,a,b,c;
i=x;
while (i>0)
{
a=i%10;
int s=1;
for(j=1;j<=a;j++)
{
s=s*j;
}
System.out.println("Factorial of"+a+"="+s);
i= i/10;
}
}
}
OUTPUT;-
in Scanner Local variable in A Scanner object used to read input from the
Variable
Type Scope Description
Name
Local variable in
x int Stores the number entered by the user.
main() method
Local variable in
s int Stores the factorial of the digit a.
main() method
Question 5:-
public class question5
{
public static void main()
{
int s = 0;
int t = 1;
if (i % 2 == 0) {
for (int j = 1; j <= i; j++)
{
f *= j;
}
s += t * f;
} else {
s += t * i;
}
t *= -1;
}
OUTPUT;-
Question 6:-
public class question6
{
public static void main()
{
int a,b;
for(a=1;a<=5;a++)
{
for(b=a;b>0;b--)
{
System.out.print(b);
}
System.out.println();
}
}
}
OUTPUT;-
Here is the variable classification table for your provided code:
Variable
Type Scope Description
Name
Question 7:-
public class question7
{
public static void main()
{
int i,j,x;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
OUTPUT;-
Question 8:-
public class quesion8
{
public static void main ()
{
int i,j,r=9;
for(i=5;r>=1;r-=2)
{
for(j=1;j<=5;j++)
{
System.out.print(r);
}
System.out.println();
}
}
}
OUTPUT;-
Here is the variable classification table for the code you provided:
Variable
Type Scope Description
Name
Local variable in Loop counter for the outer for loop (though not
i int
main() method used in the loop).
Local variable in Loop counter for the inner for loop, controlling
j int
main() method the number of prints in each row.
Question 9:-
public class question9
{
public static void main()
{
int i,j,k=0;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
k=1+k;
System.out.print(k+" ");
}
System.out.println();
}
}
}
OUTPUT;-
Here is the variable classification table for the code you provided:
Variable
Type Scope Description
Name
Local variable in Loop counter for the outer for loop that runs 5
i int
main() method times.
Question 10:-
public class question10
{
public static void main()
{
int i,j,a,b;
for(i=1;i<=5;i++)
{
for(j=1;j<=i; j++)
{
System.out.print("*");
}
System.out.println();
OUTPUT;-
i int Loop control variable for the first half of the pattern (increasing stars).
j int Loop control variable for printing stars in the first half of the pattern.
Loop control variable for the second half of the pattern (decreasing
a int
stars).
Loop control variable for printing stars in the second half of the
b int
pattern.