Sie sind auf Seite 1von 98

PROGRAM 1: Write a program in Java to input a string and reverse the original

string by
using recursive function. Display the original string and reversed string.

//program to enter a string and reverse it using recursion


import java.util.Scanner;
public class reverse
{
public static String reverseString(String str)
{
if (str.isEmpty())
return str;
//Calling Function Recursively
return reverseString(str.substring(1)) + str.charAt(0);
}
public static void main(String[] args) {
String str;
System.out.println("Enter a string ");
Scanner scanner = new Scanner(System.in);
str = scanner.nextLine();
scanner.close();
String reversed = reverseString(str);
System.out.println("The original string is: " + str);
System.out.println("The reversed string is: " + reversed);
}
}

VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1. str string To input a word and reverse it

OUTPUT
PROGRAM 2 : Write a program in java to perform binary search on the given array
using recursive technique.

import java.util.*;
import java.io.*;
class recursion
{
static void binarySearch(int arr[], int first, int last, int key)
{
int mid = (first + last)/2;
while( first <= last )
{
if ( arr[mid] < key )
{
first = mid + 1;
}
else if ( arr[mid] == key )
{
System.out.println("Element is found at index: " + mid);
break;
}
else
{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the no. of elements");
int n=sc.nextInt();
int arr[] =new int[n];
for(int i=0;i<n;i++)
{
System.out.println("enter array element");
arr[i]=sc.nextInt();
}
System.out.println("enter a the no. to be searched");
int key = sc.nextInt();
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}

VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1. N int To store number of elements in array
2. I int Loop control variable
3. Key int Enter number to be searched
4. Last int Store length of array
5. Mid int Find middle location

OUTPUT
PROGRAM 3: Write a program to determine how many Kaprekar numbers are
there in the range between 'p' and 'q' (p and q are <1000) and output them also
print the numbers.
Example -
i) 45² =2025, Left part=20 and right part =25, sum of both parts =20+25 = 45
ii) 297²=88209 Ieft part-88 and right part=209,sum of both parts = 88+209=297

//to determine the frequency of kaprekar numbers between p and q


import java.util.*;
public class Kaprekar
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the lower limit:");
int p=sc.nextInt();
System.out.print("Enter the upper limit:");
int q=sc.nextInt();
if(q<p)
System.out.println("INVALID INPUT");
else
{
int count=0;
System.out.println("THE KAPREKAR NUMBERS ARE:-");
for(int i=p;i<=q;i++)
{
String s=""+i;
int d=s.length();
int sq=i*i;
int rd=(int)(sq%(Math.pow(10,d)));
int ld=(int)(sq/(Math.pow(10,d)));
if(i==(rd+ld))
{
if(count==0)
System.out.print(i);
else
System.out.print(","+i);
count++;
}
}
System.out.println();
System.out.println("FREQUENCY OF KAPREKAR NUMBERS IS:"+count);
}
}
}

VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 P int Enter lower limit
2 Q int Enter upper limit
3 Count int To keep count of numbers
4 I int Loop control variable
5 S string New string
6 D int Store length of string
7 Sq int Square of i
8 Rd int Store remainder
9 Ld int Store divided value

OUTPUT
PROGRAM 4: write a program in java to sort an array using insertion sort technique
// program to sort an array
import java.util.*;
public class InsertionSort
{
static void insertionSort(int array[])
{
int n = array.length;
for (int j = 1; j < n; j++)
{
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) )
{
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
public static void main(String a[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the no. of elements");
int n=sc.nextInt();
int arr[] =new int[n];
for(int i=0;i<n;i++)
{
System.out.println("enter array element");
arr[i]=sc.nextInt();
}
System.out.println("Before Insertion Sort");
for(int i:arr){
System.out.print(i+" ");
}
System.out.println();
insertionSort(arr);
System.out.println("After Insertion Sort");
for(int i:arr){
System.out.print(i+" ");
}
}
}
VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 n int Store array length
2 key int Store array elements
3 i int Loop control variable
4 j int Loop control variable

OUTPUT
PROGRAM 5: Write a program to input an integer number and check the given
number is a Magic Number or not. A number is a magic number, if sum of all digits
of the number is equal to 1 and the number itself is a single digit number.( i.e. keep
adding the digits of the number itself till it becomes a single digit numbers) if the
sum is one the number is a magic number otherwise number is not a magic
number.
Example: 55=3+5 -10,10 - 1+0-1 Hence, 55 is a magic number

// program to check magic number


import java.util.*;
public class MagicNumber
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to be checked.");
int n=sc.nextInt();
int sum=0,num=n;
while(num>9)
{
sum=num;int s=0;
while(sum!=0)
{
s=s+(sum%10);
sum=sum/10;
}
num=s;
}
if(num==1)
{
System.out.println(n+" is a Magic Number.");
}
else
{
System.out.println(n+" is not a Magic Number.");
}
}
}
VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 N int Store number
2 Sum int To store sum
3 Num int To store number
4 S int To store
remainder

OUTPUT
PROGRAM 6: Write a program in java to find the difference between two matrices
of the array objects passed as parameter and store the difference in the current
object (using void calDiff( Array Ob1, Array Ob2) function and display the sum
using void Display() function of a class)

//program to find difference between two matrices


import java.util.*;
class difference
{
int m,n,r,c;
int arr[][];
difference()
{
m=2;
n=2;
r=c=0;
arr=new int[5][5];
}
void fillmat()
{
Scanner sc= new Scanner(System.in);
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
{
System.out.println("enter array elements");
arr[r][c]=sc.nextInt();
}
}
sc.close();
}
void dispmat()
{
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
{
System.out.print(arr[r][c]+" ");
}
System.out.println();
}
}
void diff( difference A,difference B)
{
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
{
if(A.arr[r][c]>B.arr[r][c])
this.arr[r][c]=A.arr[r][c]-B.arr[r][c];
else
this.arr[r][c]=B.arr[r][c]-A.arr[r][c];
}
}
}
public static void main(String args[])
{
difference ob1=new difference();
difference ob2=new difference();
difference ob3=new difference();
ob1.fillmat();
ob2.fillmat();
ob1.dispmat();
ob2.dispmat();
ob3.diff(ob1,ob2);
ob3.dispmat();
}
}
VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 m int Initialize to 2
2 n int Initialize to 2
3 r int Initialize to 0
4 c int Initialize to 0

