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

Progra - 1

Write a program to Calculate the Commission of a Salesman as


per the following data,
Sales Commission

>=100000 25% of sales

80000-99999 22.5% of sales

60000-79999 20% of sales

40000-59999 15% of sales

<40000 12.5% of sales

ALGORITHM:
STEP 1 - START
STEP 2 - INPUT sales
STEP 3 - IF (sales>=100000) THEN comm=0.25 *sales
OTHERWISE GOTO STEP 4
STEP 4 - IF (sales>=80000) THEN comm=0.225*sales OTHERWISE
GOTO STEP 5
STEP 5 - IF (sales>=60000) THEN comm=0.2 *sales OTHERWISE
GOTO STEP 6
STEP 6 - IF (sales>=40000) THEN comm=0.15 *sales OTHERWISE
GOTO STEP 7
STEP 7 - comm=0.125*sales
STEP 8 - PRINT "Commission of the employee="+comm
STEP 9 - END
m

PROGRAM:

import java.io.*;

class SalesComission

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

double sales,comm;

BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter sales”);

sales=Double.parseDouble(aa.readLine()); //reading sales from the keyboard

if(sales>=100000)

comm=0.25*sales;

else if(sales>=80000)

comm=0.225*sales;

else if(sales>=60000)

comm=0.2*sales;

else if(sales>=40000)

comm=0.15*sales;

else comm=0.125*sales;

System.out.println("Commission of the employee="+comm);}}

Output
enter sales

—————————————————————————————

2130000

—————————————————————————————

employee=532500.0

Progra -2

Write a program in java to convert a decimal number into its


binary equivalent.

PROGRAM:
{

String num;

int base;

void decimal2Binary(String dn) {

int dec=Integer.parselnt(dn); base=2;

num=””;

while (dec!=0)

int r=dec%base; //to store remainder on division by 2

num=r+num; //to concatenate remainders obtained in successive

division dec=dec/base;

System.out.println("Binary Number="+num);

}}//class close
m

Output

Enter the decimal number

Input - “435”

—————————————————————————————

Binary number= 110110011


Progra - 3

Write a program to generate all twin prime numbers between

1 to 100.

Two prime numbers are called twin primes if there is present only

one composite number between them. Or we can also say two

prime numbers whose difference is two are called twin primes. For

example, (3,5) are twin primes, since the difference between the

two numbers 5 – 3 = 2.
m
PROGRAM:

class TwinPrimeNumbers

Boolean checkPrime(int N)

int c=0;

for(int i=1; i=N; i++)

if (N%i==0)

c++;

return c=2;

public static void main()

TwinPrimeNumbers obj = new TwinPrimeNumbers(); for(int num=1;


num<=100; num++)

boolean r1=obj.checkPrime(num);

boolean r2=obj.checkPrime(num+2);

if(r1==true && r2==true) System.out.println(num+","+(num+2));

Output

3, 5

5, 7

11, 13

17, 19

29, 31

41, 43

59, 61

71, 73

Progra - 4

Write a program to nd out whether a number entered is a


magic number or not.

ALGORITHM:
STEP 1: START

STEP 2: INITIALIZE SUMOFDIGITS VARIABLE VALUE TO 0. IT WILL


REPRESENT THE SUM OF DIGITS OF A GIVEN INPUTNUMBER.

STEP 3: CREATE A COPY OF THE INPUTNUMBER (ORIGINAL


NUMBER) BY STORING ITS VALUE IN VARIABLE NUMBER.

STEP 4: USING WHILE LOOP.


A. CONTINUE THE LOOP TILL THE SUMOFDIGITS DOES NOT
BECOME A SINGLE DIGIT OR NUMBER IS NOT EQUAL TO ZERO.
B. GET THE RIGHTMOST DIGIT OF VARIABLE NUMBER BY
USING (NUMBER % 10) AND ADD ITS VALUE TO THE VARIABLE
SUMOFDIGITS.

