You are on page 1of 37

List of Java Programs

Program1 WAP to display the List of even numbers Program2 - Fa torial of a number Program! - "ompare #$o %umbers using else-if Program& - 'etermine (f )ear (s Leap )ear Program* - Fibona i +eries Program, - Palindrome %umber -. Program-- .enerate prime numbers bet$een 1 / given number 0. Program0- Pyramid of stars using nested for loops 1. Program1 - 2eversed pyramid using for loops / de rement operator. 13. Program13 - %ested +$it h 11. Program11 - "al ulate "ir le Area using radius 12. Program12 - Fa torial of a number using re ursion
1. 2. !. &. *. ,.

Program1! - Pyramid of numbers using for loops Program1& - #o Find 4a5imum of #$o %umbers. Program1* - #o Find 4inimum of #$o %umbers using onditional operator 1,. Program 1, - Write a program that $ill read a float type value from the 6eyboard and print the follo$ing output. -7+mall (nteger not less than the number. -7.iven %umber. -7Largest (nteger not greater than the number. 1-. Program 1- - Write a program to generate * 2andom nos. bet$een 1 to 1338 and it should not follo$ $ith de imal point. 10. Program 10 - Write a program to display a greet message a ording to 4ar6s obtained by student 11. Program 11 - Write a program to find +94 A%' P2:'9"# of a given 'igit. 23. Program 23 - Write a program to find sum of all integers greater than 133 and less than 233 that are divisible by 1!. 1&. 1*.