PROGRAM 7: A program to check whether a number entered is a happy number or


not.A happy number is a number in which the eventual sum of the square of the
digits of the number is equal.
Example:
28=2²+8²=4+64=68
68=6²+8²=36 +64 =100
100=1²+0²+ 0²=1+0 +0=1
Hence, 28 is a happy number.

//program to check a number is happy number or not


import java.util.*;
class happy_number
{
static int sum(int n)
{
int s,i,d;
s=0;
for(i=n;i!=0;i=i/10)
{
d=i%10;
s=s+d*d;
}
return s;
}
public static void main(String args[])
{
Scanner sn=new Scanner(System.in);
int num,s;
System.out.println("enter a number");
num=sn.nextInt();
s=num;
while(s>9)
{
s=sum(s);
}
if(s==1)
System.out.println(num+" is happy number");
else
System.out.println(num+" is not happy number");
}
}

VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 s Int To calculate new number
2 i Int Loop control variable
3 d Int To store remainder
4 num Int To input number
5 s Int To make copy of number
PROGRAM 8: write a program in java to find the transpose of the matrix.

import java.util.*;
class Transpose
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int matrix[][] = new int[m][n];
System.out.println("Enter the elements of matrix");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
matrix[c][d] = in.nextInt();
int transpose[][] = new int[n][m];
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
transpose[d][c] = matrix[c][d];
System.out.println("Transpose of the matrix:");
for (c = 0; c < n; c++)
{
for (d = 0; d < m; d++)
System.out.print(transpose[c][d]+"\t");
System.out.print("\n");
}
}
}
VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 m Int store number of rows
2 n Int store number of columns
3 c Int Loop control variable
4 d Int Loop control variable

OUTPUT
PROGRAM 9: Write a program which inputs positive natural number N and Prints
the possible consecutive number combinations, which when added give N.
A positive natural number(for eg 27) can represented as follows:
2+3+4+5+6+7=27
8+9+10=27
13+14=27

//program to print the possible consecutive number combinations


import java.util.*;
class num_combination
{
public static void main(String args[])
{
Scanner sc= new Scanner (System.in);
System.out.print("Enter a number : ");
int n=sc.nextInt();
int sum=0,c=0,j=0;
for(int i=1;i<n;i++)
{
sum=i;
j=i+1;

while(sum<n)
{
sum=sum+j;
j++;
}
if(sum==n)
{
for(int k=i;k<j;k++)
{
if(k==i)
System.out.print(k);
else
System.out.print(" + "+k);
}
System.out.println();
}
}
}
}
VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 n int store number
2 c int count
3 sum int Store sum of i and j
4 i int Loop control variable
5 j int Store value of next number to i
6 k int To print number combinations

OUTPUT
PROGRAM 10: Write a program in java to merge the current object array elements
with parameterized element

import java.util.*;
class Mixer
{
int arr[ ];
int n;
static Scanner sc=new Scanner(System.in);
Mixer(int nn)
{ n=nn;
arr=new int[n];
}
void accept( )
{ System.out.println("Enter "+ n+ " elements in ascending order");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
}
Mixer mix( Mixer A)
{ Mixer B=new Mixer(n+A.n);
int x=0;
for(int i=0 ;i<n ;i++)
B.arr[x++]=arr[i];
for(int j =0 ;j<A.n ;j++)
B.arr[x++]=A.arr[j] ;
return B ;
}
void display()
{
for(int i=0;i<n;i++)
System.out.println(arr[i]);
}
public static void main(String args[])
{
System.out.println("enter the length of first array");
int a=sc.nextInt();
System.out.println("enter the length of second array");
int b=sc.nextInt();
Mixer P=new Mixer(a);
Mixer Q=new Mixer(b);
Mixer R=new Mixer(a+b);
P.accept();
Q.accept();
R=P.mix(Q);
R.display();
}
}

VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 n int store length of array
2 a int Store length of first array
3 b int Store length of second array
4 i int Loop control variable
5 x int To increment index position by 1 while
mixing the two arrays
OUTPUT
PROGRAM 11: Write a program to enter a string and perform the following:
(a) display the count of palindrome words in the sentence.
(b)Display the palindrome words in the sentence.

//program to check a number to be a palindrome number


import java.util.*;
class Palindrome
{
boolean IsPalindrome(String s)
{
int l=s.length();
String rev="";
for(int i=l-1; i>=0; i--)
{
rev=rev+s.charAt(i);
}
if(rev.equals(s))
return true;
else
return false;
}
public static void main(String args[])
{
Palindrome ob=new Palindrome();
Scanner sc=new Scanner(System.in);
System.out.print("Enter the sentence : ");
String s=sc.nextLine();
s=s.toUpperCase();

StringTokenizer str = new StringTokenizer(s,".?! ");


int w=str.countTokens();

String word[]=new String[w];


for(int i=0;i<w;i++)
word[i]=str.nextToken();
int count=0;
System.out.print("OUTPUT : ");
for(int i=0; i<w; i++)
{
if(ob.IsPalindrome(word[i])==true)
{
count++;
System.out.print(word[i]+" ");
}
}

// To show the palindrome or not.


if(count==0)
System.out.println("No Palindrome Words");
else
System.out.println("\nNumber of Palindromic Words : "+count);
}
}

VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 L int store length of string
2 Rev string Store reversed string
3 S string Input a sentence In upper case
4 I int Loop control variable
5 Count int To count palindrome words in sentence

OUTPUT
PROGRAM 12: Write a program to perform insertion and deletion in a stack as an
array.

//program on stacking technique