STEP 5: CHECK WHETHER THE VARIABLE SUMOFDIGITS IS


EQUAL TO 1.
A. IF BOTH ARE EQUAL THEN THE INPUTNUMBER IS MAGIC
NUMBER.
B. OTHERWISE, THE INPUTNUMBER IS NOT A MAGIC
NUMBER.

STEP 6: END
m

fi
PROGRAM:

Import java.util.*; class magic {

public static void main(String args[]) {

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

Scanner scan = new Scanner(System.in);

int inputNumber = scan.nextInt();

boolean result = checkMagicNumber(inputNumber); if(result)

System.out.println(inputNumber +" is a Magic Number"); else

System.out.println(inputNumber +" is NOT a Magic Number"); }

static boolean checkMagicNumber(int inputNumber) { // Initialize


sumOfDigits of inputNumber

int sumOfDigits = 0;

// Create a copy of the inputNumber

int number = inputNumber;

while( sumOfDigits > 9 || number > 0) {

/* if number is zero and sumOfDigits is non-zero

then the loop will continue.

Loop will stop when number is zero and sumOfDigits becomes a


single digit */

if (number == 0) { number = sumOfDigits;

sumOfDigits = 0; }

sumOfDigits = sumOfDigits + (number % 10);

number = number / 10; }

/* If sumOfDigits is equal to 1 then

the number is Magic number, otherwise not */ return (sumOfDigits


== 1);

}}

Output

Enter any number: 145

145 is a Magic number


Progra - 5

Write a program to Search an Array Using Binary Search.

ALGORITHM:
STEP 1: START

STEP 2: INPUT THE NUMBER OF ELEMENTS IN THE ARRAY

STEP 3: CREATE ARRAY TO STORE THE NUMBERS

STEP 4: INPUT THE ELEMENTS IN THE ARRAY IN ARRAY FROM THE USER

STEP 5: INPUT THE VALUE TO BE SEARCHED FOR

STEP 6: INITIALIZE THE FIRST AND LAST LIMIT OF PORTION OF ARRAY TO BE


SEARCHED

STEP 7: WHILE (FIRST<=LAST)


MATCH THE MID VALUE OF THE PORTION DEFINED BY FIRST AND LAST

STEP 8: IF MID VALUE MATCHES WITH THE VALUE TO BE SEARCHED THEN GOTO
STEP 11

STEP 9: IF MID VALUE IS LESS THAN VALUE BEING SEARCHED THEN


FIRST=MIDDLE + 1 GOTO STEP 7

STEP 10: IF MID VALUE IS MORE THAN VALUE TO BE SEARCHED THEN


LAST=MIDDLE-1 GOTO STEP 7

STEP 11: PRINT THAT THE VALUE IS FOUND

STEP 12: IF (FIRST>LAST) PRINT NOT FOUND

STEP 13: END


m

PROGRAM:

import java.util.*; class BinarySearch

public static void main(String args[])

int i, num, item, array[], rst, last, middle; Scanner sc= new
Scanner(System.in); System.out.println("Enter number of
elements:"); num = sc.nextInt();

//Creating array to store the all the numbers

array = new int[num];

System.out.println("Enter " + num + " integers"); //Loop to store


each numbers in array

for (i = 0; i < num; i++) array[i] =sc.nextInt();

System.out.println("Enter the search value:"); item = sc.nextInt();

rst = 0;

last = num - 1;

middle = ( rst + last)/2;

while( rst <= last )

if ( array[middle] < item )

rst = middle + 1;

else if ( array[middle] == item )

System.out.println(item + " found at location " + (middle + 1));


break;

} else

last = middle - 1; }

middle = ( rst + last)/2; }

if ( rst > last )

System.out.println(item + " is not found. ");

} }

fi
fi
fi
fi

fi
fi
fi
Output

Enter number of elements: 7

Enter 7 integers

66

77

99

Enter the search value: 77

77 found at location 4

