Final Comp. Project
Final Comp. Project
PROJECT
JAVA PROGRAMS
SESSION 2023 -2024
NAME: Kushaagra
CLASS: 12
SECTION: C
ROLL NO. : 19
ACKNOWLEDGEMENT
import java.util.*;
class armstrong_no
{
public static void main(String ar[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
long num=sc.nextLong();
long sum=0;
for(long temp=num;temp!=0;temp=temp/10)
{
long rem=temp%10;
sum=sum+(long)Math.pow(rem,3);
}
if(sum==num)
System.out.println("Number is armstrong");
else
System.out.println("Number is not armstrong");
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main
function.
3. Enter the number you want to check.
4. Initiate the loop to perform the process.
5. Check whether the number is Armstrong or not
and print accordingly.
6. STOP.
`
JAVA PROGRAM TO CHECK WHETHER A NUMBER IS AUTOMORPHIC NUMBER OR NOT.
import java.util.*;
class automorphic_no
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter any number");
long n=sc.nextLong();
long sq=(long)Math.pow(n,2);
long sum=0;
for(long temp=n;temp>0;temp=temp/10)
{
sum++;
}
long p=(long)Math.pow(10,sum);
if (sq%p==n)
{
System.out.println("automorphic number");
}
else
System.out.println("not automorphic number");
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Enter the number you want to check.
4. Initiate the loop to perform the process.
5. Check whether the number is automorphic or not and print accordingly.
6. STOP.
JAVA PROGRAM TO CHECK WHETHER A NUMBER IS NIVEN NUMBER OR NOT.
import java.util.*;
class niven_number
{
public static void main(String ar[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
long num=sc.nextLong();
long sum=0;
for(long temp=num;temp!=0;temp=temp/10)
{
long rem=temp%10;
sum+=rem;
}
if (num%sum==0)
System.out.println("it is a niven number");
else
System.out.println("it is not a niven number");
}
}
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Enter the number you want to check.
4. Initiate the loop to perform the process.
5. Check whether the number is niven or not and print accordingly.
6. STOP.
OUTPUT :
JAVA PROGRAM TO CHECK WHETHER A NUMBER ISNEON NUMBER OR NOT.
import java.util.*;
class neon_no
{
public static void main(String ar[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
long num=sc.nextLong();
long sum=0;
long sq=num*num;
for(long temp=sq;temp!=0;temp=temp/10)
{
long rem=temp%10;
sum+=rem;
}
if (sum==num)
System.out.println("it is a neon number");
else
System.out.println("it is not a neon number");
}
}
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Enter the number you want to check.
4. Initiate the loop to perform the process.
5. Check whether the number is neon or not and print accordingly.
6. STOP.
OUTPUT :
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Enter the number you want to check.
4. Initiate the loop to perform the process.
5. Check whether the number is a duck number or not and print accordingly.
6. STOP.
OUTPUT :
JAVA PROGRAM TO CHECK WHETHER A NUMBER IS DISARIUM NUMBER OR NOT.
import java.util.*;
class disarium_no{
public static void main(String afgrg[]){
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
long num=sc.nextLong();
long sum=0, p=0;
for(long temp=num;temp>0;temp=temp/10)
{
long rem=temp%10;
sum++;
}
for(long temp=num;temp>0;temp=temp/10)
{
long rem=temp%10;
p=p+(long)Math.pow(rem,sum);
sum--;
}
if(p==num)
System.out.println("disarium");
else
System.out.println("not disarium");
}
}
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Enter the number you want to check.
4. Initiate the loop to perform the process.
5. Check whether the number is disarium number or not and print accordingly.
6. STOP.
OUTPUT :
JAVA PROGRAM TO CHECK WHETHER A NUMBER IS PALINDROME NUMBER OR
NOT.
import java.util.*;
class palindrome_no
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
long n=sc.nextLong();
long rev=0;
for(long temp=n;temp>0;temp=temp/10)
{
long rem=temp%10;
rev=rev*10+rem;
}
if(rev==n)
{
System.out.println("number is palindrome");
}
else
{
System.out.println("number is not palindrome");
}
}
}
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Enter the number you want to check.
4. Initiate the loop to perform the process.
5. Check whether the number is palindrome or not and print accordingly.
6. STOP.
OUTPUT :
JAVA PROGRAM TO CHECK WHETHER A NUMBER IS SUPER SIX NUMBER OR NOT.
import java.util.*;
class super_six_number
{
public static void main(String ar[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
long num=sc.nextLong();
long flag=0;
for(long temp=num;temp!=0;temp=temp/10)
{
long rem=temp%10;
if(rem==6)
flag++;
}
if (flag>=2)
System.out.println("it is a super six number");
else
System.out.println("it is not a super six number");
}
}
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Enter the number you want to check.
4. Initiate the loop to perform the process.
5. Check whether the number is super six or not and print accordingly.
6. STOP.
OUTPUT :
● PROGRAMS BASED ON ARRAYS(SINGLE DIMENSION)
JAVA PROGRAM TO SORT THE GIVEN ARRAY USING BUBBLE SORT TECHNIQUE.
import java.util.*;
public class bubble_sort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the length of array :");
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println("enter the elements of array :");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(int i = 0 ;i <n;i++)
{
for(int j = 0 ;j < n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("the sorted array is :");
for(int i = 0 ;i < n;i++)
{
System.out.print(arr[i]+" ");
}
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Inputting the length of the array.
4. Initializing and entering the elements of the array.
5. Using for loops to sort the array by bubble sort technique.
6. Print the sorted array.
7. STOP.
OUTPUT :
1. START.
2. Import the util package and create class and main function.
3. Inputting the length of the array.
4. Initializing and entering the elements of the array in ascending order.
5. Entering the element, the user wants to search.
6. Searching the element using binary search technique.
7. Printing the position of the element if it is present in the array otherwise printing
“NUMBER IS NOT PRESENT IN THE GIVEN ARRAY”
8. STOP.
OUTPUT:
import java.util.*;
class merging
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the length of the 2 arrays :");
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int arr1[] = new int[n1];
System.out.println("enter the elements of the first array");
for(int i = 0; i < n1; i++)
{
arr1[i] = sc.nextInt();
}
System.out.println("enter the elements of the second array");
int arr2[] = new int[n2];
for(int i = 0; i < n2; i++)
{
arr2[i] = sc.nextInt();
}
int arr3[] = new int[n1+n2];
for(int i =0;i<n1;i++)
{
arr3[i] = arr1[i];
}
for(int i =0;i<n2;i++)
{
arr3[i+n1] = arr2[i];
}
System.out.println("the merged array is :");
for(int i = 0; i < (n1+n2); i++)
{
System.out.print(arr3[i]+" ");
}
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Inputting the length of the two arrays
4. Entering the elements of first array.
5. Entering the elements of the second array.
6. Creating a new array of sum of length of the initial two arrays.
7. Copying the elements of the two arrays in the third one to create one single array.
8. Printing the merged array.
9. STOP.
OUTPUT :
● PROGRAMS BASED ON ARRAYS(DOUBLE DIMENSION)
OUTPUT :
import java.util.*;
class columns_sort
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter number of rows and columns :");
int r = sc.nextInt();
int c = sc.nextInt();
int arr[][] = new int[r][c];
System.out.println("enter the elements :");
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
arr[i][j] = sc.nextInt();
}
}
for (int j = 0; j < c; j++)
{
for (int i = 0; i < r - 1; i++)
{
for (int k = i + 1; k < r; k++)
{
if (arr[i][j] > arr[k][j])
{
int temp = arr[i][j];
arr[i][j] = arr[k][j];
arr[k][j] = temp;
}
}
}
}
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Inputting the number of rows and column of the array.
4. Entering the elements of the array.
5. Sorting the columns of the array one my one.
6. Printing the sorted array.
7. STOP.
OUTPUT :
import java.util.*;
class transpose
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter rows and colomns of the array");
int r = sc.nextInt();
int c = sc.nextInt();
int arr[][] = new int[r][c];
System.out.println("enter the elements of the array");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
arr[i][j] = sc.nextInt();
}
}
int transpose[][]= new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
transpose[j][i]=arr[i][j];
}
}
System.out.println("the transposed array is");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(transpose[i][j]+" ");
}
System.out.println();
}
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Inputting the number of rows and column of the array.
4. Entering the elements of the array.
5. Finding the transpose of the given matrix and storing it in another matrix.
6. Printing the transpose of the matrix.
7. STOP.
OUTPUT :
● PROGRAMS BASED ON STRING
JAVA PROGRAM TO CHECK FOR A PALINDROME WORD.
import java.util.*;
class palindrome_word
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the word");
String n=sc.next();
n=n.toLowerCase();
String join="";
for(int i=0;i<n.length();i++)
{
char ch=n.charAt(i);
join=ch+join;
}
if(join.equals(n))
System.out.println("palindrome word");
else
System.out.println("not palindrome word");
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Inputting the number of rows and column of the array.
4. Entering the elements of the array.
5. Finding the transpose of the given matrix and storing it in another matrix.
6. Printing the transpose of the matrix.
7. STOP.
OUTPUT :
JAVA PROGRAM TO SEARCH FOR THE LONGEST AND SHORTEST WORD.
import java.util.*;
class longest_shortest {
public static void main(String ar[]) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the string");
String n1=sc.nextLine();
StringTokenizer ob1 = new StringTokenizer(n1);
StringTokenizer ob2 = new StringTokenizer(n1);
String word="", longest="", shortest=null;
int l1,l2=0;
for(;ob1.hasMoreTokens();)
{
word=ob1.nextToken();
l1=word.length();
if(l1>l2)
{
l2=l1;
longest=word;
}
}
for(;ob2.hasMoreTokens();)
{
word=ob2.nextToken();
l1=word.length();
if(shortest == null || word.length() <shortest.length())
shortest=word;
}
System.out.println("LONGEST WORD : "+longest);
System.out.println("SHORTEST WORD : "+shortest);
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Inputting the number of rows and column of the array.
4. Entering the elements of the array.
5. Finding the transpose of the given matrix and storing it in another matrix.
6. Printing the transpose of the matrix.
7. STOP.
OUTPUT :
● PROGRAMS BASED ON RECURSION
JAVA PROGRAM TO FIND HCF OF TWO NUMBERS USING RCURSION.
import java.util.*;
class HCF
{
public static void main(String[] args)
{
Scanner sc =new Scanner(System.in);
System.out.println("ENTER TWO NUMBERS :");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int hcf = findHCF(num1, num2);
System.out.println("HCF of " + num1 + " and " + num2 + " is " + hcf);
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Inputting the range.
4. Calling the SERIES function in recursive form to print the fibonacci series.
5. STOP.
OUTPUT :
● PROGRAMS BASED ON NESTED LOOPS AND
CONDITIONS
⮚ JAVA PROGRAM TO PRINT A PATTERN OF N ROWS.
import java.util.*;
class pattern
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter number of rows");
int n=sc.nextInt();
{
for(int i=1;i<=n;i++)
{
for (int j=n;j>=i;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Inputting the number of rows.
4. Using nested loops for printing the pattern.
5. STOP.
OUTPUT :
JAVA PROGRAM TO PRINT A PYRAMID OF N ROWS.
import java.util.*;
class pyramid
{
public static void main(String ar[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter n terms");
int n=sc.nextInt();
int i,j,m,s;
for(i=1;i<=n;i++)
{
for(s=n;s>i;s--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print(j);
}
for(m=i-1;m>=1;m--)
{
System.out.print(m);
}
System.out.println();
}
}
}
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Inputting the number of rows.
4. Using nested loops for printing the pattern.
5. STOP.
OUTPUT :
JAVA PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION.
import java.util.*;
class quadratic_equation
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("format of a quadratic equation : a*x*x + b*x + c = 0");
System.out.println("enter value of a, b, c respectively");
double a=sc.nextDouble();
double b=sc.nextDouble();
double c=sc.nextDouble();
double r1 = (-b + (Math.sqrt((b*b)-(4*a*c))))/(2*a);
double r2 = (-b - (Math.sqrt((b*b)-(4*a*c))))/(2*a);
System.out.println("roots for the equation : "+r1+","+r2);
}
}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Printing the format of the quadratic equation and inputting the values.
4. Using formula to calculate the roots of the equation.
5. Printing the roots.
6. STOP.
OUTPUT :
A MENU DRIVEN JAVA PROGRAM TO FIND THE ARE OF THE SHAPE CHOOSE BY
USER.
import java.util.*;
class Area_switch_case
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("MENU :");
System.out.println("1.Area of circle");
System.out.println("2.Area of rectangle");
System.out.println("3.Area of square");
System.out.println("Enter your choice");
int choice=sc.nextInt();
switch(choice)
{
case 1: System.out.println("Input radius");
double r=sc.nextDouble();
double ans=3.14*r*r;
System.out.println("Area of circle is ="+ans);
break;
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Printing the menu.
4. Entering the choice of the user.
5. Inputting the dimensions of the shape.
6. Calculating and printing the area of the shape chosen by user.
7. STOP.
OUTPUT :
ALGORITHM:
1. START.
2. Import the util package and create class and main function.
3. Printing the menu.
4. Entering the choice of the user.
5. Inputting the dimensions of the shape.
6. Calculating and printing the area of the shape chosen by user.
7. STOP.
OUTPUT :
import java.util.*;
class calculator {
public static void main(String ar[]) {
Scanner sc = new Scanner(System.in);
System.out.println("menu:");
System.out.println("+:Addition");
System.out.println("-:Subtraction");
System.out.println("*:Multiplication");
System.out.println("/:Division");
System.out.println("enter your choice");
char ch=sc.next().charAt(0);
int x,y;
switch(ch){
case'+':System.out.println("enter two numbers");
x=sc.nextInt();
y=sc.nextInt();
int sum=x+y;
System.out.println("sum="+sum);
break;
case'-':System.out.println("enter two numbers");
x=sc.nextInt();
y=sc.nextInt();
int dif=x-y;
System.out.println("difference="+dif);
break;
case'*':System.out.println("enter two numbers");
x=sc.nextInt();
y=sc.nextInt();
int mul=x*y;
System.out.println("product="+mul);
break;
case'/':System.out.println("enter dividend");
x=sc.nextInt();
System.out.println("enter divisor");
y=sc.nextInt();
int div=x/y;
int rem=x%y;
System.out.println("quotient="+div);
System.out.println("remainder="+rem);
break;
default:System.out.println("wrong choice");
}}}
ALGORITHM :
1. START.
2. Import the util package and create class and main function.
3. Printing the menu.
4. Entering the choice of the user.
5. Entering the numbers from the user to perform the desired calculation.
6. Calculating and printing the desired output by the user.
7. STOP.
OUTPUT :
OUTPUT :