import java.util.*;
class stack
{
int arr[];
int top;
stack()
{
arr=new int[10];
top=-1;
}
void addval(int num)
{
if(top<9)
{
top++;
arr[top]=num;
}
else
System.out.println("over flow");
}
void delval()
{
int num;
if(top==-1)
System.out.println("under flow");
else
{
num=arr[top];
top--;
System.out.println(num);
}
}
void disp()
{
for(int i=top;i>=0;i--)
System.out.println(arr[i]);
}
public static void main(String Args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("enter 1 to add value, enter 2 to delete value”);
int c=sc.nextInt();
stack ob1= new stack();
switchI
{ case 1:
System.out.println(“\nter the no. to be entered”);
int a=sc.nextInt();
ob1.addval(a);
ob1.disp();
break;
case 2:ob1.delval();
ob1.disp();
break;
default: System.out.println(“wrong choice”);
}
}
}

VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 Top int Stack pointer
2 I int Loop control variable
3 C int To enter 1 to add value and 2 to delete value

4 Num int Number to enter in a stack


PROGRAM 13: Write a program to input a paragraph and count the number of
words and sentences present in the given paragraph and to display the count.
Sentence may be terminated by full stop or a question mark and each word is
separated by a single space

// program to count total number of sentences and words


import java.util.*;
class words
{
static int sentence(String in)
{
int c=0;
StringTokenizer s= new StringTokenizer( in, "." );
System.out.println("sentence are:");
while(s.hasMoreTokens())
{
System.out.println(s.nextToken());
c++;
}
return c;
}
static int word(String in)
{
int wc=0;
StringTokenizer w= new StringTokenizer( in );
System.out.println("words are:");
while(w.hasMoreTokens())
{
System.out.println(w.nextToken());
wc++;
}
return wc;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a paragraph");
String a=sc.nextLine();
int x=sentence(a);
int y=word(a);
System.out.println("total no. of sentences="+x);
System.out.println("total no. of words="+y);
}
}
VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 c int To count sentence
2 wc int To count words
3 x int Display total number of sentences
4 y int Display total number of words

OUTPUT
PROGRAM 14: write a program to perform insertion and deletion in a queue as an
array.

//program to enter and delete elements from a queue


import java.util.*;
class queue
{
int arr[]; int rear; int front; int size;
queue(int ss)
{
size=ss;
arr=new int[size];
front=rear-1;
}
void insertnum(int num)
{
if(rear==-1)
{
front=rear=0;
arr[rear]=num;
}
else if(rear<size-1)
{
rear++;
arr[rear]=num;
}
else
System.out.println("queue overflow");
}
void delnum()
{
int num;
if(front==-1)
System.out.println("queue underflow");
else
{
num=arr[front];
System.out.println(num);
if(front==rear)
front=rear=-1;
else
front++;
}
}
void disp()
{
for(int i=0;i<size;i++)
System.out.println(arr[i]);
}
public static void main(String Args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the size of the array");
int s=sc.nextInt();
System.out.println("enter 1 to add value, enter 2 to delete value");
int c=sc.nextInt();
queue ob1= new queue(s);
switch(c)
{ case 1:
System.out.println("enter the no. to be entered");
int a=sc.nextInt();
ob1.insertnum(a);
break;
case 2:ob1.delnum();
break;
default: System.out.println("wrong choice");
}
ob1.disp();

}
}

VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 Rear int To enter elements
2 front int To delete elements
3 Size int To initialize size of array
4 i int Loop control variable
5 num int Number to be entered
6 c int Choice 1 to enter elements and 2 to delete elements
PROGRAM 15: write a program to convert word to piglatin form.

//program to convert a word to piglatin


import java.util.*;
public class PiglatinWord
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the word to be converted.");
String word=ob.next();
word=word.toUpperCase();
String piglatin="";
int flag=0;
for(int i=0;i<word.length();i++)
{
char x=word.charAt(i);
if(x=='A' || x=='E' || x=='I' || x=='O' ||x=='U')
{
piglatin=word.substring(i)+word.substring(0,i)+"AY";
flag=1;
break;
}
}
if(flag==0)
{
piglatin=word+"AY";
}
System.out.println(word+" in Piglatin format is "+piglatin);
}
}
VARIABLE DESCRIPTION

SNO NAME TYPE USE


.
1 Flag int store length of string
2 Word strin Enter word to be converted
g
3 Piglati strin Store converted word
n g
4 I int Loop control variable
5 X char To extract character from loop variable

OUTPUT
PROGRAM 16: Write a program to input any string find the shortest and the
longest word present in a sentence and print them along with their length.

//program to find smallest and largest word in a sentence


import java.util.*;
class SmallestLargestWord
{
public static void main(String[] args)
{ Scanner sc= new Scanner(System.in);
System.out.println("enter a sentence");
String =sc.nextLine();
String word = "", small = "", large="";
String[] words = new String[100];
int length = 0;
string = string + " ";
for(int i = 0; i < string.length(); i++)
{
if(string.charAt(i) != ' ')
{
word = word + string.charAt(i);
}
else
{
words[length] = word;
length++;
word = "";
}
}
small = large = words[0];
for(int k = 0; k < length; k++){
if(small.length() > words[k].length())
small = words[k];
if(large.length() < words[k].length())
large = words[k];
}
System.out.println("Smallest word: " + small);
System.out.println("Largest word: " + large);
}
}
VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 length int store length of word
2 string string Enter a sentence
3 word string To extract a word from sentence
4 small string To store smallest word
5 large string To store largest word
6 k int Loop control variable

OUTPUT
PROGRAM 17: Write a program to input a sentence and interchange the first and
the last alphabet of each word of the given string and print both strings

//program to swap first and last characters of a word in a sentence


import java.util.*;
class SwapFirstLastCharacters
{
static String count(String str)
{
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++)
{
int k = i;
while (i < ch.length && ch[i] != ' ')
i++;
char temp = ch[k];
ch[k] = ch[i - 1];
ch[i - 1] = temp;
}
return new String(ch);
}
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("enter a sentence");
String str = sc.nextLine();
System.out.println("original string:"+str);
System.out.println("changed string:"+count(str));
}
}