Program 21 - Write a program to on atenate string using for Loop 22. Program 22 - Program to 'isplay 4ultipli ation #able 2!. Program 2! - Write a program to +$ap the values 2&. Program 2& - Write a program to onvert given no. of days into months and days.;Assume that ea h month is of !3 days< 2*. Program 2* - Write a program to 'isplay (nvert #riangle using $hile loop. 2,. Program 2, - Write a program to find $hether given no. is Armstrong or not. 2-. Program 2- - s$it h ase demo 20. Program 20 - Write a program to generate =armoni +eries. 21. Program 21 - Write a program to find average of onse utive % :dd numbers and even numbers. !3. Program !3 - 'isplay #riangle as follo$> ;using for loops<
21.

1 2! &*, - 0 1 13 ... % ?@

Programs to $or6 out


1. WAP to display a olor name depending on olor value using s$it h. 2. A epting single hara ter8 int8 float8 string and double value from the 6eyboard. !. #o grade the students using s$it h and if-else. &. #o ompute the po$er of 2 using for loop *. #o find the sum of the digits of a given integer number.
2

6. .iven the month 8 identify the season using s$it h.

Program1 List of even numbers


/* List Even Numbers Java Example This List Even Numbers Java Example shows how to find and list even numbers between 1 and an given number!

*/

publi" "lass ListEvenNumbers # publi" stati" void main$%tring&' args( # //define limit int limit ) *+, limit(, % stem!out!println$-Printing Even numbers between 1 and - . for$int i)1, i /) limit, i..(# // if the number is divisible b 0 then it is even if$ i 1 0 )) +(# % stem!out!print$i . - -(, 2

2 2 2

/* 3utput of List Even Numbers Java Example would be Printing Even numbers between 1 and *+ 0 4 5 6 1+ 10 14 15 16 0+ 00 04 05 06 7+ 70 74 75 76 4+ 40 44 45 46 *+ */

Program0 8 Fa torial of a number


/* */ publi" "lass Number9a"torial # publi" stati" void main$%tring&' args( # int number ) *, /* * 9a"torial of an number is: n! * 9or example; fa"torial of 4 is 4*7*0*1! */ int fa"torial ) number, for$int i )$number 8 1(, i < 1, i88( This program shows how to "al"ulate 9a"torial of a number!

&

# 2 2 2

fa"torial ) fa"torial * i,

% stem!out!println$-9a"torial of a number is - . fa"torial(,

/* 3utput of the 9a"torial program would be 9a"torial of a number is 10+ */

Program7 8 "ompare #$o %umbers using else-if


/* =ompare Two Numbers Java Example This =ompare Two Numbers Java Example shows how to "ompare two numbers using if else if statements! */

publi" "lass =ompareTwoNumbers #

publi" stati" void main$%tring&' args( #

//de"lare two numbers to "ompare int num1 ) 704, int num0 ) 074,

if$num1 < num0(# % stem!out!println$num1 . - is greater than - . num0(, 2 else if$num1 / num0(#

% stem!out!println$num1 . - is less than - . num0(, 2 else# % stem!out!println$num1 . - is e>ual to - . num0(, 2 2 2

/* 3utput of =ompare Two Numbers Java Example would be 704 is greater than 074 */

Program4 8 'etermine (f )ear (s Leap )ear


/* ?etermine @f Aear @s Leap Aear Java Example This ?etermine @f Aear @s Leap Aear Java Example shows how to determine whether the given */ ear is leap ear or not!

publi" "lass ?etermineLeapAearExample #

publi" stati" void main$%tring&' args( #

// ear we want to "he"B int ear ) 0++4,

//if

ear is divisible b

4; it is a leap

ear

if$ ear 1 4++ )) +( CC $$ ear 1 4 )) +( DD $ ear 1 1++ :) +(( % stem!out!println$-Aear - . else % stem!out!println$-Aear - . 2 2 ear . - is not a leap ear-(, ear . - is a leap ear-(,

/* 3utput of the example would be Aear 0++4 is a leap */ ear

Program* 8 Fibona
/* */

i +eries

9ibona""i %eries Java Example This 9ibona""i %eries Java Example shows how to "reate and print 9ibona""i %eries using Java!

publi" "lass Java9ibona""i%eriesExample # publi" stati" void main$%tring&' args( # //number of elements to generate in a series int limit ) 0+, long&' series ) new long&limit', //"reate first 0 series elements

series&+' ) +, series&1' ) 1, //"reate the 9ibona""i series and store it in an arra for$int i)0, i / limit, i..(# series&i' ) series&i81' . series&i80', 2 //print the 9ibona""i series numbers % stem!out!println$-9ibona""i %eries upto - . limit(, for$int i)+, i/ limit, i..(# % stem!out!print$series&i' . - -(, 2 2 2

/* 3utput of the 9ibona""i %eries Java Example would be 9ibona""i %eries upto 0+ + 1 1 0 7 * 6 17 01 74 ** 6E 144 077 7FF 51+ E6F 1*EF 0*64 4161 */

Program5 8 Palindrome %umber


/* */ This program shows how to "he"B for in the given list of numbers whether ea"h number is palindrome or not

publi" "lass JavaPalindromeNumberExample # publi" stati" void main$%tring&' args( # //arra of numbers to be "he"Bed int numbers&' ) new int&'#101;17;74;11;00;*42, //iterate through the numbers for$int i)+, i / numbers!length, i..(# int number ) numbers&i', int reversedNumber ) +, int temp)+, /* * @f the number is e>ual to itGs reversed number; then * the given number is a palindrome number! * * 9or ex;101 is a palindrome number while 10 is not! */ //reverse the number while$number < +(# temp ) number 1 1+, number ) number / 1+, reversedNumber ) reversedNumber * 1+ . temp,

2 if$numbers&i' )) reversedNumber( % stem!out!println$numbers&i' . - is a palindrome-(, else % stem!out!println$numbers&i' . - not a palindrome -(, 2 2 2

/* 3utput of Java Palindrome Number Example would be 101 is a palindrome number 17 is not a palindrome number 74 is not a palindrome number 11 is a palindrome number 00 is a palindrome number *4 is not a palindrome number */

ProgramF8 Henerate prime numbers between 1 D given number


/* Prime Numbers Java Example This Prime Numbers Java example shows how to generate prime numbers between 1 and given number using for loop!

*/ publi" "lass HeneratePrimeNumbersExample # publi" stati" void main$%tring&' args( # //define limit int limit ) 1++, % stem!out!println$-Prime numbers between 1 and - . limit(, //loop through the numbers one b for$int i)1, i / 1++, i..(# boolean isPrime ) true, //"he"B to see if the number is prime for$int I)0, I / i , I..(# if$i 1 I )) +(# isPrime ) false, breaB, 2 one

2 // print the number if$isPrime( % stem!out!print$i . - -(,

/* 3utput of Prime Numbers example would be Prime numbers between 1 and 1++ 1 0 7 * F 11 17 1F 1E 07 0E 71 7F 41 47 4F *7 *E 51 5F F1 F7 FE 67 6E EF */

Program68 Pyramid of stars using nested for loops


/* Java P ramid 1 Example This Java P ramid example shows how to generate p ramid or triangle liBe given below using for loop! * ** *** **** ***** */ publi" "lass JavaP ramid1 # publi" stati" void main$%tring&' args( # for$int i)1, i/) * ,i..(# for$int I)+, I / i, I..(# % stem!out!print$-*-(, 2 //generate a new line % stem!out!println$--(, 2 2 /* 3utput of the above program would be * ** *** **** ***** */ 2

ProgramE Jeversed p ramid using for loops D de"rement operator!


/*

13

Java P ramid * Example This Java P ramid example shows how to generate p ramid or triangle liBe given below using for loop! 1074* 1074 107 10 1 */ publi" "lass JavaP ramid* # publi" stati" void main$%tring&' args( # for$int i)*, i<+ ,i88(# for$int I)+, I / i, I..(# % stem!out!print$I.1(, 2 2 2 2 /* 3utput of the example would be 1074* 1074 107 10 1 */ % stem!out!println$--(,

Program1+ 8 %ested +$it h


/* %tatements Example This example shows how to use nested swit"h statements in a Iava program! */

11

publi" "lass Nested%wit"hExample #

publi" stati" void main$%tring&' args( #

/* * LiBe an other Java statements; swit"h statements

* "an also be nested in ea"h other as given in * below example! */

int i ) +, int I ) 1,

swit"h$i( # "ase +K swit"h$I( # "ase +K % stem!out!println$-i is +; I is +-(, breaB,

"ase 1K % stem!out!println$-i is +; I is 1-(, breaB,

12

defaultK

% stem!out!println$-nested default "ase::-(, 2

breaB,

defaultK % stem!out!println$-No mat"hing "ase found::-(,

2 2

/* 3utput would be; i is +; I is 1 */

1!

Program11 8 =al"ulate =ir"le Lrea using radius


/* */ This program shows how to "al"ulate area of "ir"le using itGs radius!

import Iava!io!MufferedJeader, import Iava!io!@3Ex"eption, import Iava!io!@nput%treamJeader, publi" "lass =al"ulate=ir"leLreaExample # publi" stati" void main$%tring&' args( # int radius ) +, % stem!out!println$-Please enter radius of a "ir"le-(, tr # //get the radius from "onsole MufferedJeader br ) new MufferedJeader$new @nput%treamJeader$% stem!in((, radius ) @nteger!parse@nt$br!readLine$((, 2 //if invalid value was entered "at"h$Number9ormatEx"eption ne( # % stem!out!println$-@nvalid radius value- . ne(, % stem!exit$+(, 2 "at"h$@3Ex"eption ioe( # % stem!out!println$-@3 Error K- . ioe(, % stem!exit$+(, 2 /* * Lrea of a "ir"le is * pi * r * r * where r is a radius of a "ir"le! */ //N3TE K use Nath!P@ "onstant to get value of pi double area ) Nath!P@ * radius * radius, 2 2 /* 3utput of =al"ulate =ir"le Lrea using Java Example would be % stem!out!println$-Lrea of a "ir"le is - . area(,

1&

Please enter radius of a "ir"le 1E Lrea of a "ir"le is 1174!114E4FE4*E1*0 */

Program10 8 Fa torial of a number using re ursion


/* This program shows how to "al"ulate 9a"torial of a number using re"ursion fun"tion!

?@

import Iava!io!MufferedJeader, import Iava!io!@3Ex"eption, import Iava!io!@nput%treamJeader, publi" "lass Java9a"torialOsingJe"ursion # publi" stati" void main$%tring args&'( throws Number9ormatEx"eption; @3Ex"eption# % stem!out!println$-Enter the numberK -(, //get input from the user MufferedJeader br)new MufferedJeader$new @nput%treamJeader$% stem!in((, int a ) @nteger!parse@nt$br!readLine$((, //"all the re"ursive fun"tion to generate fa"torial int result) fa"t$a(, % stem!out!println$-9a"torial of the number isK - . result(,

stati" int fa"t$int b( # if$b /) 1( //if the number is 1 then return 1 return 1, else //else "all the same fun"tion with the value 8 1 return b * fa"t$b81(, 2 2 /* 3utput of this Java example would be Enter the numberK * 9a"torial of the number isK 10+ */

1*

Program17 p ramid of numbers using for loops


/* Henerate P ramid 9or a Hiven Number Example This Java example shows how to generate a p ramid of numbers for given number using for loop example! */

import Iava!io!MufferedJeader, import Iava!io!@nput%treamJeader,

publi" "lass HenerateP ramidExample #

publi" stati" void main $%tring&' args( throws Ex"eption#

MufferedJeader Be board ) new MufferedJeader $new @nput%treamJeader$% stem!in((,

% stem!out!println$-Enter NumberK-(, int as) @nteger!parse@nt $Be board!readLine$((, % stem!out!println$-Enter PK-(, int x) @nteger!parse@nt $Be board!readLine$((,

int

) +,

for$int i)+, i/) as ,i..(#

1,

for$int I)1, I /) i , I..(# % stem!out!print$ ) 2 . x, . -Qt-(,

% stem!out!println$--(, 2 2 2

/* 3utput of this example would be

Enter NumberK * Enter PK 1

+ 1 7 5 0 4 F * 6 10 E 17 14

1+ 11

8888888888888888888888888888888888888888888888

1-

Enter NumberK * Enter PK 0

+ 0 5 4 6 1+ 15 04 16 05 06

10 14 0+ 00

8888888888888888888888888888888888888888888888

Enter NumberK * Enter PK 7

+ 7 E 5 10 1* 04 75 0F 7E 40

16 01 7+ 77 */

10

Program14 To Find 4a5imum of #$o %umbers.


/* To 9ind Naximum of 0 Numbers using if else */ "lass Naxoftwo# publi" stati" void main$%tring args&'(# //taBing value as "ommand line argument! //=onverting %tring format to @nteger value int i ) @nteger!parse@nt$args&+'(, int I ) @nteger!parse@nt$args&1'(, if$i < I( % stem!out!println$i.- is greater than -.I(, else % stem!out!println$I.- is greater than -.i(, 2 2

Program1* To 9ind Ninimum of Two Numbers using "onditional operator


/* To find minimum of 0 Numbers using ternar */ "lass Ninoftwo# publi" stati" void main$%tring args&'(# //taBing value as "ommand line argument! //=onverting %tring format to @nteger value int i ) @nteger!parse@nt$args&+'(, operator

11

int I ) @nteger!parse@nt$args&1'(, int result ) $i/I(RiKI, % stem!out!println$result.- is a minimum value-(, 2 2

23

Program 15
/* Srite a program that will read a float t pe value from the print the following output! 8<%mall @nteger not less than the number! 8<Hiven Number! 8<Largest @nteger not greater than the number! */ class ValueFormat{ public static void main(String args[]){ double i = 34.32 !!given number Be board and

S"stem.out.println(#Small $nteger not greater t%an t%e number & #'(at%.ceil(i)) S"stem.out.println(#)iven *umber & #'i) S"stem.out.println(#+argest $nteger not greater t%an t%e number & #'(at%.,loor(i)) -

Program 1F 8 Srite a program to generate * Jandom nos! between 1 to 1++; and it should not follow with de"imal point!

class .andom/emo{ public static void main(String args[]){ ,or(int i=0 i1=2 i''){ S"stem.out.println((int)((at%.random()3044)) -

21

Program 16 5 Srite a program to displa a greet message a""ording to NarBs obtained b student!
class S6itc%/emo{ public static void main(String args[]){ int mar7s = $nteger.parse$nt(args[4]) as command line argument. s6itc%(mar7s!04){ case 04& case 8& case 9& S"stem.out.println(#:;cellent#) brea7 case <& S"stem.out.println(#Ver" )ood#) brea7 case =& S"stem.out.println(#)ood#) brea7 case 2& S"stem.out.println(#>or7 ?ard#) brea7 case 4& S"stem.out.println(#@oor#) brea7 case 3& !!ta7e mar7s

22

case 2& case 0& case 4& S"stem.out.println(#Ver" @oor#) brea7 de,ault& S"stem.out.println(#$nvalid value :ntered#) -

Program 1E 5 Srite a program to find %ON LN? PJ3?O=T of a given ?igit!


class SumA@roductAo,/igit{ public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) !!ta7ing value as command line argument. int temp = numBresult=4 !!+ogic ,or sum o, digit 6%ile(tempC4){ result = result ' temp temp55 S"stem.out.println(#Sum o, /igit ,or #'num'# is & #'result) !!+ogic ,or product o, digit temp = num

2!

result = 0 6%ile(temp C 4){ result = result 3 temp temp55 S"stem.out.println(#@roduct o, /igit ,or #'num'# is & #'result) -

Program 0+ 5 Srite a program to find sum of all integers greater than 1++ and less than 0++ that are divisible b F
class SumD,/igit{ public static void main(String args[]){ int result=4 ,or(int i=044 i1=244 i''){ i,(iE<==4) result'=i S"stem.out.println(#Dutput o, @rogram is & #'result) -

Program 01 5 Srite a program to "on"atenate string using for Loop


ExampleK @nput 8 *

2&

3utput 8 1 0 7 4 * */ class Foin{ public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) String result = # # ,or(int i=0 i1=num i''){ result = result ' i ' # # S"stem.out.println(result) -

Program 00 5 Program to ?ispla


class (ultiplicationGable{

Nultipli"ation Table

public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) S"stem.out.println(#33333(H+G$@+$IJG$D* GJK+:33333#) ,or(int i=0 i1=num i''){ ,or(int L=0 L1=num L''){ S"stem.out.print(# #'i3L'# #) S"stem.out.print(#Mn#) -

2*

Program 07 5 Srite a program to %wap the values


class S6ap{ public static void main(String args[]){ int num0 = $nteger.parse$nt(args[4]) int num2 = $nteger.parse$nt(args[0]) S"stem.out.println(#Mn333Ke,ore S6apping333#) S"stem.out.println(#*umber 0 & #'num0) S"stem.out.println(#*umber 2 & #'num2) !!S6ap logic num0 = num0 ' num2 num2 = num0 5 num2 num0 = num0 5 num2 S"stem.out.println(#Mn333J,ter S6apping333#) S"stem.out.println(#*umber 0 & #'num0) S"stem.out.println(#*umber 2 & #'num2) -

2,

Program 04 5 Srite a program to "onvert given no! of da s into months and da s!$Lssume that ea"h month is of 7+ da s(
Example K @nput 8 5E 3utput 8 5E da s ) 0 Nonth and E da s */ class /a"(ont%/emo{ public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) int da"s = numE34 int mont% = num!34 S"stem.out.println(num'# da"s = #'mont%'# (ont% and #'da"s'# da"s#) -

Program 0* 5 Srite a program to ?ispla using while loop!


ExampleK @nput 8 * 3utput K * * * * * 4 4 4 4 7 7 7 0 0 1

@nvert Triangle

2-

*/ class $nvertGriangle{ public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) 6%ile(num C 4){ ,or(int L=0 L1=num L''){ S"stem.out.print(# #'num'# #) S"stem.out.print(#Mn#) num55 -

Program 05 5 Srite a program to find whether given no! is Lrmstrong or not!


Example K @nput 8 1*7 3utput 8 1T7 . *T7 . 7T7 ) 1*7; so it is Lrmstrong no! */ class Jrmstrong{ public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) int n = num !!use to c%ec7 at last time

int c%ec7=4Bremainder 6%ile(num C 4){ remainder = num E 04

20

c%ec7 = c%ec7 ' (int)(at%.po6(remainderB3) num = num ! 04 i,(c%ec7 == n) S"stem.out.println(n'# is an Jrmstrong *umber#) else S"stem.out.println(n'# is not a Jrmstrong *umber#) -

Program 0F 5 swit"h "ase demo


Example K @nput 8 104 3utput 8 3ne Two 9our */

class S6itc%Iase/emo{ public static void main(String args[]){ tr"{ int num = $nteger.parse$nt(args[4]) int n = num !!used at last time c%ec7

int reverse=4Bremainder 6%ile(num C 4){ remainder = num E 04 reverse = reverse 3 04 ' remainder num = num ! 04 -

21

String result=##

!!contains t%e actual output

6%ile(reverse C 4){ remainder = reverse E 04 reverse = reverse ! 04 s6itc%(remainder){ case 4 & result = result ' #Nero # brea7 case 0 & result = result ' #Dne # brea7 case 2 & result = result ' #G6o # brea7 case 3 & result = result ' #G%ree # brea7 case 4 & result = result ' #Four # brea7 case 2 & result = result ' #Five # brea7 case = & result = result ' #Si; # brea7

!3

case < & result = result ' #Seven # brea7 case 9 & result = result ' #:ig%t # brea7 case 8 & result = result ' #*ine # brea7 de,ault& result=## S"stem.out.println(result) -catc%(:;ception e){ S"stem.out.println(#$nvalid *umber Format#) -

Program 06 8 Srite a program to generate Uarmoni" %eries!


Example K @nput 8 * 3utput 8 1 . 1/0 . 1/7 . 1/4 . 1/* ) 0!06 $Lpproximatel ( */ class ?armonicSeries{

!1

public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) double result = 4.4 6%ile(num C 4){ result = result ' (double) 0 ! num num55 S"stem.out.println(#Dutput o, ?armonic Series is #'result) -

!2

Program 0E 5 Srite a program to find average of "onse"utive N 3dd no! and Even no!
class :venDddAJvg{ public static void main(String args[]){ int n = $nteger.parse$nt(args[4]) int cnt:ven=4BcntDdd=4Bsum:ven=4BsumDdd=4 6%ile(n C 4){ i,(nE2==4){ cnt:ven'' sum:ven = sum:ven ' n else{ cntDdd'' sumDdd = sumDdd ' n n55 int evenJvgBoddJvg evenJvg = sum:ven!cnt:ven oddJvg = sumDdd!cntDdd S"stem.out.println(#Jverage o, ,irst * :ven no is #'evenJvg) S"stem.out.println(#Jverage o, ,irst * Ddd no is #'oddJvg)

!!

Program 7+ 5 ?ispla
1 0 7 4 * 5 F 6 E 1+ !!! N */ class Dutput0{

Triangle as follow!

public static void main(String args[]){ int c=4 int n = $nteger.parse$nt(args[4]) loop0& ,or(int i=0 i1=n i''){ loop2& ,or(int L=0 L1=i L''){ i,(cO=n){ c'' S"stem.out.print(c'# #) else brea7 loop0 S"stem.out.print(#Mn#) -

!&

Extra Programs 1 5 Srite a program to 9ind whether number is Prime or Not!


class @rime*o{ public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) int ,lag=4 ,or(int i=2 i1num i''){ i,(numEi==4) { S"stem.out.println(num'# is not a @rime *umber#) ,lag = 0 brea7 i,(,lag==4) S"stem.out.println(num'# is a @rime *umber#) -

!*

Program 0 5 Srite a program to find whether no! is palindrome or not!


Example K @nput 8 10*01 is a palindrome no! @nput 8 1074* is not a palindrome no! */ class @alindrome{ public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) int n = num !!used at last time c%ec7

int reverse=4Bremainder 6%ile(num C 4){ remainder = num E 04 reverse = reverse 3 04 ' remainder num = num ! 04 i,(reverse == n) S"stem.out.println(n'# is a @alindrome *umber#) else S"stem.out.println(n'# is not a @alindrome *umber#) -

!,

Program 7 5 ?ispla

Triangle as follow

+ 1 + 1 + 1 + 1 + 1 */ class Dutput2{ public static void main(String args[]){ ,or(int i=0 i1=4 i''){ ,or(int L=0 L1=i L''){ S"stem.out.print(((i'L)E2)'# #) S"stem.out.print(#Mn#) -

!-

You might also like