List of Java Programs
List of Java Programs
Program1 WAP to display the List of even numbers Program2 - Factorial of a number Program3 - Compare Two Numbers using else-if Program4 - Determine If Year Is Leap Year Program5 - Fibonacci Series Program6 - Palindrome Number 7. Program7- Generate prime numbers between 1 & given number 8. Program8- Pyramid of stars using nested for loops 9. Program9 - Reversed pyramid using for loops & decrement operator. 10. Program10 - Nested Switch 11. Program11 - Calculate Circle Area using radius 12. Program12 - Factorial of a number using recursion
1. 2. 3. 4. 5. 6.
Program13 - Pyramid of numbers using for loops Program14 - To Find Maximum of Two Numbers. Program15 - To Find Minimum of Two Numbers using conditional operator 16. Program 16 - Write a program that will read a float type value from the keyboard and print the following output. ->Small Integer not less than the number. ->Given Number. ->Largest Integer not greater than the number. 17. Program 17 - Write a program to generate 5 Random nos. between 1 to 100, and it should not follow with decimal point. 18. Program 18 - Write a program to display a greet message according to Marks obtained by student 19. Program 19 - Write a program to find SUM AND PRODUCT of a given Digit. 20. Program 20 - Write a program to find sum of all integers greater than 100 and less than 200 that are divisible by 7
13. 14. 15.
Program 21 - Write a program to concatenate string using for Loop 22. Program 22 - Program to Display Multiplication Table 23. Program 23 - Write a program to Swap the values 24. Program 24 - Write a program to convert given no. of days into months and days.(Assume that each month is of 30 days) 25. Program 25 - Write a program to Display Invert Triangle using while loop. 26. Program 26 - Write a program to find whether given no. is Armstrong or not. 27. Program 27 - switch case demo 28. Program 28 - Write a program to generate Harmonic Series. 29. Program 29 - Write a program to find average of consecutive N Odd numbers and even numbers. 30. Program 30 - Display Triangle as follow: (using for loops)
21.
1 23 456 7 8 9 10 ... N */
*/
public class ListEvenNumbers { public static void main(String[] args) { //define limit int limit = 50; limit); System.out.println("Printing Even numbers between 1 and " + for(int i=1; i <= limit; i++){ // if the number is divisible by 2 then it is even if( i % 2 == 0){ System.out.print(i + " "); }
} } }
/* Output of List Even Numbers Java Example would be Printing Even numbers between 1 and 50 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 */
{ } } }
factorial = factorial * i;
//declare two numbers to compare int num1 = 324; int num2 = 234;
if(num1 > num2){ System.out.println(num1 + " is greater than " + num2); } else if(num1 < num2){
System.out.println(num1 + " is less than " + num2); } else{ System.out.println(num1 + " is equal to " + num2); } } }
/* Output of Compare Two Numbers Java Example would be 324 is greater than 234 */
if(year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) System.out.println("Year " + year + " is a leap year"); else System.out.println("Year " + year + " is not a leap year"); } }
series[0] = 0; series[1] = 1; //create the Fibonacci series and store it in an array for(int i=2; i < limit; i++){ series[i] = series[i-1] + series[i-2]; } //print the Fibonacci series numbers System.out.println("Fibonacci Series upto " + limit); for(int i=0; i< limit; i++){ System.out.print(series[i] + " "); } } }
/* Output of the Fibonacci Series Java Example would be Fibonacci Series upto 20 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 */
public class JavaPalindromeNumberExample { public static void main(String[] args) { //array of numbers to be checked int numbers[] = new int[]{121,13,34,11,22,54}; //iterate through the numbers for(int i=0; i < numbers.length; i++){ int number = numbers[i]; int reversedNumber = 0; int temp=0; /* * If the number is equal to it's reversed number, then * the given number is a palindrome number. * * For ex,121 is a palindrome number while 12 is not. */ //reverse the number while(number > 0){ temp = number % 10; number = number / 10; reversedNumber = reversedNumber * 10 + temp;
} if(numbers[i] == reversedNumber) System.out.println(numbers[i] + " is a palindrome"); else System.out.println(numbers[i] + " not a palindrome "); } } }
/* Output of Java Palindrome Number Example would be 121 is a palindrome number 13 is not a palindrome number 34 is not a palindrome number 11 is a palindrome number 22 is a palindrome number 54 is not a palindrome number */
*/ public class GeneratePrimeNumbersExample { public static void main(String[] args) { //define limit int limit = 100; System.out.println("Prime numbers between 1 and " + limit); //loop through the numbers one by one for(int i=1; i < 100; i++){ boolean isPrime = true; //check to see if the number is prime for(int j=2; j < i ; j++){ if(i % j == 0){ isPrime = false; break; }
/* Output of Prime Numbers example would be Prime numbers between 1 and 100 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 */
10
Java Pyramid 5 Example This Java Pyramid example shows how to generate pyramid or triangle like given below using for loop. 12345 1234 123 12 1 */ public class JavaPyramid5 { public static void main(String[] args) { for(int i=5; i>0 ;i--){ for(int j=0; j < i; j++){ System.out.print(j+1); } } } } /* Output of the example would be 12345 1234 123 12 1 */ System.out.println("");
11
/* * Like any other Java statements, switch statements * can also be nested in each other as given in * below example. */
int i = 0; int j = 1;
12
default:
break;
} }
13
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CalculateCircleAreaExample { public static void main(String[] args) { int radius = 0; System.out.println("Please enter radius of a circle"); try { //get the radius from console BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); radius = Integer.parseInt(br.readLine()); } //if invalid value was entered catch(NumberFormatException ne) { System.out.println("Invalid radius value" + ne); System.exit(0); } catch(IOException ioe) { System.out.println("IO Error :" + ioe); System.exit(0); } /* * Area of a circle is * pi * r * r * where r is a radius of a circle. */ //NOTE : use Math.PI constant to get value of pi double area = Math.PI * radius * radius; } } /* Output of Calculate Circle Area using Java Example would be System.out.println("Area of a circle is " + area);
14
*/
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class JavaFactorialUsingRecursion { public static void main(String args[]) throws NumberFormatException, IOException{ System.out.println("Enter the number: "); //get input from the user BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a = Integer.parseInt(br.readLine()); //call the recursive function to generate factorial int result= fact(a); System.out.println("Factorial of the number is: " + result);
static int fact(int b) { if(b <= 1) //if the number is 1 then return 1 return 1; else //else call the same function with the value - 1 return b * fact(b-1); } } /* Output of this Java example would be Enter the number: 5 Factorial of the number is: 120 */
15
System.out.println("Enter Number:"); int as= Integer.parseInt (keyboard.readLine()); System.out.println("Enter X:"); int x= Integer.parseInt (keyboard.readLine());
int y = 0;
16
System.out.println(""); } } }
0 1 3 6 2 4 7 5 8 12 9 13 14
10 11
----------------------------------------------
17
0 2 6 4 8 10 16 24 18 26 28
12 14 20 22
----------------------------------------------
0 3 9 6 12 15 24 36 27 39 42
18 21 30 33 */
18
19
20
Program 16
/* Write a program that will read a float type value from the print the following output. ->Small Integer not less than the number. ->Given Number. ->Largest Integer not greater than the number. */ class ValueFormat{ public static void main(String args[]){ double i = 34.32; //given number System.out.println("Small Integer not greater than the number : "+Math.ceil(i)); System.out.println("Given Number : "+i); System.out.println("Largest Integer not greater than the number : "+Math.floor(i)); } keyboard and
Program 17 - Write a program to generate 5 Random nos. between 1 to 100, and it should not follow with decimal point.
class RandomDemo{ public static void main(String args[]){ for(int i=1;i<=5;i++){ System.out.println((int)(Math.random()*100)); } }
21
Program 18 - Write a program to display a greet message according to Marks obtained by student.
class SwitchDemo{ public static void main(String args[]){ int marks = Integer.parseInt(args[0]); as command line argument. switch(marks/10){ case 10: case 9: case 8: System.out.println("Excellent"); break; case 7: System.out.println("Very Good"); break; case 6: System.out.println("Good"); break; case 5: System.out.println("Work Hard"); break; case 4: System.out.println("Poor"); break; case 3: //take marks
22
case 2: case 1: case 0: System.out.println("Very Poor"); break; default: System.out.println("Invalid value Entered"); } } }
23
result = 1; while(temp > 0){ result = result * temp; temp--; } System.out.println("Product of Digit for "+num+" is : "+result); } }
Program 20 - Write a program to find sum of all integers greater than 100 and less than 200 that are divisible by 7
class SumOfDigit{ public static void main(String args[]){ int result=0; for(int i=100;i<=200;i++){ if(i%7==0) result+=i; } System.out.println("Output of Program is : "+result); } }
24
Output - 1 2 3 4 5 */ class Join{ public static void main(String args[]){ int num = Integer.parseInt(args[0]); String result = " "; for(int i=1;i<=num;i++){ result = result + i + " "; } System.out.println(result); } }
25
26
Program 24 - Write a program to convert given no. of days into months and days.(Assume that each month is of 30 days)
Example : Input - 69 Output - 69 days = 2 Month and 9 days */ class DayMonthDemo{ public static void main(String args[]){ int num = Integer.parseInt(args[0]); int days = num%30; int month = num/30; System.out.println(num+" days = "+month+" Month and "+days+" days"); } }
27
*/ class InvertTriangle{ public static void main(String args[]){ int num = Integer.parseInt(args[0]); while(num > 0){ for(int j=1;j<=num;j++){ System.out.print(" "+num+" "); } System.out.print("\n"); num--; } } }
28
check = check + (int)Math.pow(remainder,3); num = num / 10; } if(check == n) System.out.println(n+" is an Armstrong Number"); else System.out.println(n+" is not a Armstrong Number"); } }
class SwitchCaseDemo{ public static void main(String args[]){ try{ int num = Integer.parseInt(args[0]); int n = num; //used at last time check int reverse=0,remainder; while(num > 0){ remainder = num % 10; reverse = reverse * 10 + remainder; num = num / 10; }
29
String result=""; //contains the actual output while(reverse > 0){ remainder = reverse % 10; reverse = reverse / 10; switch(remainder){ case 0 : result = result + "Zero "; break; case 1 : result = result + "One "; break; case 2 : result = result + "Two "; break; case 3 : result = result + "Three "; break; case 4 : result = result + "Four "; break; case 5 : result = result + "Five "; break; case 6 : result = result + "Six "; break;
30
case 7 : result = result + "Seven "; break; case 8 : result = result + "Eight "; break; case 9 : result = result + "Nine "; break; default: result=""; } } System.out.println(result); }catch(Exception e){ System.out.println("Invalid Number Format"); } } }
31
public static void main(String args[]){ int num = Integer.parseInt(args[0]); double result = 0.0; while(num > 0){ result = result + (double) 1 / num; num--; } System.out.println("Output of Harmonic Series is "+result); } }
32
Program 29 - Write a program to find average of consecutive N Odd no. and Even no.
class EvenOdd_Avg{ public static void main(String args[]){ int n = Integer.parseInt(args[0]); int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0; while(n > 0){ if(n%2==0){ cntEven++; sumEven = sumEven + n; } else{ cntOdd++; sumOdd = sumOdd + n; } n--; } int evenAvg,oddAvg; evenAvg = sumEven/cntEven; oddAvg = sumOdd/cntOdd; System.out.println("Average of first N Even no is "+evenAvg); System.out.println("Average of first N Odd no is "+oddAvg);
} }
33
34
35
36
0 1 0 1 0 1 0 1 0 1 */ class Output2{ public static void main(String args[]){ for(int i=1;i<=4;i++){ for(int j=1;j<=i;j++){ System.out.print(((i+j)%2)+" "); } System.out.print("\n"); } } }
37