VARIABLE DESCRIPTION
SNO NAME TYPE USE
.
1 str strin Enter a sentence
g
2 k int To make a copy of loop control positioned word
3 i int Loop control variable

OUTPUT
PROGRAM 18: Write a program to input a number.Count and print the frequency
of each digit present in that number.

// program to count frequency of digits in a number


import java.util.*;
class frequency
{
static int fre(int n,int d)
{
int c = 0;
while (n > 0)
{
if (n % 10 == d)
c++;
n = n / 10;
}
return c;
}
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
int n = sc.nextInt();
for(int i=0;i<=9;i++)
{
int D = i;
if(fre(n,D)!=0)
System.out.println(i+"\t:\t"+fre(n, D));
}
}
}
VARIABLE DESCRIPTION
SNO NAM TYPE USE
. E
1 c Int To count frequency
2 n Int To enter number
3 i Int Loop control variable
4 D Int To make copy of number
OUTPUT
PROGRAM 19: program to count number of words in a sentence

// program to count number of words in a sentence


import java.util.Scanner;
class ToCountNumberOfWords
{
public static void main(String[] args)
{ int word=0; int sentence=0;
Scanner Kb=new Scanner(System.in);
System.out.println("Enter a sentence!");
while(Kb.hasNext())
{
String line=Kb.nextLine();
String[] arr=line.split(" ");
for(int i=0;i<arr.length;i++)

word++;
}
while(Kb.hasNext())
{
String line=Kb.nextLine();
String[] arr=line.split(".");
for(int i=0;i<arr.length;i++)
sentence++;
}
System.out.println(word);
System.out.println(sentence);
}
}
VARIABLE DESCRIPTION
SNO NAME TYPE USE
.
1 Word int To count number of words
2 sentenc int To count number of sentence
e
3 I int Loop control variable

OUTPUT
PROGRAM 20: program to count words in a paragraph

// program to count words in a paragraph


import java.util.*;
class count_words
{
static int Sentence(String input)
{
int len=0;
if(input.trim().length()==0)
{
len=0;
}
else
{
int count=0;
for(int i=0;i<input.length();i++)
{
char ch=input.charAt(i);
if(ch=='.')
count++;
len=count;
}
}
return len;
}
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
System.out.println("enter a paragraph");
System.out.println(Sentence(a));
}
}

VARIABLE DESCRIPTION
SNO NAM TYP USE
. E E
1 Len int To trim length of para
2 Ch char To find delimeter (.)
3 Count int To count words
4 I int Loop control variable

PROGRAM 21: Write a program to print the address(row index and column index
of the largest element of the matrix of M Rows and N columns

//program to find the largest and second largest number in the array
import java.util.*;
class secondlargest
{
public static void main (String args[])
{
int i,j,a=0,b=0,c=0,d=0,x=0,y=0,l1=0,l2=0;
int arr[][]=new int[3][3];
Scanner sc= new Scanner(System.in);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.println("enter array elements");
arr[i][j]=sc.nextInt();
}
}
x=arr[0][0];
for(i=1;i<3;i++)
{
for(j=1;j<3;j++)
{
if(x>arr[i][j])
{
l1=arr[i][j];
a=i; b=j;
}
else
{
l1=arr[0][0];
a=0; b=0;
}
}
}
y=arr[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(y<x&&y>arr[i][j])
{
l2=y; c=I; d=j;
}
}
}
System.out.println("largest element:"+l1);
}
}

VARIABLE DESCRIPTION

SNO. NAME TYPE USE


1 a Int To store locations of i
2 b Int To store locations of j
3 c int To store locations of i
4 d int To store locations of j
5 x int Stores array at[0][0]
6 y int Store array
7 l1 Int Stores different array elements
8 l2 int Stores different array elements
9 i Int Loop control variable
10 j Int Loop control variable

OUTPUT
PROGRAM 22: program to create pascal’s triangle

import java.io.*;
class Pascal
{
public voidpascalw()throwsIOException //pascalw() function
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a no.”);
intn=Integer.parseInt(br.readLine()); //accepting value int [ ] pas = new int[n+1];
pas[0] = 1;
for (int i=0;i<n;i++) //loop evaluating theelements
{for (int j=0; j<=i;++j)
System.out.print(pas[j]+""); //printing the Pascal Triangle elements
System.out.println();
for (int j=i+1; j>0; j--) pas[j]=pas[j]+pas[j-1];
}}}

Variable description

No. Name Type Description


1 br BufferedRead BufferedReader object
er
2 n int Input value
3 pas int[] Matrix storing pascal numbers
4 i int Loop variable
5 j int Loop variable
output

PROGRAM 23 to display entered number in words

import java.io.*; class Num2Words


{public static void main(Stringargs[])throwsIOException //mainfunction
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any Number(less than 99)");
intamt=Integer.parseInt(br.readLine()); //accepting number intz,g;
String
x[]={“”,"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen
","Eighteen","Nineteen"};
String x1[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
String
x2[]={"","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
z=amt%10; //finding the number inwords
g=amt/10; if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]); else System.out.println(x[amt-9]);
}}
variabledescription

No. Name Type Method Description


1 br BufferedRead main() BufferedReader object
er
2 z int main() amt%10
3 g int main() amt/10
4 x String[] main() String array storing no.in words
5 x1 String[] main() String array storing no.in words
6 x2 String[] main() String array storing no.in words
7 amt int main() input number
output
program 24 to print an AP series
class APSeries
{private double a,d;
APSeries() //defaultconstructor
{a = d = 0;
}
APSeries(doublea,doubled) //parameterizedconstructor
{this.a = a; this.d = d;
}
doublenTHTerm(intn) //final APterm
{return (a+(n-1)*d);
}
doubleSum(int n) //function calculatingsum
{return (n*(a+nTHTerm(n))/2);
}
voidshowSeries(intn) //displaying APSeries
{System.out.print("\n\tSeries\n\t"); for(int i=1;i<=n;i++)
{System.out.print(nTHTerm(i)+" ");
}
System.out.print("\n\tSum :"+Sum(n));
}
}
voidmain()throwsIOException //mainfunction
{BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 1st term");
a=Integer.parseInt(br.readLine()); //accepting 1stterm
System.out.println("Enter Common difference"); d=Integer.parseInt(br.readLine());
//accepting common difference System.out.println("Enter no.ofterms");
intn=Integer.parseInt(br.readLine()); //accepting no. of terms nTHTerm(n);
Sum(n); showSeries(n);
}
variable descriptionoutput

