Professional Documents
Culture Documents
List of Java Programs
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 ... % ?@
*/
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 *+ */
&
# 2 2 2
fa"torial ) fa"torial * i,
//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(#
/* 3utput of =ompare Two Numbers Java Example would be 704 is greater than 074 */
//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-(,
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 */
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,
/* 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 */
*/ 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
/* 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 */
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$--(,
11
int i ) +, int I ) 1,
12
defaultK
breaB,
2 2
1!
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&
?@
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*
% 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
) +,
1,
% stem!out!println$--(, 2 2 2
+ 1 7 5 0 4 F * 6 10 E 17 14
1+ 11
8888888888888888888888888888888888888888888888
1-
+ 0 5 4 6 1+ 15 04 16 05 06
10 14 0+ 00
8888888888888888888888888888888888888888888888
+ 7 E 5 10 1* 04 75 0F 7E 40
16 01 7+ 77 */
10
11
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#) -
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) -
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) -
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*
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#) -
@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 -
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#) -
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=##
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#) -
!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#) -
!&
!*
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#) -
!-