0% found this document useful (0 votes)
2 views18 pages

Java Record 10to20

The document contains Java programs for various tasks including squaring array elements, sorting arrays, searching elements, finding the largest and smallest numbers in an array, summing matrices, checking for Armstrong numbers, calculating factorials, checking for palindromes, calculating student grades, and summing integers within a specific range. Each program includes a clear aim, code implementation, and expected output. The programs utilize basic Java concepts such as arrays, loops, conditionals, and user input.

Uploaded by

antonytom2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Java Record 10to20

The document contains Java programs for various tasks including squaring array elements, sorting arrays, searching elements, finding the largest and smallest numbers in an array, summing matrices, checking for Armstrong numbers, calculating factorials, checking for palindromes, calculating student grades, and summing integers within a specific range. Each program includes a clear aim, code implementation, and expected output. The programs utilize basic Java concepts such as arrays, loops, conditionals, and user input.

Uploaded by

antonytom2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

11 .

SQUARE OF ARRAY ELEMENTS

AIM: Write a java Program to Input an Array, Store the Squares of these Elements in an
Array & Print it.

Program code

import java.io.*;

import java.util.*;

class arraysquare{

public static void main(String args[])throws IOException{

Scanner obj1=new Scanner(System.in);

int n;

System.out.println("Enter the limit");

n=obj1.nextInt();

int arr[]=new int[n];

System.out.println("Enter Array Elements");

for(int i=0;i<n;i++){

arr[i]=obj1.nextInt();}

int arr1[]=new int[n];

System.out.println("Display Square Of Array");

for(int i=0;i<n;i++){

arr1[i]=arr[i]*arr[i];

System.out.println(+arr1[i]);}}}

Output
12. SORT ARRAY ASSCENDING AND DESCENDING ORDER

AIM: Write a java program to sort an array in ascending order and in descending order.

Program code

import java.io.*;

import java.util.*;