No. Name Type Method Description


1 a int - 1stterm
2 d int - common difference
3 n int Sum(), showSeries(), nTHTerm() total terms
4 i int showSeries() loop variable
PROGRAM 25 to display calender of any month of any year
import java.io.*;
class Calendar
{public voiddee()throwsIOException //dee() function
{int i,count=0,b,d=1;
BufferedReader br=new BufferedReader(newInputStreamReader(System.in));
System.out.println(“Entermonth”); //accepting month and year
intmonth=Integer.parseInt(br.readLine());
System.out.println(“Enter Year”);
int year=Integer.parseInt(br.readLine());
/* Computing and displaying calendar*/ String w="SMTWTFS";
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
String
month1[]={"January","February","March","April","May","June","July","August","Sep
tember","October","November","December"};
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0)) days[1]=29;
System.out.println("================The Calendar of"+month1[month-1]+"
"+year+"is==================");
for(i=0;i<w.length();i++) System.out.print(w.charAt(i)+"\t"); System.out.println(" ");
for(i=1;i<year;i++)
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0)) count+=2;
else count+=1; for(i=0;i<month;i++) count+=days[i]; count+=1; count%=7;
b=7-count; if(b!=1 || b!=7) while(count>0)
{System.out.print(' '+"\t"); count--;
}
for(i=1;i<7;i++)
{while(b>0 && d<=days[month-1])
{System.out.print(d+"\t"); d++;
b--;
} b=7;
System.out.println(" ");
}}}
variabledescription

No. Name Type Method Description


1 br BufferedRead dee() BufferedReader object
er
2 i int dee() loop variable
3 count int dee() counter
4 b int dee() week counter
5 d int dee() day counter
6 month int dee() input month
7 year int dee() input year
8 w String dee() week days
9 days String[] dee() array storing days
10 month1 String[] dee() array storing months
output
Program 26 to calculate factorial using recursion
import java.io.*; class Factorial
{public static void main(String args[])throwsIOException //mainfunction
{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no =");
int n=Integer.parseInt(br.readLine()); //accepting no. Factorial obj = newFactorial();
long f = obj.fact(n);
System.out.println("Factorial="+f); //displaying factorial
}
public longfact(intn) //recursivefact()
{if(n<2) return 1;
else return (n*fact(n-1));
}}
variabledescription

No. Name Type Method Description


1 br BufferedRead main() BufferedReader object
er
2 n int main() input number
3 obj Factorial main() Factorial object
4 f long main() variable storing factorial
5 n int fact() parameter in recursive function fact()
output
Program 27 to display Fibonacci series
import java.io.*; class Fibonacci
{public static void main(String args[])throwsIOException //mainfunction
{Fibonacci obj = new Fibonacci();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no ofterm="); //accepting no. of terms int n
=Integer.parseInt(br.readLine());
System.out.println();
for(inti=1;i<=n;i++) //Fibonacci element displayloop
{int f = obj.fib(i); System.out.print(f+" ");
}}
public intfib(int n) //Recursive function fib() for calculation of Fibonaccielement
{if(n<=1) return n; else
return (fib(n-1) +fib(n-2));
}}
variabledescription

No. Name Type Method Description


1 br BufferedRead main() BufferedReader object
er
2 obj Fibonacci main() Fibonacci object
3 n int main() input number
4 i int main() loop variable for Fibonacci element
5 f int main() Fibonacci element
6 n int fib() recursive function fib() parameter
output
program 28 to calculate GCD using recursion
import java.io.*; class GCD
{public static void main(String args[])throwsIOException //mainfunction
{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the numbers =");
int p=Integer.parseInt(br.readLine()); //accepting nos. int q
=Integer.parseInt(br.readLine());
GCD obj = new GCD(); int g = obj.calc(p,q);
System.out.println("GCD ="+g);
}
public int calc(intp,intq) //recursive function calculatingGCD
{if(q==0) return p;
else return calc(q,p%q);
}}
variabledescription

No. Name Type Method Description


1 br BufferedRead main() BufferedReader object
er
2 p int main() input number
,calc()
3 q int main() input number
,calc()
4 obj GCD main() GCD object
5 g int main() variable storing the GCD
output
Program 29 to display spiral matrix
import java.io.*; class SpiralMatrix
{public static void main(String[] args)throwsIOException //mainfunction
{int a[][],r,c,k1=2,k2=3,p=0,co=0,re=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the dimension of matrix A x A =");
int l=Integer.parseInt(br.readLine()); //accepting dimension of square spiral matrix
a=newint[l][l];
r=l/2;c=r-1; if(l%2==0)
{System.out.println("wrong entry for spiral path"); System.exit(0);
}
/*Calculating and displaying spiral matrix*/ while(p!=(int)Math.pow(l,2))
{if(co!=0) re=1;
for(int ri=1;ri<=k1-re;ri++)
{p++;c++;if(c==l)break;a[r][c]=p;} if(c==l)break;
for(int dw=1;dw<=k1-1;dw++)
{p++;r++;a[r][c]=p;} for(intle=1;le<=k2-1;le++)
{p++;c--;a[r][c]=p;}
for(int up=1;up<=k2-1;up++)
{p++;r--;a[r][c]=p;} k1=k1+2; k2=k2+2;
co++;
}
for(inty=0;y<l;y++) //Displayingmatrix
{for(int yy=0;yy<l;yy++) System.out.print("\t"+a[y][yy]); System.out.println();
System.out.println();
}}}
variabledescription

No. Name Type Method Description


