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

Computer Practical Project

The document contains Java code examples for various programming tasks, including checking for automorphic and palindrome numbers, calculating series sums, and printing patterns. Each code snippet is accompanied by a variable classification table that describes the purpose, type, and scope of each variable used. The document serves as a reference for understanding basic Java programming concepts and variable usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Computer Practical Project

The document contains Java code examples for various programming tasks, including checking for automorphic and palindrome numbers, calculating series sums, and printing patterns. Each code snippet is accompanied by a variable classification table that describes the purpose, type, and scope of each variable used. The document serves as a reference for understanding basic Java programming concepts and variable usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Question1:-

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;-

Here's the variable classification table for the given code:


Variable Purpose Type Scope Notes

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

Used in both cases


Stores the number entered by Inside main
n int (Automorphic and
the user for checking method
Palindrome checks)

Stores the original value of n


Inside case Used to compare with the
o for comparison in int
1 last digits of n^2 (square)
Automorphic check

Used to check for


Inside case
q Stores the square of n int Automorphic property
1
(square comparison)

Counts the number of digits Inside case Used to extract the last d
d int
in n 1 digits from n^2

Inside case Used to compare the last d


l Stores the last d digits of q int
1 digits of the square q

Used to compare the


Stores the original number for Inside case
a int original number with its
Palindrome check 2
reversed form

Inside case Used to check if the


b Stores the reversed number int
2 number is a Palindrome

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;-

Here is the variable classification table for your code:


Variable
Type Scope Description
Name

Local variable in
scanner Scanner Used to read user input from the console.
main() method

Local variable in Stores the choice entered by the user (1 or 2)


z int
main() method to decide the series to display.

x int Local variable in Stores the base value of the series (X = 2).
Variable
Type Scope Description
Name

case 1 block

Local variable in Stores the sum of the series calculated in case


sum int
case 1 block 1.

Local variable in Stores the result of Math.pow(x, i) which is


r int
case 1 block used to compute each term in the series.

Local variable in Stores the number used to generate the series


num int
case 2 block 1, 11, 111, 1111, 11111.

Used as the loop counter in both case 1 and


Local variable in
i int case 2 for iterating over the number of terms
for loops
or series digits.

Question 3:-

import java.util.Scanner;

public class question3


{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();

double s = 0;

for (int i = 1; i <= n; i++)


{
int a = 0;
int b = 1;

for (int j = 1; j <= i; j++)


{
a += j;
b *= j;
}

s += (double) a / b;
}

System.out.println("Sum of the series is: " + s);


}
}

OUTPUT;-

Here is the variable classification table for your code:


Variable
Type Scope Description
Name

Local variable in
sc Scanner Used to take input from the user.
main() method

Stores the value of n entered by the user,


Local variable in
n int used to determine the number of terms in the
main() method
series.

Local variable in
s double Holds the cumulative sum of the series.
main() method

Local variable in Loop counter for iterating over the terms of


i int
the outer loop the series.

Local variable in Used to store the sum of numbers from 1 to i


a int
the inner loop for each term in the series.

Local variable in Used to store the product of numbers from 1


b int
the inner loop to i for each term in the series.

Local variable in Loop counter for calculating the sum and


j int
the inner loop product for each term.

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;-

Here is the variable classification table for your provided code:


Variable
Type Scope Description
Name

in Scanner Local variable in A Scanner object used to read input from the
Variable
Type Scope Description
Name

main() method user.

Local variable in
x int Stores the number entered by the user.
main() method

Used to iterate through the digits of the


Local variable in
i int number (x). It gets modified in the while
main() method
loop.

Local variable in Loop counter for calculating the factorial of


j int
the for loop each digit.

Local variable in Stores the current digit of the number i


a int
main() method (extracted using modulus).

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;

for (int i = 1; i <= 10; i++)


{
int f = 1;

if (i % 2 == 0) {
for (int j = 1; j <= i; j++)
{
f *= j;
}
s += t * f;
} else {
s += t * i;
}

t *= -1;
}

System.out.println("Sum of the series is: " + s);


}
}

OUTPUT;-

Here is the variable classification table for your provided code:


Variable
Type Scope Description
Name

Local variable in main()


s int Stores the cumulative sum of the series.
method

Local variable in main() Used to alternate the sign of the terms


t int
method in the series.

i int Local variable in the for Loop counter to iterate through


Variable
Type Scope Description
Name

loop numbers from 1 to 10.

Local variable in the for


f int Holds the factorial of i when i is even.
loop

Local variable in the Loop counter to calculate the factorial


j int
inner for loop of i.

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

Local variable in Loop counter for the outer for loop,


a int
main() method controlling the rows.

Local variable in Loop counter for the inner for loop,


b int
main() method controlling the numbers printed in each row.

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;-

Here is the variable classification table for your provided code:


Variable
Type Scope Description
Name

Local variable in Loop counter for the outer for loop,


i int
main() method controlling the rows.

Local variable in Loop counter for the inner for loop,


j int
main() method controlling the numbers printed in each row.

Declared but not used in the program. It can


Local variable in be removed.
x int
main() method

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.

Controls the number being printed in each row,


Local variable in
r int initialized as 9 and decremented by 2 after each
main() method
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.

Loop counter for the inner for loop, controlling


Local variable in
j int the number of times the number k is printed per
main() method
row.

Local variable in A counter that starts at 0 and increments by 1 in


k int
main() method every iteration of the inner loop.

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();

for(a=4; a>0; a--)


{
for (b=1;b<=a; b++)
{
System.out.print("*");
}
System.out.println();
}
}
}

OUTPUT;-

Variable Classification Table


Variable Type Description

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.

You might also like