Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Computer

Project File

Name - Anubhav Singh


Class - XII A
Roll No. - 05
Certificate

This is to certify Anubhav


Singh of Std. XII A has
successfully completed this
project on Computer for the
academic year 2024-25
under my guidance.

Teacher’s Singnature
Acknowledgement

It is my pleasure and proud


privilege to place on record
my deep and sincerest
gratitude to the Almighty, my
parents and my teachers who
have been a source of
constant support throughout
this project.
A special word of
appreciation for my elder
sister, Sanskriti Singh for
helping me in one way or the
other.

Anubhav Singh
XII A
Programs, Their
Variable
Description and
Their Outputs
Q-1 Write a program in java to check
whether a given number is an Adam
number or not.
import java.util.Scanner;
public class Adam_number {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to be checked");
int num = sc.nextInt();
int sq = num*num;

int rev_num = 0;
while(num !=0) {
int rem = num%10;
rev_num = rev_num*10 + rem;
num = num/10;
}

int rev_sq = 0;
while(sq!=0) {
int r = sq%10;
rev_sq = rev_sq*10 + r;
sq = sq/10;
}

if(rev_sq == (rev_num * rev_num))


System.out.println("The number is an Adam
number");
else
System.out.println("The number is NOT an Adam
number");
}
}

1
Variable
Datatpe Function
name

to store the number entered by the


num int
user

sq int to store the square of num

to store the reverse of the original


rev_num int
number

to store the remainder when the


rem int
original number is divided by 10.

to store the reverse of the square of


rev_sq int
the original number

to store the remainder when the


r int square of the original number is
divided by 10.

2
Q-2 Write a program in java to check
whether a given number is a Duck number
or not.

import java.util.Scanner;
public class Duck_number {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be
checked");
String num = sc.next();

int count = 0;
for(int i=0;i<num.length();i++) {
char ch = num.charAt(i);

if(num.charAt(0)!= 0 && ch == '0')


count++;
}

if(count!=0)
System.out.println("The number is a Duck number");
else
System.out.println("The number is NOT a Duck
number");
}
}

3
Variable
Datatpe Function
name

to store the number entered by the


num String
user

to count the occurrence of the


count int
digit '0' in the input number.

to use as a loop counter in the for


i int loop to iterate through the digits of
the input number.

to store each character of the input


ch char
number during the iteration.

4
Q-3 Write a program in java to check
whether a given number is a Dudeney
number or not.

import java.util.Scanner;
public class Dudeney_number {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be
checked");
int num = sc.nextInt();
int n = num;

int digi = 0;
while(num!=0) {
digi = digi + num%10;
num = num/10;
}

if(digi == (int)(Math.cbrt(n)))
System.out.println("The number is a Dudeney
number");
else
System.out.println("The number is NOT a Dudeney
number");
}
}

5
Variable
Datatpe Function
name

to store the number entered by the


num int
user

to store the original value of the


n int input number for comparison
purposes

to store the sum of the individual


digi int
digits of the input number.

6
Q-4 Write a program in java to check
whether a given number is a Fascinating
number or not.

import java.util.Scanner;
public class Fascinating_numbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be checked");
int number = sc.nextInt();
String con = Integer.toString(number) +
Integer.toString(number*2) + Integer.toString(number*3);
int x[] = new int[9];

for(int i =0;i<con.length();i++) {
char ch = con.charAt(i);
x[i] = Character.getNumericValue(ch);
}
int count = 0;
if(con.length()!=9)
System.out.println("The number is NOT fascinating
number");
else {
for(int i =1;i<=9;i++) {
for(int j = 0;j<9;j++) {
if(x[j] == i)
count = count +1;
}
}
}

if(count == 9)
System.out.println("The number is fascinating number");
else
System.out.println("The number is NOT fascinating
number");
}
}
7
Variable
Datatpe Function
name

to store the number entered by the


number int
user

to store the concatenation of the


original number, the number
con String multiplied by 2, and the number
multiplied by 3, all represented as
strings.

to store the individual digits of the


x int
concatenated string con.

i int used as a loop counter

to store each character of the


ch char concatenated string during the
iteration.

to count the occurrence of each


count int digit from 1 to 9 in the
concatenated string.

8
Q-5 Write a program in java to check
whether a given number is a Magic
number or not.

import java.util.Scanner;
public class Magic_number {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be
checked");
int num = sc.nextInt();
int n = num;

while(num>=10) {
int s_d = 0;
while(n!=0) {
s_d = s_d + n%10;
n=n/10;
}
num = s_d;
n = num;
}

if(num == 1)
System.out.println("The number is a Magic
number");
else
System.out.println("The number is NOT a MAgic
number");
}
}

9
Variable
Datatpe Function
name

to store the number entered by the


num int
user

to store the original value of the


n int input number for repeated digit
sum calculations.

to store the sum of the individual


s_d int
digits of the input number

10
Q-6 Write a program in java to check
whether a given number is a Triangular
number or not.

import java.util.Scanner;
public class Triangular_number {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be
checked");
int num = sc.nextInt();

int count =0;


for(int i = 1; i<=num;i++)
{
count = count +i;
if(count == num)
break;
}

if(count == num)
System.out.println("The number is a triangular
number");
else
System.out.println("The number is NOT a triangular
number");
}
}

11
Variable
Datatpe Function
name

to store the number entered by the


num int
user

to calculate the cumulative sum of


numbers from 1 to num to
count int
determine if it forms a triangular
number.

to use as a loop counter in the for


i int loop to iterate through the
numbers from 1 to num.

12
Q-7 Write a program in java to check
whether a given number is a Unique Digit
number or not.

import java.util.Scanner;
public class Unique_digit_number {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to be checked");
String num = sc.next();
int x[] = new int[num.length()];

for(int i=0;i<num.length();i++) {
char ch = num.charAt(i);
x[i] = Character.getNumericValue(ch);
}

int count = 0;
for(int i=0;i<num.length()-1;i++) {
for(int j = i+1; j<num.length();j++) {
if(x[i] == x[j])
count = count +1;
}
}

if(count == 0)
System.out.println("The number is a unique
number");
else
System.out.println("The number is NOT a unique
number");
}
}

13
Variable
Datatpe Function
name

to store the number entered by the


num String
user

to store the individual digits of the


x int
number

i int used as a loop counter

to store each character of the input


ch char
number during the iteration

to count the occurrence of


count int repeated digits in the input
number

14
Q-8 Write a program in java to print the
sum of all the elements of a 3x3 array.

import java.util.Scanner;
public class sum_of_all_elements {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x[][] = new int[3][3];

int sum =0;


for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
System.out.println("Enter the element on row "+i+"
and coloumn "+j);
x[i][j] = sc.nextInt();
sum = sum +x[i][j];
}
}

for(int i = 0; i<3; i++)


{
for(int j = 0; j<3;j++)
{
System.out.println(x[i][j]);
}
}

System.out.println("The sum of the elements is


"+sum);
}
}

15
Variable
Datatpe Function
name

x int to store the elements of the matrix.

to calculate the sum of all the


sum int
elements in the matrix.

to use as a loop counter in the for


i int loop to iterate the rows of the
matrix.

to use as a loop counter in the for


j int loop to iterate the colomns of the
matrix.

16
Q-9 Write a program in java to check
whether a given number is a Goldbach
number or not.

import java.util.Scanner;
public class GoldbachNumberChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check if it's a Goldbach
number: ");
int number = sc.nextInt();
if (number % 2 != 0 || number <= 2) {
System.out.println(number + " is not a Goldbach number.");
} else {
boolean isGoldbach = false;
for (int i = 2; i <= number / 2; i++) {
if (isPrime(i) && isPrime(number - i)) {
isGoldbach = true;
break;
}
}
if (isGoldbach == true) {
System.out.println(number + " is a Goldbach number.");
} else {
System.out.println(number + " is not a Goldbach
number.");
}
static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
return true;
}
}
17
Variable
Datatpe Function
name

to store the number entered by the


number int
user

used to track whether the given


isGoldbach boolean number is a Goldbach number or
not.

used as a loop counter in the for


i int loop to iterate through the
numbers

18
Q-10 Write a program in java to check
whether a given number is a Bouncy
number or not.

import java.util.Scanner;
public class BouncyNumberChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check if it's a
bouncy number: ");
int number = sc.nextInt();
if (isBouncyNumber(number)) {
System.out.println(number + " is a bouncy number.");
} else {
System.out.println(number + " is not a bouncy
number.");
}
}

static boolean isBouncyNumber(int n) {


boolean increasing = false;
boolean decreasing = false;
int prevDigit = n % 10;
n /= 10;
while (n > 0) {
int digit = n % 10;
if (digit < prevDigit) {
increasing = true;
} else if (digit > prevDigit) {
decreasing = true;
}
if (increasing && decreasing) {
return true;
}
prevDigit = digit;
n /= 10;
}
return false;
}
19
}
Variable
Datatpe Function
name

to store the number entered by the


number int
user

used to track if the digits of the


increasing boolean
number are in increasing order.

used to track if the digits of the


decreasing boolean
number are in decreasing order.

stores the previous digit while


prevDigit int iterating through the digits of the
number.

to store the number being checked


for bounciness and is manipulated
n int
during the iteration through its
digits.

20
Q-11 Write a program in java to print the
transverse of 3X3 matrix.

import java.util.Scanner;
public class TransposeMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] matrix = new int[3][3];
System.out.println("Enter the elements of the 3x3
matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = sc.nextInt();
}
}

System.out.println("Original Matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

System.out.println("Transpose of the Matrix:");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[j][i] + " ");
}
System.out.println();
}
}
}