class arrayassdesc{

public static void main(String args[])throws IOException{

Scanner obj1=new Scanner(System.in);

int n;

System.out.println("Enter the limit");

n=obj1.nextInt();

int arr[]=new int[n];

System.out.println("Enter Array Elements");

for(int i=0;i<n;i++){

arr[i]=obj1.nextInt();

for(int i=0;i<n-1;i++){

for(int j=0;j<n-1;j++){

if(arr[j]>arr[j+1]){

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

System.out.println("Array Elements in Ascending Order");

for(int i=0;i<n;i++){

System.out.println(arr[i]);
}

for(int i=0;i<n-1;i++){

for(int j=0;j<n-1;j++){

if(arr[j]<arr[j+1]){

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

System.out.println("Array Elements in Descending Order");

for(int i=0;i<n;i++){

System.out.println(arr[i]);

Output
13. SEARCH ELEMENT IN ARRAY

AIM: Write a java program to search an element in an array also print it's position.

Program code

import java.io.*;

import java.util.*;

class arraysearch

public static void main(String args[])

Scanner obj=new Scanner(System.in);

int n,s,found=0;

System.out.println("Enter the array limit");

n=obj.nextInt();

int arr[]=new int[n];

System.out.println("Enter the array elements");

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

arr[i]=obj.nextInt();

System.out.println("Enter the Element to search");

s=obj.nextInt();

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

if(arr[i]==s)

System.out.println("Element"+s+" found at index "+i);

found=1;

}
}

if(found!=1){

System.out.println("Element not found");

Output
14. LARGEST AND SMALLEST IN ARRAY

AIM: Write a java program to print largest & smallest numbers in an Array.

Program code

import java.io.*;

import java.util.*;

class arraylargesmall {

public static void main(String args[])

Scanner obj=new Scanner(System.in);

int n,max=0;

System.out.println("Enter the limit");

n=obj.nextInt();

int arr[]=new int[n];

System.out.println("Enter the elements");

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

arr[i]=obj.nextInt();

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

if(arr[i]>max)

max=arr[i];

System.out.println("Largest Element="+max);

for(int i=0;i<n;i++)
{

if(arr[i]<arr[0])

arr[0]=arr[i];

System.out.println("Smallest Element="+arr[0]);

Output
15. SUM OF TWO MATRICES

AIM: Write a java program to input the matrices A and B and find the sum of the two
matrices. Using the concepts of class and object.

Program code

import java.util.*;

class matrix{

int m1[][]=new int[5][5],i,j,r,c;

Scanner sc=new Scanner(System.in);

matrix(int r1,int c1)

r=r1;

c=c1;

matrix()

void matrixread(){

for(i=0;i<r;i++)

for(j=0;j<c;j++)

m1[i][j]=sc.nextInt();

void display(){

for(i=0;i<r;i++)
{

for(j=0;j<c;j++)

System.out.print(m1[i][j]+"\t");

System.out.println();

void add(matrix x1,matrix x2){

for(i=0;i<r;i++)

for(j=0;j<c;j++)

m1[i][j]=x1.m1[i][j]+x2.m1[i][j];

System.out.print(m1[i][j]+"\t");

System.out.println();

public class mainmatrix{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("enter the row and column");

int r=sc.nextInt();

int c=sc.nextInt();

matrix s1=new matrix(r,c);


matrix s2=new matrix(r,c);

matrix s3=new matrix(r,c);

System.out.println("enter matrix1");

s1.matrixread();

System.out.println("enter matrix2");

s2.matrixread();

System.out.println(" matrix1");

s1.display();

System.out.println(" matrix2");

s2.display();

System.out.println(" sum");

s3.add(s1,s2);

Output
16. CHECK ARMSTRONG OR NOT

AIM: Write a java program checks if a number is Armstrong or not .

Program code
import java.io.*;

import java.util.*;

class amstrong1{

void amst(){

Scanner obj1=new Scanner(System.in);

int n;

System.out.println("Enter the number");

n=obj1.nextInt();

int n1=n;

int n2=n;

int count=0;

while(n1 != 0) {

int r;

r=n1%10;

count=count+1;

n1 = n1 / 10;

int sum=0;

while(n!=0){

int r1,a;

r1=n%10;

a=1;

for(int i=1;i<=count;i++){

a=a*r1;

sum=sum+a;
n=n/10;

if(n2==sum){

System.out.println(+n2+" is amstrong number");

else{

System.out.println(+n2+" is not amstrong number");

class amstrong{

public static void main(String args[])throws IOException{

amstrong1 obj=new amstrong1();

obj.amst();

Output
17. FACTORIAL OF A NUMBER

AIM: Write a java program to print the Factorial of a number.

Program code

import java.io.*;

import java.util.*;

class factorial{

int s,a=1;

Scanner g=new Scanner(System.in);

void display(){

System.out.println("Enter the number : ");

s=g.nextInt(); }

void f(){

for(int i=1;i<=s;i++){

a=a*i;}

System.out.println("Fcatorial = " +a);}

class fact{

public static void main(String args[])throws IOException{

factorial q=new factorial();

q.display();

q.f(); }

Output
18. CHECK PALINDROME OR NOT

AIM: Write a java program to check whether a number is palindrome or not.

Program code

import java.io.*;

import java.util.*;

class pal{

int n;

Scanner g=new Scanner(System.in);

int display(){

// System.out.println("PALINDROM CHECK\n");

System.out.println("Enter the number : ");

n=g.nextInt();

return n;

int rev(){

int s=0;

while(n!=0){

int y=n%10;

s=y+(s*10);

n=n/10;

return s;

void prints(int m,int o){

if(m==o){

System.out.println(o +" is palindrom");

else{
System.out.println(m +" is not palindrom");

class pali{

public static void main(String args[])throws IOException{

pal k=new pal();

int a=k.display();

int b=k.rev();

k.prints(a,b);

Output
19. CALCULATE GRADE OF STUDENT

AIM: Write a program to read the 5 subject marks of the student, a function to calculate
the percentage, and print the grade based on the percentage.

Program code

import java.util.*;

import java.io.*;

class sub{

Scanner ob=new Scanner(System.in);

float a[]=new float[5];

String name;

void enter(){

System.out.print("Enter the name : ");

name=ob.next();

for(int i=0;i<5;i++){

System.out.println("Enter the mark of " +(i+1) +" subject : ");

a[i]=ob.nextFloat();

void avg(){

float total=0,p;

for(int i=0;i<5;i++){

total=total+a[i];

System.out.println("Total = " +total);

p=(total/500)*100;

System.out.println("Percentage = " +p +"%");

if(p>=90){

System.out.println("A grade \n");

} else if(p>=80 && p<=89){


System.out.println("B grade \n");

} else if(p>=70 && p<80){

System.out.println("C grade \n");

} else if(p>=60 && p<70){

System.out.println("D grade \n");

} else if(p>=50 && p<60){

System.out.println("E grade \n");

}else{

System.out.println("Failed\n");

class mark{

public static void main(String args[])throws IOException{

sub s=new sub();

s.enter();

s.avg();

Output
20. SUM OF ALL INTEGERS

AIM: Write a program to find sum of all integers greater than 100 and less than 200 that
are divisible by n

Program code

import java.io.*;

import java.util.*;

class sumint{

Scanner obj=new Scanner(System.in);

int n,sum=0;

void read(){

System.out.println("Enter the number");

n=obj.nextInt();

for(int i=100;i<200;i++){

if(i%n==0){

sum=sum+i;}}

System.out.println("Sum ="+sum);}

class suminteger100to200{

public static void main(String args[])throws IOException{

sumint obj1=new sumint();

obj1.read();}

Output

You might also like