1 br BufferedRead main() BufferedReader object
er
2 a int[][] main() spiral matrix
3 r int main() l/2
4 c int main() r-1
5 k1 int main() stores 2
6 k2 int main() stores 3
7 p int main() loop gate
8 co int main() coloumn index
9 re int main() row index
10 l int main() dimensions of thr matrix
11 ri int main() right side matrix loop variable
12 le int main() left side matrix loop variable
13 dw int main() down side matrix loop variable
14 up int main() up side matrix loop variable
15 y int main() loop variable to print matrix
16 yy int main() loop variable to print matrix
output
program 30 to display magic square
/*A Magic Square is a square whose sum of diagonal elements, row elements and
coloumn
elements is the same*/ import java.io.*;
class MagicSquare
{public static void main(Stringargs[])throwsException //mainfunction
{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the dimension of magical square=");
int n=Integer.parseInt(br.readLine()); //accepting dimensions int arr[][]=newint[n]
[n],c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++) //loop for finding magic squareelements
{r--; c++;
if(r==-1) r=n-1; if(c>n-1) c=0;
if(arr[r][c]!=0)
{r=r+2; c--;
}
arr[r][c]=num; if(r==0&&c==0)
{r=n-1; c=1;
arr[r][c]=++num;
}
if(c==n-1&&r==0) arr[++r][c]=++num;
}
System.out.println();
for(r=0;r<n;r++) //loop displaying magicsquare
{for(c=0;c<n;c++) System.out.print(arr[r][c]+" "); System.out.println();
}}}
variabledescription

No. Name Type Method Description


1 br BufferedRead main() BufferedReader object
er
2 n int main() input dimensions
3 arr int[][] main() magic square matrix
4 num int main() loop variable for magic square
5 r int main() row
6 c int main() coloumn
output
program 31 to search an array using linear search
import java.io.*; class LinearSearch
{int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public LinearSearch(int nn)
{n=nn;
}
public void input()throws IOException //function for obtaining values fromuser
{System.out.println("enter elements"); for(i=0;i<n;i++)
{a[i] = Integer.parseInt(br.readLine());
}}
publicvoiddisplay() //function displaying arrayvalues
{System.out.println(); for(i=0;i<n;i++)
{System.out.print(a[i]+" ");
}}
public voidsearch(intv) //linear searchfunction
{int flag=-1;
for(int i=0; i<n ; i++)
{if(a[i] == v) flag =i;
}
if(flag== -1 ) System.out.println("not found");
else System.out.println(v+" found at position - "+flag);
}
public static void main(String args[])throwsIOException //mainfunction
{LinearSearch obj = new LinearSearch(10); obj.input();
obj.display();
System.out.println("enter no. to besearched-"); //accepting the values to be
searched int v = Integer.parseInt(br.readLine());
obj.search(v);
}}
variabledescription

No. Name Type Method Description


1 br BufferedRead - BufferedReader object
er
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int LinearSearch() parameter in constructor
6 v int search(), search element
main()
7 flag int search() flag
8 obj LinearSearch main() LinearSearch object
output
Program 32 to sort an array using bubble sort
import java.io.*; class BubbleSort
{int n,i;
int a[] = new int[100];
publicBubbleSort(intnn) //parameterizedconstructor
{n=nn;
}
public void input()throwsIOException //function accepting arrayelements
{BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter elements");
for(i=0;i<n;i++)
{a[i] = Integer.parseInt(br.readLine());
}}
publicvoiddisplay() //function displaying array elements
{System.out.println(); for(i=0;i<n;i++)
{System.out.print(a[i]+" ");
}}
publicvoidsort() //function sorting array elements using Bubble Sorttechnique
{int j,temp;
for(i=0 ; i<n-1 ; i++)
{for(j=0 ; j<n-1-i ; j++)
{if(a[j] > a[j+1])
{temp = a[j];
a[j] =a[j+1]; a[j+1] = temp;
}}}}
public static void main(String args[])throwsIOException //mainfunction
{BubbleSort x = new BubbleSort(5); x.input();
System.out.print("Before sorting - "); x.display();
System.out.print("After sorting - "); x.sort();
x.display();}}
variabledescription

No. Name Type Method Description


1 br BufferedRead input BufferedReader object
er
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int BubbleSort parameter in constructor
()
6 j int sort() sort index
7 temp int sort() temporary storage
8 x SelectionSort main() SelectionSort object
output
program 33 to convert a number into its binary equivalent
import java.io.*; class Dec2Bin
{int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
publicDec2Bin(intnn) //parameterizedcontructor
{n=nn;
}
public voiddectobin(intno) //function converting decimalto binarynumber
{int c = 0;
int temp = no; while(temp !=0)
{a[c++] = temp % 2; temp = temp /2;
}
System.out.println("Binary eq. of "+no+" = ");
for( i = c-1 ; i>=0; i--) //Displaying binary number System.out.print( a[ i ]);
}
public static void main(String args[])throwsIOException //mainfunction
{Dec2Bin obj = new Dec2Bin(30); System.out.println("enter decimal no -"); int no =
Integer.parseInt(br.readLine()); obj.dectobin(no);
}}

variabledescription

No. Name Type Method Description


1 br BufferedRead BufferedReader object
er
2 n int - array length
3 i int - loop variable
4 a[] int[] - array storing binary no.
5 nn int Dec2Bin() parameter in constructor
6 no int main(), input number
dectobin()
7 temp int dectobin() temporary storage
8 c int dectobin() counter
9 obj Dec2Bin main() Dec2Bin object
output
program 34 to display date from a given number
import java.io.*; class Day2Date
{static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public void calc(int n,intyr) //function to calculatedate
{int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ;
String m[ ] = { "Jan", "Feb",
"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" } ; if ( yr % 4 == 0)
a[1] =29;
int t=0,s=0;
while( t <n) //loop calculatingdate
{t =t + a[s++];
}
int d = n + a[--s] - t;
if( d == 1|| d == 21 || d == 31 )
{System.out.println( d + "st" + m[s] + " , "+yr);
}
if( d == 2 || d == 22 )
{System.out.println( d + "nd" + m[s] + " , "+yr);
}
if( d == 3|| d == 23 )
{System.out.println( d + "rd" + m[s] + " , "+yr);
}
else {System.out.println( d + "th" + m[s] + " , "+yr);
}}
public static void main(String args[])throwsIOException //mainfunction
{Day2Date obj = new Day2Date();
System.out.println( "Enter day no="); //accepting day no. int n
=Integer.parseInt(br.readLine());
System.out.println( "Enter year="); //accepting year int yr =
Integer.parseInt(br.readLine());
obj.calc(n,yr);
}}
variabledescription