21
Variable
Datatpe Function
name

to store the elements of the 3x3


matrix int
matrix entered by the user.

used as a loop counter in the for


j int
loop to iterate through the colmns

used as a loop counter in the for


i int
loop to iterate through the rows

22
Q-12 Write a program in java to check
whether a given number is an Armstrong
number or not.
import java.util.Scanner;
public class ArmstrongNumberChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check if it's an
Armstrong number: ");
int number = sc.nextInt();

if (isArmstrongNumber(number)) {
System.out.println(number + " is an Armstrong
number.");
} else {
System.out.println(number + " is not an Armstrong
number.");
}
}

static boolean isArmstrongNumber(int n) {


int originalNumber, remainder, result = 0, digits = 0;
originalNumber = n;
while (originalNumber != 0) {
originalNumber /= 10;
++digits;
}

originalNumber = n;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += Math.pow(remainder, digits);
originalNumber /= 10;
}
return result == n;
}
}
23
Variable
Datatpe Function
name

to store the number entered by the


number int
user

to store the original value of the


originalNumber int
input number for later comparison.

to store the remainder when


remainder int dividing the number by 10 during
the calculation process.

to accumulate the result of the


result int
Armstrong number calculation.

to count the number of digits in the


digits int
input number.

24
Q-13 Write a program in java to check
whether a given String is an Palindrome or
not.
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string to check if it's a
palindrome: ");
String input = sc.nextLine();

