Computer Project File
Computer Project File
Project File
Teacher’s Singnature
Acknowledgement
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;
}
1
Variable
Datatpe Function
name
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(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
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
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
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
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();
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
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
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];
15
Variable
Datatpe Function
name
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
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.");
}
}
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();
}
21
Variable
Datatpe Function
name
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.");
}
}
originalNumber = n;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += Math.pow(remainder, digits);
originalNumber /= 10;
}
return result == n;
}
}
23
Variable
Datatpe Function
name
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.");
}
}
25
Variable
Datatpe Function
name
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.");
}
}
27
Variable
Datatpe Function
name
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.");
}
}
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
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
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];
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
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
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();
}
}
39
Variable
Datatpe Function
name
40