Progra - 6

Write a program to Sort an array Using Selection Sort.

ALGORITHM:
STEP 1: START

STEP 2: INPUT a[ ]

STEP 3: PRINT a[ i ]+” “

STEP 4: ag = -1

STEP 5: From i = 0 to i < n-1 REPEAT STEP 7 TO 11

STEP 6: min = i STEP 8 - FROM j = i+1 to j

STEP 7: FROM j = i+1 to j

STEP 8: IF (a[j]<a[min]) then min = j

STEP 9: IF (min!=i) GOTO STEP 11

STEP 10: temp = a[i], a[i] = a[min], a[min] = temp

STEP 11: create main function to call all functions

STEP 12: END


fl
m

PROGRAM:

import java.io.*;

class SelectionSort

{int n,i;

int a[] = new int[100];

public SelectionSort(int nn) //parameterized constructor {n=nn;

public void input() throws IOException //function accepting array


elements {Bu eredReader br =new Bu eredReader(new
InputStreamReader(System.in)); System.out.println("enter
elements");

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

{a[i] = Integer.parseInt(br.readLine());

}}

public void display() //function displaying array elements

{System.out.println();

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

{System.out.print(a[i]+" ");

}}

public void sort() //function sorting array elements using selection


sort technique {int j,temp,min;

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

{min =i;

for(j=i+1;j<n;j++)

{if(a[j]<a[min])

min =j;

if(min!=i)

{temp = a[i];

a[i] =a[min];

a[min] = temp;

}}}

ff
ff
public static void main(String args[]) throws IOException //main
function {SelectionSort x = new SelectionSort(5);

x.input();

System.out.print("Before sorting - ");

x.display();

System.out.print("After sorting - ");

x.sort();

x.display();

}}
Output

enter elements 45

7878

878

98

Before sorting -

45 7878 2 878 98 After sorting - 2 45 98 878 7878


Progra - 7

Write a program to Generate Sum of All Elements of a 5*5


Double Dimensional Array

ALGORITHM:
STEP 1: START

STEP 2: INPUT a[ ]

STEP 3: FROM x=0 to x<5 REPEAT STEP 4

STEP 4: FROM y=0 to y<5 REPEAT STEP 5

STEP 5: PRINT (a[x][y]+” “)

STEP 6: FROM x=0 to x<5 REPEAT STEP 7

STEP 7: FROM y=0 to y<5 REPEAT STEP 8

STEP 8: Sum = Sum + a[x][y]

STEP 9: PRINT Sum

STEP 10: END


PROGRAM:

import java.io.*;

class MatrixSum

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


function { int a[][]=new int[5][5];

Bu eredReader aa=new Bu eredReader(new


InputStreamReader(System.in)); int x,y,z,Sum=0;

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

for(x=0;x<5;x++) //loop for reading array

{for(y=0;y<5;y++)

{ z=Integer.parseInt(aa.readLine()); //accepting array element a[x]


[y]=z;

}}

System.out.println("Array -");

for(x=0;x<5;x++)

{for(y=0;y<5;y++)

{System.out.print(a[x][y]+" ");

System.out.print("\n");

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);

}}
ff

ff
Output

25

Array –

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

Sum of Array elements=325


Progra - 8

Write a program to nd Sum of Each Column of a Double


Dimensional Array

ALGORITHM:
STEP 1: START

STEP 2: INPUT a[ ]

STEP 3: FROM x=0 to x<4 REPEAT STEP 4

STEP 4: FROM y=0 to y<4 REPEAT STEP 5

STEP 5: PRINT (a[x][y]+” “)

STEP 6: FROM x=0 to x<4 REPEAT STEP 7, STEP 9 and STEP 10

STEP 7: FROM y=0 to y<4 REPEAT STEP 8

STEP 8: Sum = Sum + a[x][y]

STEP 9: PRINT Sum

STEP 10: Sum = 0

STEP 11: END


fi

PROGRAM:

import java.io.*;

class ColoumnSum

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


function

{int a[][]=new int[4][4];

Bu eredReader aa=new Bu eredReader(new


InputStreamReader(System.in)); int x,y,z,Sum=0;

System.out.println("Enter the array"); //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 -");

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);

Sum=0;

}}}
ff