if (isPalindrome(input)) {
System.out.println(input + " is a palindrome.");
} else {
System.out.println(input + " is not a
palindrome.");
}
}

static boolean isPalindrome(String str) {


int left = 0;
int right = str.length() - 1;

while (left < right) {


if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}

25
Variable
Datatpe Function
name

to store the string entered by the


input String user to be checked for being a
palindrome.

to keep track of the left index while


left int traversing the string to check for a
palindrome.

to keep track of the right index


right int while traversing the string to check
for a palindrome.

26
Q-14 Write a program in java to check
whether a given number is an Perfect
number or not.

import java.util.Scanner;
public class PerfectNumberChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check if
it's a perfect number: ");
int number = sc.nextInt();

if (isPerfectNumber(number)) {
System.out.println(number + " is a perfect
number.");
} else {
System.out.println(number + " is not a perfect
number.");
}
}

// Function to check if a number is a perfect


number
static boolean isPerfectNumber(int n) {
int sum = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum == n;
}
}

27
Variable
Datatpe Function
name

to store the number entered by the


number int
user

to calculate the sum of all the


sum int
divisors of the number.

to use as a loop counter in the for


i int loop to check for divisors of the
original number.

to store the original number for


n int
further iterations.

28
Q-15 Write a program in java to accept an
even integer from 9 to 50. Find all odd prime
pairs whose sum is equal to the number.

import java.util.Scanner;
public class OddPrimePairsFinder {
public static void main(String[] args) {
Scanner sc = new Scaner(System.in);
System.out.println(“Enter a number”);
int number = sc.nextInt();
if (number % 2 == 0 && number >= 9 && number <= 50)
{
System.out.println("Odd prime pairs whose sum is
equal to " + number + ":");
for (int i = 3; i <= number / 2; i += 2) {
if (isPrime(i) && isPrime(number - i)) {
System.out.println(i + " + " + (number - i) + " = " +
number);
}
}
} else {
System.out.println("Please enter a valid even
integer between 9 and 50.");
}
}

static boolean isPrime(int n) {


if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
29
Variable
Datatpe Function
name

to store the number entered by the


number int
user

to use as a loop counter in the for


i int
loop.

to store the original number for


n int
further iterations.

30
Q-16 Write a program in java to print the
sum of all the elementsof the left and right
diagonals of a square matrix.
import java.util.Scanner;
public class Sum_of_diagonals {
public static void main(String args []) {
Scanner sc =new Scanner(System.in);
int a,b;
System.out.println("Enter the number of rows and columns");
a = sc.nextInt();
b = a;
int x[][] = new int[a][b];

for(int i = 0;i<a;i++) {
for(int j =0;j<b;j++) {
System.out.println("Enter the element at row "+i+" and
column "+j);
x[i][j] = sc.nextInt();
}
}

int sl = 0;
for(int i = 0;i<a;i++) {
for(int j =0;j<b;j++) {
if(i==j)
sl = sl+x[i][j];
}
}
System.out.println("The sum of the elements of the left
diagonal is = "+sl);

int sr =0;
for(int i = 0;i<a;i++) {
sr = sr + x[i][a-i-1];
}
System.out.println("The sum of the elements of the right
diagonal is = "+sr);
}
}

31
Variable
Datatpe Function
name

to store the number of rows of the


a int
matrix.

to store the number of columns of


b int
the matrix.

to store the elements of the square


x int
matrix.

used as a loop counter in the for


i int
loop for rows.

used as a loop counter in the for


j int
loop for colomns.

to store the sum of the elements of


sl int
the left diagonal.

to store the sum of the elements of


sr int
the right diagonal.

32
Q-17 Write a program in java to print the
Tribonacci series up to n terms.
import java.util.Scanner;
public class Tribonacci {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the term till which you
want tribonacci series");
int n = sc.nextInt();
int a = 0;
int b = 1;
int c =2;
int d;

if(n==1)
System.out.println(a);
else if(n == 2)
System.out.println(a+", "+b);
else if(n==3)
System.out.println(a+", "+b+", "+c);
else {
System.out.print(a+", "+b+", "+c);

for(int i=4;i<=n;i++) {
d = a+b+c;
a=b;
b=c;
c=d;
System.out.print(", "+d);
}
}
}
}
33
Variable
Datatpe Function
name

to store the term till where the


n int
Tribonacci series is to be printed.

representing the first term of the


a int
Tribonacci series.

representing the second term of


b int
the Tribonacci series.

representing the third term of the


c int
Tribonacci series.

used for calculations in the


d int
Tribonacci series.

used as a loop counter in the for


i int
loop.

34
Q-18 Write a program in java to check
whether the elements of two 2-D arrays are
equal or not.

import java.util.Scanner;
class Matching_of_arrays
{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a[][] = new int [3][3];
int b[][] = new int[3][3];

System.out.println(“Enter elements of 1st array : ”);


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a[i][j] = sc.nextInt();
}
}
System.out.println(“Enter elements of 2nd array : ”);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
b[i][j] = sc.nextInt();
}
}

int count=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(a[i][j] == b[i][j])
count++;
}
}
if(count == 9)
System.out.println(“They are equal”);
else
System.out.println(“They are not equal”);
}
}
35
Variable
Datatpe Function
name