No. Name Type Method Description


1 br BufferedRead - BufferedReader object
er
2 n int calc(), Day number
main()
3 yr int calc(), year
main()
4 a int[] calc() array storing day
5 m int[] calc() array storing month
6 t int calc() array index
7 s int calc() array index
8 d int calc() n+a[--s]+t
9 obj Day2Date main() Day2Date object
output
program 35 to create star pattern from given string
import java.io.*; class Pattern
{public static void main (String args[]) throws IOException
{int i,sp,j,k,l;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter thestring="); //acceptingstring
String s = br.readLine(); l=s.length();
/*printing the pattern*/ for(i=0;i<l;i++)
if(i==l/2) System.out.println(s); else{sp=Math.abs((l/2)-i); for(j=sp;j<l/2;j++)
System.out.print(" "); k=0;
while(k<3)
{System.out.print(s.charAt(i)); for(j=0;j<sp-1;j++) System.out.print(" ");
k++;
}
System.out.println(" ");
}}}
variabledescription

No. Name Type Method Description


1 Br BufferedRead main() BufferedReader object
er
2 S String main() input string
3 I int main() loop variable for printing the pattern
4 Sp int main() Math.abs((l/2)-i)
5 J int main() loop variable for printing the pattern
6 K int main() loop variable for printing the pattern
7 L int main() length of string
output
program 36 to find a word in entered string
import java.util.StringTokenizer;
import java.io.*;
public class WordSearch
{public static void main(String[] args)throwsIOException //mainfunction
{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string=");
String s=br.readLine(); //accepting string
StringTokenizer st = newStringTokenizer(s,""); //StringTokenizerinitialization
System.out.println("enter the word to be searched=");
String look = br.readLine(); int flag = -1;
while(st.hasMoreElements()) //searching forword
{if(look.equals(st.nextElement())) flag =1;
}
if(flag ==-1)
{System.out.println("the wordnotfound"); //displaying theresult
}
else {
System.out.println("the word found");
}}}
variable descriptionoutput

No. Name Type Method Description


1 br BufferedReader main() BufferedReader object
2 s String main() input string
3 st StringTokenizer main() StringTokenizer object
4 look String main() word to be searched
5 flag int main() flag
program 37 to decode entered string
import java.io.*; class Decode
{public voidcompute()throwsIOException //compute() function
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter name:”);
String name=br.readLine(); System.out.println(“Enter number:”); int
n=Integer.parseInt(br.readLine()); int j,i,l,c=0,y,n1;
l=name.length();
System.out.println("original string is "+name); for(i=0;i<l;i++)
{char c1=name.charAt(i);
try //trying forNumberFormatException
{c=(int)c1 ;
}
catch(NumberFormatException e)
{}
if(n>0)
{if((c+n)<=90)
/*Decoding String*/ System.out.print((char)(c+n)); else {c=c+n;
c=c%10; c=65+(c-1);
System.out.print((char)(c));
}}
else if(n<0)
{n1=Math.abs(n);
if((c-n1) >=65)
System.out.print((char) (c-n1)); else {if(c>65)
c=c-65;
else c=n1; System.out.print((char)(90-(c-1)));
}}
else if (n==0)
{System.out.println("no change "+name); break;
}}}}
variabledescription

No. Name Type Method Description


1 br BufferedReader compute( BufferedReader object
)
2 name String compute( input string
)
3 n int compute( decode number
)
4 j int compute( loop variable
)
5 i int compute( loop variable
)
6 l int compute( length of string
)
7 c int compute( ASCII of c1
)
8 y int compute(
)
9 n1 int compute(
)
10 c1 char compute( character at index i
)
11 e NumberFormatExcep compute( NumberFOrmatException object
tion )
output
program 38 to display given string in alphabetical order
import java.io.*; class Alpha
{String str; int l;
char c[] = new char[100];
publicAlpha() //Alpha() constructor
{str = ""; l =0;
}
public void readword()throwsIOException //function to read inputstring
{System.out.println("enter word - ");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); str =
br.readLine();
l = str.length();
}
publicvoidarrange() //function to arrange string in ascendingorder
{int i,j; chartemp;
for(i=0;i<l;i++)
{c[i]= str.charAt(i);
}
for(i=0;i<l-1;i++) //loops for swapping ofcharacters
{for(j=0;j<l-1-i;j++)
{if(c[j] > c[j+1])
{temp = c[j];
c[j] = c[j+1]; c[j+1] = temp;
}}}}
publicvoiddisplay() //function to display the rearrangedstring
{System.out.println(); for(int i=0;i<l;i++)
{System.out.print(c[i]);
}}
public static void main(String args[])throwsIOException //mainfunction
{Alpha obj = new Alpha(); obj.readword(); obj.arrange(); obj.display();
}}
variabledescription

No. Name Type Method Description


1 br BufferedRead readword( BufferedReader object
er )
2 str String - input string
3 l int - length
4 c char[] - character array
5 i int readword( loop variable
)
6 j int readword( loop variable
)
7 temp char readword( temporary storage
)
8 obj Alpha main() Alpha object
output
program 39 to count and display number of vowels and consonents in a string
import java.io.*;
class Vowels
{public static void main(Stringargs[])throwsIOException //mainfunction
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string");
Stringa=br.readLine(); //Accepting string intz=a.length(),y,x=0,b=0;
for(y=0;y<z;y++) //loop for counting number ofvowels
{if(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.charAt(y)=='o'||
a.charAt(y)=='u') x++;
else b++;
}
System.out.println("Number of vowels instring="+x); //displaying result
System.out.println("Number of consonants in string="+b);
}}
Variable description