ff
Output

16

Array-

1234

5678

9 10 11 12

13 14 15 16

Sum of column 1 is 28

Sum of column 2 is 32

Sum of column 3 is 36

Sum of column 4 is 40

Progra - 9

Write a program to nd Sum of Diagonal of a Double


Dimensional Array of 4*4

ALGORITHM:
STEP 1: START

STEP 2: INPUT a[ ]

STEP 3: FROM x=0 to x<4 REPEAT STEP 4

STEP 4: FROM y=0 to y<4 REPEAT STEP 5

STEP 5: PRINT (a[x][y]+” “)

STEP 6: FROM x=0 to x<4 REPEAT STEP 7

STEP 7: Sum = Sum + a[x][y], y = y+1

STEP 8: PRINT Sum

STEP 9: Sum = 0

STEP 10: END


fi

PROGRAM:

import java.io.*;

class DiagonalSum

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


function

{int a[][]=new int[4][4];

Bu eredReader aa=new Bu eredReader(new


InputStreamReader(System.in));

int x,y,z,Sum=0;

System.out.println("Enter the array"); for(x=0;x<4;x++) //Reading


array {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++)

{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;

System.out.println("Sum of diagonal is "+Sum); Sum=0;

}}
ff

ff
Output

16

Array-

1234

5678

9 10 11 12

13 14 15 16

Sum of Diagonal is 34

Progra - 10

Write a program to create a String and Count Number of


Vowels and Consonants.

ALGORITHM:
STEP 1: START

STEP 2: a = “Computer Applications”

STEP 3: z = a.length()

STEP 4: x=0, b=0

STEP 5: FROM y = 0 tp y<z REPEAT STEP 6

STEP 6: IF (a.charAt(y)==‘a’||a.charAt(y)==‘e’||a.charAt(y)==‘i’||a.charAt(y)==‘o’||
a.charAt(y)==‘u’) THEN x=x+1 OTHERWISE b = b+1

STEP 7: PRINT x

STEP 8: PRINT b

STEP 9: END

PROGRAM:

import java.io.*;

class Vowels

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


function

{Bu eredReader br=new Bu eredReader(new


InputStreamReader(System.in)); System.out.println("Enter a string");

String a= br.readLine(); //Accepting string

int z=a.length(),y,x=0,b=0;

for(y=0;y<z;y++) //loop for counting number of vowels


{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 in string ="+x); //displaying


result System.out.println("Number of consonants in string ="+b);

}}
ff

ff
Output

Enter a string Pneumonoultramicroscopicsilicovolcanokoniosis

Number of vowels in the string = 20

Number of consonants in the string = 25


Progra - 11

Pascal’s Triangle

ALGORITHM:
STEP 1: START
STEP 2: pas[0]=1
STEP 3: IF i=0 THEN GOTO STEP 4
STEP 4: IF j=0 THEN GOTO STEP 5
STEP 5: PRINT pas[j]+””
STEP 6: i++ & IF i<n GOTO STEP 4
STEP 7: j=0 & IF j<=i GOTO STEP 5
STEP 8: IF j=i+1 THEN GOTO STEP 7
STEP 9: pas[j]=pas[j]+pas[j-1]
STEP 10: j - - & IF j>0 GOTO STEP 9
STEP 11: END

PROGRAM:
import java.io.*;

class Pascal

{public void pascalw()throws IOException

{Bu eredReader br=new Bu eredReader(new InputStreamReader(System.in));


System.out.println(“Enter a no.”);

int n=Integer.parseInt(br.readLine()); //accepting value

int [ ] pas = new int [n+1];

pas[0] = 1;

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

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

System.out.print(pas[j]+" ");

System.out.println( );

for (int j=i+1; j>0; j--)

pas[j]=pas[j]+pas[j-1];

}}}