to store the number entered by the


a int
user for the first array.

to store the number entered by the


b int
user for the second array.

to use as a loop counter in the for


i int
loop for rows.

to use as a loop counter in the for


j int
loop for rows.

to count the number of matching


count int
elements between the two arrays.

36
Q-19 Write a program in java to accept a
2D array and print the sum of its columns.
import java.util.Scanner;
public class sum_of_all_columns {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows");
int a = sc.nextInt();
System.out.println("Enter the number of columns");
int b = sc.nextInt();
int x[][] = new int[a][b];

for(int i = 0;i<a;i++) {
for(int j = 0;j<b;j++) {
System.out.println("Enter the element on row "+i+"
and coloumn "+j);
x[i][j] = sc.nextInt();
}
}

int f = 0;
for(int k = 0;k<b;k++) {
int scol = 0;
for(int i = 0;i<a;i++) {
for(int j = 0;j<b;j++) {
if(j == f) {
scol = scol + x[i][j];
}
}
}
f+=1;
System.out.println("The sum of all the elements of
column "+(f-1)+" is "+scol);
}
}
}
37
Variable
Datatpe Function
name

x int stores the elements of the matrix.

stores the number of rows in the


a int
matrix.

stores the number of columns in


b int
the matrix.

used as a counter to keep track of


f int the current column being
summed.

stores the sum of the elements of


scol int
the current column.

used as a loop counter in the for


i,j,k int
and nested for loop.

38
Q-20 Write a program in java to accept a
2D array and print the sum of its rows.

import java.util.Scanner;
public class anu {
public static void main(String args []) {
Scanner sc = new Scanner(System.in);
int x[][] = new int[3][3];

//inputing values
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
System.out.println("Enter the element on row
"+i+" and coloumn "+j);
x[i][j] = sc.nextInt();
}
}

//sum of the rows


for(int i = 0;i<3;i++)
{
int sum =0;
for(int j = 0;j<3;j++)
{
sum = sum + x[i][j];
}
System.out.println("The sum of all the elements
of row "+i+" is "+sum);
}
}
}

39
Variable
Datatpe Function
name

stores the elements entered by the


x int
user.

used in the for loops to iterate over


i int
the rows of the 2D array x.

used in the for loops to iterate over


j int
the columns of the 2D array x.

store the sum of the elements of


sum int
each row in the 2D array x.

40

You might also like