No. Name Type Method Description


1 br BufferedRead main() BufferedReader object
er
2 a String main() input string
3 z int main() length of string
4 y int main() loop variable
5 b int main() no. of consonants
6 x int main() no. of vowels
output
Program 40 to replace all vowels with * in a word
import java.io.*; class VowelReplace
{public static void main(Stringargs[])throwsIOException //mainfunction
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a String”);
StringBuffera=newStringBuffer(br.readLine()); //accepting a string
System.out.println("Original String-"+a);
int z=0;
for(z=0;z<a.length();z++) //loop for replacing vowels with"*"
{if(a.charAt(z)=='a'||a.charAt(z)=='e'||a.charAt(z)=='i'||a.charAt(z)=='o'||
a.charAt(z)=='u') a.setCharAt(z,'*');
}System.out.println("NewString-"+a); //displaying theresult
}}
variable descriptionoutput

No. Name Type Method Description


1 br BufferedReader main() BufferedReader object
2 a StringBuffer main() StringBuffer object of input string
3 z int main() loop variable
program 41 to display sum of array
import java.io.*; class MatrixSum
{public static void main(Stringargs[])throwsIOException //mainfunction
{ int a[][]=new int[5][5];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in)); int
x,y,z,Sum=0;
System.out.println("Enter the array"); for(x=0;x<5;x++) //loop for readingarray
{for(y=0;y<5;y++)
{z=Integer.parseInt(aa.readLine()); //accepting arrayelement
a[x][y]=z;
}}
System.out.println("Array -");
for(x=0;x<5;x++) //loop for printing array
{for(y=0;y<5;y++)
{System.out.print(a[x][y]+" ");
}
System.out.print("\n");
} //loop for printing sum of array elements
for(x=0;x<5;x++)
{for(y=0;y<5;y++)
{Sum=Sum+a[x][y];
}}

System.out.println("Sum of Array elements="+Sum); //displaying sum


}}
variabledescription

No. Name Type Method Description


1 aa BufferedRead main() BufferedReader object
er
2 a int[][] main() input array
3 x int main() loop variable
4 y int main() loop variable
5 z int main() input element
6 Sum main() main() Sum of all elements
output
Program 42 to display sum of each column of an array
import java.io.*; class ColoumnSum
{public static void main(Stringargs[])throwsIOException //mainfunction
{int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in)); int
x,y,z,Sum=0;
System.out.println("Enterthearray"); //reading array for(x=0;x<4;x++)
{for(y=0;y<4;y++)
{z=Integer.parseInt(aa.readLine()); a[x][y]=z;
}}
System.out.println("Array-"); //printing the array in matrix form for(x=0;x<4;x++)
{for(y=0;y<4;y++)
{System.out.print(a[x][y]+" ");
}System.out.print("\n");
}
for(y=0;y<4;y++)
{for(x=0;x<4;x++)
{Sum=Sum+a[x][y];
}
System.out.println("Sum of column "+(y+1)+" is "+Sum); //printing sum of
Sum=0; coloumn
}}}
variabledescription

No. Name Type Method Description


1 aa BufferedRead main() BufferedReader object
er
2 a int[][] main() input array
3 x int main() loop variable
4 y int main() loop variable
5 z int main() input element
6 Sum int main() Sum of each couloumn
output
program 43 to find sum of diagonals of a DD array
import java.io.*; class DiagonalSum
{public static void main(Stringargs[])throwsIOException //mainfunction
{int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in)); int
x,y,z,Sum=0;
System.out.println("Enter the array"); for(x=0;x<4;x++) //Readingarray
{for(y=0;y<4;y++)
{z=Integer.parseInt(aa.readLine()); a[x][y]=z;
}}
System.out.println("Array -");
for(x=0;x<4;x++) //displaying array
{for(y=0;y<4;y++)
{System.out.print(a[x][y]+" ");
}
System.out.print("\n");
} y=0;
for(x=0;x<4;x++)
{Sum=Sum+a[x][y]; y=y+1; //loop for finding sum of diagonal
}
System.out.println("Sum of diagonalis"+Sum); //displaying the sum of diagonal
Sum=0;
}}
variabledescription

No. Name Type Method Description


1 aa BufferedRead main() BufferedReader object
er
2 a int[][] main() input matrix
3 x int main() loop variable
4 y int main() loop variable
5 Sum int main() Sum of diagonals
6 z int main() input element
output
program 44 to convert decimal number to roman numeral
import java.io.*;
public class Dec2Roman
{public static void main()throwsIOException //mainfunction
{DataInputStream in=new DataInputStream(System.in); System.out.print("Enter
Number : ");
intnum=Integer.parseInt(in.readLine()); //accepting decimalnumber
Stringhund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
String ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
/*Displaying equivalent roman number*/
System.out.println("Roman Equivalent=
"+hund[num/100]+ten[(num/10)%10]+unit[num%10]);
}}
variabledescription

No. Name Type Method Description


1 in DataInputStre main() DataInputStream object
am
2 num int main() input number
3 hund String[] main() array storing 100thposition
4 ten String[] main() array storing 10thposition
5 unit String[] main() array storing units position
output
program 45 to convert Celsius into Fahrenheit
import java.io.*; class C2F
{ public static void main(Stringargs[])throwsIOException //mainfunction
{Temperature ob= new Temperature();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter temperatureinCelsius"); //accepting temperature double
temp=ob.convert(Double.parseDouble(br.readLine())); System.out.println("The
temperature in fahrenheit is ="+temp);
}}
class Temperature extends C2F
{doubleconvert(doublecelcius) //function to convert Celsius tofahrenheit
{double far=1.8*celcius+32.0; return far;
}}
variabledescription

No. Name Type Method Description


1 br BufferedRead main() BufferedReader object
er
2 ob C2F main() C2F object
3 temp double main() calculated Fahrenheit temperature
4 celcius double convert( input temperature in Celsius
)
5 far double convert( Calculated Fahrenheit temperature
)
output

Das könnte Ihnen auch gefallen