ff
m

ff

Output
Progra - 12

To create a string and replace all vowels with ‘*’

ALGORITHM:
STEP 1: START
STEP 2: a=“Computer Applications”
STEP 3: x=0
STEP 4: FROM z=0 to z<a.length() REPEAT STEP 5
STEP 5: if(a.charAt(z)==‘a’||a.charAt(z)==‘i’||a.charAt(z)==’e’||a.charAt(z)==’o’||a.charAt(z)==’u’) tHEN
a.setCharAt(z.””)
STEP 6: PRINT “New String-“+a
STEP 7: END

PROGRAM:
import java.io.*;

class VowelReplace

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

{Bu eredReader br=new Bu eredReader(new InputStreamReader(System.in));


System.out.println(“Enter a String”);

StringBu er a=new StringBu er(br.readLine());

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("New String -"+a);

}}
ff
ff
m

ff
ff

Output
Progra - 13

Star Pattern using input String

PROGRAM:
import java.io.*;

class Pattern

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

{int i,sp,j,k,l;

Bu eredReader br = new Bu eredReader(new InputStreamReader(System.in));

System.out.println("enter the string ="); 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(" ");

}}}
ff
m

ff

Output
Progra - 14

Decoding of string

PROGRAM:
import java.io.*;

class Decode

{public void compute()throws IOException //compute() function {Bu eredReader


br=new Bu eredReader(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 for NumberFormatException

{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));

}}

m
ff

ff

Output
Progra - 15

Sales Commission

Sales Commission
>=100000 25% of sales
80000-99999 22.5% of sales
60000-79999 20% of sales
40000-59999 15% of sales
<40000 12.5% of sales

PROGRAM:
import java.io.*;

class SalesComission

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

{double sales,comm;

Bu eredReader aa=new Bu eredReader(new InputStreamReader(System.in));


System.out.println(“Enter sales”);

sales=Double.parseDouble(aa.readLine());

if(sales>=100000) comm=0.25*sales;

else if(sales>=80000) comm=0.225*sales;

else if(sales>=60000)

comm=0.2*sales;

else if(sales>=40000)

comm=0.15*sales;

else comm=0.125*sales;

System.out.println("Commission of the employee="+comm); }}


ff
m

ff

Output
Progra - 16

Decimal to Roman Numeral

PROGRAM:
import java.io.*;

public class Dec2Roman

