1. Program1 WAP to display the List of even numbers
2. Program2 - Fatorial of a number !. Program! - "ompare #$o %umbers using else-if &. Program& - 'etermine (f )ear (s Leap )ear *. Program* - Fibonai +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 / derement operator. 13. Program13 - %ested +$ith 11. Program11 - "alulate "irle Area using radius 12. Program12 - Fatorial of a number using reursion 1!. Program1! - Pyramid of numbers using for loops 1&. Program1& - #o Find 4a5imum of #$o %umbers. 1*. 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 deimal point. 10. Program 10 - Write a program to display a greet message aording 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 21. Program 21 - Write a program to onatenate string using for Loop 22. Program 22 - Program to 'isplay 4ultipliation #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 eah 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$ith ase demo 20. Program 20 - Write a program to generate =armoni +eries. 21. Program 21 - Write a program to find average of onseutive % :dd numbers and even numbers. !3. Program !3 - 'isplay #riangle as follo$> ;using for loops< 1 2 ! & * , - 0 1 13 ... % ?@ Programs to $or6 out 1. WAP to display a olor name depending on olor value using s$ith. 2. Aepting single harater8 int8 float8 string and double value from the 6eyboard. !. #o grade the students using s$ith 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$ith. ! 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 ) *+,
%stem!out!println$-Printing Even numbers between 1 and - . limit(,
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 Fatorial of a number /* This program shows how to "al"ulate 9a"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( & # fa"torial ) fa"torial * i, 2
%stem!out!println$-9a"torial of a number is - . fa"torial(, 2 2
/* 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 - . ear . - is a leap ear-(, else %stem!out!println$-Aear - . ear . - is not a leap ear-(, 2 2
/* 3utput of the example would be Aear 0++4 is a leap ear */ Program* 8 Fibonai +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
/* 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, 0 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 one 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 2 // print the number if$isPrime( %stem!out!print$i . - -(, 2 1 2 2
/* 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 Pramid 1 Example This Java Pramid example shows how to generate pramid or triangle liBe given below using for loop!
* ** *** **** ***** */
publi" "lass JavaPramid1 #
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 2
/* 3utput of the above program would be * ** *** **** ***** */ ProgramE Jeversed pramid using for loops D de"rement operator! /* 13 Java Pramid * Example This Java Pramid example shows how to generate pramid or triangle liBe given below using for loop!
1074* 1074 107 10 1
*/
publi" "lass JavaPramid* #
publi" stati" void main$%tring&' args( #
for$int i)*, i<+ ,i88(#
for$int I)+, I / i, I..(# %stem!out!print$I.1(, 2
%stem!out!println$--(, 2
2 2
/*
3utput of the example would be 1074* 1074 107 10 1
*/ Program1+ 8 %ested +$ith /* %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,
/* 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! */
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,
%stem!out!println$-Lrea of a "ir"le is - . area(, 2 2
/* 3utput of =al"ulate =ir"le Lrea using Java Example would be 1& Please enter radius of a "ir"le 1E Lrea of a "ir"le is 1174!114E4FE4*E1*0 */ Program10 8 Fatorial of a number using reursion /* 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,
//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(, 2
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 pramid of numbers using for loops /* Henerate Pramid 9or a Hiven Number Example This Java example shows how to generate a pramid of numbers for given number using for loop example! */
publi" stati" void main $%tring&' args( throws Ex"eption#
MufferedJeader Beboard ) new MufferedJeader $new @nput%treamJeader$%stem!in((,
%stem!out!println$-Enter NumberK-(, int as) @nteger!parse@nt $Beboard!readLine$((, %stem!out!println$-Enter PK-(, int x) @nteger!parse@nt $Beboard!readLine$((,
int ) +,
for$int i)+, i/) as ,i..(#
1, for$int I)1, I /) i , I..(# %stem!out!print$ . -Qt-(, ) . x, 2
%stem!out!println$--(, 2 2 2
/* 3utput of this example would be
Enter NumberK * Enter PK 1
+ 1 0 7 4 * 5 F 6 E 1+ 11 10 17 14
8888888888888888888888888888888888888888888888
1- Enter NumberK * Enter PK 0
+ 0 4 5 6 1+ 10 14 15 16 0+ 00 04 05 06
8888888888888888888888888888888888888888888888
Enter NumberK * Enter PK 7
+ 7 5 E 10 1* 16 01 04 0F 7+ 77 75 7E 40 */ 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 operator */ "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&+'(, 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 tpe value from the Beboard and 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 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=0i1=2i''){ 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]) !!ta7e mar7s 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& 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=044i1=244i''){ 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=0i1=numi''){ result = result ' i ' # # - S"stem.out.println(result) - - Program 00 5 Program to ?ispla Nultipli"ation Table class (ultiplicationGable{ 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=0i1=numi''){ ,or(int L=0L1=numL''){ 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 das into months and das!$Lssume that ea"h month is of 7+ das( Example K @nput 8 5E 3utput 8 5E das ) 0 Nonth and E das */ 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 @nvert Triangle using while loop! ExampleK @nput 8 * 3utput K * * * * * 4 4 4 4 7 7 7 0 0 1 2- */ class $nvertGriangle{ public static void main(String args[]){ int num = $nteger.parse$nt(args[4]) 6%ile(num C 4){ ,or(int L=0L1=numL''){ 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 Triangle as follow! 1 0 7 4 * 5 F 6 E 1+ !!! N */ class Dutput0{ public static void main(String args[]){ int c=4 int n = $nteger.parse$nt(args[4]) loop0& ,or(int i=0i1=ni''){ loop2& ,or(int L=0L1=iL''){ 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=2i1numi''){ 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