{public static void main() throws IOException //main function

{DataInputStream in=new DataInputStream(System.in);

System.out.print("Enter Number : ");

int num=Integer.parseInt(in.readLine()); //accepting decimal number

String hund[]={"","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]); }}

Output
Progra - 17

Date Program

PROGRAM:
import java.io.*;

class Day2Date

{static Bu eredReader br =new Bu eredReader(new


InputStreamReader(System.in)); public void calc(int n, int yr) //function to calculate
date

{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)

{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[]) throws IOException

{Day2Date obj = new Day2Date();

System.out.println( "Enter day no = ");

int n = Integer.parseInt(br.readLine());

System.out.println( "Enter year = ");

int yr = Integer.parseInt(br.readLine());

obj.calc(n,yr);

}}
ff
m

ff

Output
Progra - 18

Celsius to Fahrenheut using inheritance

PROGRAM:
import java.io.*;

class C2F

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

{Temperature ob= new Temperature();

Bu eredReader br=new Bu eredReader(new InputStreamReader(System.in));

System.out.println("Enter temperature in Celsius"); //accepting temperature

double temp=ob.convert(Double.parseDouble(br.readLine()));

System.out.println("The temperature in fahrenheit is = "+temp);

class Temperature extends C2F

{double convert(double celcius) //function to convert Celsius to fahrenheit

{double far=1.8*celcius+32.0;

return far;

}}
ff
m

ff

Output
Progra - 19

GCD Series

PROGRAM:
import java.io.*;

class GCD

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


{Bu eredReader br = new Bu eredReader(new InputStreamReader(System.in));
System.out.println("enter the numbers =");

int p = Integer.parseInt(br.readLine()); int q = Integer.parseInt(br.readLine()); GCD


obj = new GCD();

int g = obj.calc(p,q); System.out.println("GCD ="+g);

//accepting nos.

public int calc(int p,int q) {if(q==0)

return p;

else return calc(q,p%q); }}


ff
m

ff

Output
Progra - 20

Frequency of each string character

PROGRAM:
import java.io.*;

class Frequency

{private int i,a1,l,p,j,freq;

public Frequency() //default constructor

{p=0;

freq=0; // initialise instance variables

public void count(String str)

{int ii;

l=str.length();

System.out.print(str);

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

{char a=str.charAt(i);

for(ii=0;ii<l;ii++)

{char b = str.charAt(ii);

if (a==b)

freq=freq+1;

System.out.println(a+" occurs "+freq+" times");

freq=0;

}}

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

{Bu eredReader br =new Bu eredReader(new InputStreamReader(System.in));


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

String str = br.readLine();

Frequency x = new Frequency();

x.count(str);

}}
ff
m

ff

Output
Progra - 21

Number in Words

PROGRAM:
import java.io.*;

class Num2Words

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

//main function

{Bu eredReader br=new Bu eredReader(new InputStreamReader(System.in));

System.out.println("Enter any Number(less than 99)");

int amt=Integer.parseInt(br.readLine()); //accepting number

int z,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; // nding the number in words

g=amt/10;

if(g!=1)

System.out.println(x2[g-1]+" "+x1[z]);

else System.out.println(x[amt-9]);

}}
ff
m

fi
ff

Output
Progra - 22

Arithmetic Progession (AP) Series

PROGRAM:
class APSeries

{private double a,d;

APSeries() //default constructor

{a = d = 0;

APSeries(double a,double d)

{this.a = a;

this.d = d;

double nTHTerm(int n)

{return (a+(n-1)*d);

double Sum(int n)

{return (n*(a+nTHTerm(n))/2);

void showSeries(int n) //displaying AP Series {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));

void main()throws IOException

{Bu eredReader br= new Bu eredReader(new InputStreamReader(System.in));


System.out.println("Enter 1st term");

a=Integer.parseInt(br.readLine()); //accepting 1st term System.out.println("Enter


Common di erence");

d=Integer.parseInt(br.readLine()); System.out.println("Enter no.of terms"); int


n=Integer.parseInt(br.readLine()); nTHTerm(n);

Sum(n);

showSeries(n);

}
ff
m

ff
ff

Output
Progra - 23

Calendar of any Month

PROGRAM:
class APSeries

{private double a,d;

APSeries() //default constructor

{a = d = 0;

APSeries(double a,double d)

{this.a = a;

this.d = d;

double nTHTerm(int n)

{return (a+(n-1)*d);

double Sum(int n)

{return (n*(a+nTHTerm(n))/2);

void showSeries(int n) //displaying AP Series {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));

void main()throws IOException

{Bu eredReader br= new Bu eredReader(new InputStreamReader(System.in));


System.out.println("Enter 1st term");

a=Integer.parseInt(br.readLine()); //accepting 1st term System.out.println("Enter


Common di erence");

d=Integer.parseInt(br.readLine()); System.out.println("Enter no.of terms"); int


n=Integer.parseInt(br.readLine()); nTHTerm(n);

Sum(n);

showSeries(n);

}
ff
m

ff
ff

Output
Progra - 24

Factorial (Using Recursion)

PROGRAM:
import java.io.*;

class Factorial

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


{Bu eredReader br = new Bu eredReader(new InputStreamReader(System.in));
System.out.println("enter no =");

int n = Integer.parseInt(br.readLine());

Factorial obj = new Factorial();

long f = obj.fact(n);

System.out.println("Factorial ="+f);

public long fact(int n)

{if(n<2)

return 1;

else return (n*fact(n-1));

}}
ff
m

ff

Output
Progra - 25

Fibonacci (Using Recursion)

PROGRAM:
import java.io.*;

class Fibonacci

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

{Fibonacci obj = new Fibonacci();

Bu eredReader br = new Bu eredReader(new InputStreamReader(System.in));


System.out.println("enter no of term ="); //accepting no. of terms

int n = Integer.parseInt(br.readLine());

System.out.println();

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

{int f = obj. b(i);

System.out.print(f+" ");

}}

public int b(int n)

{if(n<=1)

return n;

else

return ( b(n-1) + b(n-2));

}}
ff
fi
m
fi
fi

fi
ff

Output
Progra - 26

Spiram Matrix

PROGRAM:
import java.io.*;

class SpiralMatrix

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

{int a[][],r,c,k1=2,k2=3,p=0,co=0,re=0;

Bu eredReader br = new Bu eredReader(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=new int[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(int le=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(int y=0;y<l;y++)

{for(int yy=0;yy<l;yy++)

System.out.print("\t"+a[y][yy]);

System.out.println();

System.out.println();

}}}
ff
m

ff

Output
Progra - 27

Magic Number

PROGRAM:
import java.io.*;

class MagicSquare

{public static void main(String args[])throws Exception //main function


{Bu eredReader br = new Bu eredReader(new InputStreamReader(System.in));
System.out.println("enter the dimension of magical square=");

int n = Integer.parseInt(br.readLine()); //accepting dimensions

int arr[][]=new int[n][n],c=n/2-1,r=1,num;

for(num=1;num<=n*n;num++) {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++) {for(c=0;c<n;c++) System.out.print(arr[r][c]+" ");


System.out.println();

}}}
ff
m

ff

Output
Progra - 28

Palindrome Check

PROGRAM:
import java.io.*;

class Palindrome

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

{Bu eredReader br = new Bu eredReader(new InputStreamReader(System.in));


System.out.println("enter the string=");

String s = br.readLine(); //accepting the string

StringBu er sb = new StringBu er(s);

sb.reverse();

String rev = new String(sb); if(s.equalsIgnoreCase(rev))


System.out.println("Palindrome " );

else System.out.println("Not Palindrome " ); }}


ff
ff
m

ff
ff

Output
Progra - 29

String in Alphabetical Order

PROGRAM:
import java.io.*; class Alpha {String str;

int l;

char c[] = new char[100]; public Alpha()

{str = "";

l =0;

//Alpha() constructor

public void readword() throws IOException //function to read input string


{System.out.println("enter word - ");

Bu eredReader br =new Bu eredReader(new InputStreamReader(System.in));

str = br.readLine();

l = str.length();

public void arrange()

{int i,j;

char temp;

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

{c[i]= str.charAt(i);

for(i=0;i<l-1;i++)

{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;

}}}}

public void display()

{System.out.println();

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

{System.out.print(c[i]);

}}

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

{Alpha obj = new Alpha();

obj.readword();

obj.arrange();

obj.display();

}}
ff
m

ff

Output
Progra - 30

Sum of All Matrix Diagonal Elements

PROGRAM:
import java.io.*;

class DiagonalSum

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

{int a[][]=new int[4][4];

Bu eredReader aa=new Bu eredReader(new InputStreamReader(System.in)); int


x,y,z,Sum=0;

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

for(x=0;x<4;x++) //Reading array

{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++) {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;

//displaying array

//loop for nding sum of diagonal

System.out.println("Sum of diagonal is "+Sum); Sum=0;

}}
ff
m
fi

ff

Output
Bibliography
Site Used Link
Stack Over ow www.stackover ow.com

HackerRank www.hackerrank.com
fl
fl

You might also like