Professional Documents
Culture Documents
/*
List Even Numbers Java Example
This List Even Numbers Java Example shows how to find and list even
numbers between 1 and any given number.
*/
//define limit
int limit = 50;
/*
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
*/
int number = 5;
/*
* Factorial of any number is! n.
* For example, factorial of 4 is 4*3*2*1.
*/
4
{
factorial = factorial * i;
}
/*
Output of the Factorial program would be
Factorial of a number is 120
*/
This Compare Two Numbers Java Example shows how to compare two numbers
*/
5
System.out.println(num1 + " is less than " + num2);
else{
/*
*/
*/
6
//year we want to check
else
/*
*/
7
series[0] = 0;
series[1] = 1;
/*
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
*/
/*
* 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
8
}
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
*/
//define limit
int limit = 100;
if(i % j == 0){
isPrime = false;
break;
}
}
// print the number
if(isPrime)
System.out.print(i + " ");
}
9
}
}
/*
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
*/
*
**
***
****
*****
*/
/*
Output of the above program would be
*
**
***
****
*****
*/
Program9 – Reversed pyramid using for loops &
decrement operator.
/*
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
*/
System.out.println("");
}
}
}
/*
*/
Statements Example
java program.
*/
11
public class NestedSwitchExample {
/*
* below example.
*/
int i = 0;
int j = 1;
switch(i)
case 0:
switch(j)
case 0:
System.out.println("i is 0, j is 0");
break;
case 1:
System.out.println("i is 0, j is 1");
break;
12
default:
System.out.println("nested default
case!!");
break;
default:
/*
i is 0, j is 1
*/
13
Program11 - Calculate Circle Area using radius
/*
This program shows how to calculate
area of circle using it's radius.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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.
*/
/*
Output of Calculate Circle Area using Java Example would be
14
Please enter radius of a circle
19
Area of a circle is 1134.1149479459152
*/
Program12 - Factorial of a number using recursion
/*
This program shows how to calculate
Factorial of a number using recursion function.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Output of this Java example would be
15
Program13 – pyramid of numbers using for loops
/*
This Java example shows how to generate a pyramid of numbers for given
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
System.out.println("Enter Number:");
System.out.println("Enter X:");
int y = 0;
16
for(int j=1; j <= i ; j++){
System.out.print(y + "\t");
y = y + x;
System.out.println("");
/*
Enter Number:
Enter X:
1 2
3 4 5
6 7 8 9
10 11 12 13 14
----------------------------------------------
17
Enter Number:
Enter X:
2 4
6 8 10
12 14 16 18
20 22 24 26 28
----------------------------------------------
Enter Number:
Enter X:
3 6
9 12 15
18 21 24 27
30 33 36 39 42
*/
18
Program14 – To Find Maximum of Two Numbers.
/*
*/
class Maxoftwo{
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
if(i > j)
else
*/
class Minoftwo{
int i = Integer.parseInt(args[0]);
19
int j = Integer.parseInt(args[1]);
20
Program 16
/* Write a program that will read a float type value from the keyboard and
print the following output.
->Given Number.
*/
class ValueFormat{
class RandomDemo{
for(int i=1;i<=5;i++){
System.out.println((int)(Math.random()*100));
21
}
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:
22
case 2:
case 1:
case 0:
System.out.println("Very Poor");
break;
default:
while(temp>0){
temp--;
temp = num;
23
result = 1;
temp--;
int result=0;
for(int i=100;i<=200;i++){
if(i%7==0)
result+=i;
Input - 5
24
Output - 1 2 3 4 5 */
class Join{
for(int i=1;i<=num;i++){
System.out.println(result);
System.out.println("*****MULTIPLICATION TABLE*****");
for(int i=1;i<=num;i++){
for(int j=1;j<=num;j++){
System.out.print("\n");
25
Program 23 - Write a program to Swap the values
class Swap{
System.out.println("\n***Before Swapping***");
System.out.println("Number 1 : "+num1);
System.out.println("Number 2 : "+num2);
//Swap logic
System.out.println("\n***After Swapping***");
System.out.println("Number 1 : "+num1);
System.out.println("Number 2 : "+num2);
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
class DayMonthDemo{
Input - 5
Output :
5 5 5 5 5
4 4 4 4
3 3 3
2 2
27
*/
class InvertTriangle{
for(int j=1;j<=num;j++){
System.out.print("\n");
num--;
Input - 153
class Armstrong{
int check=0,remainder;
28
check = check + (int)Math.pow(remainder,3);
if(check == n)
else
Input - 124
class SwitchCaseDemo{
try{
int reverse=0,remainder;
29
String result=""; //contains the actual output
switch(remainder){
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
break;
case 6 :
break;
30
case 7 :
break;
case 8 :
break;
case 9 :
break;
default:
result="";
System.out.println(result);
}catch(Exception e){
Input - 5
class HarmonicSeries{
31
public static void main(String args[]){
num--;
32
Program 29 - Write a program to find average of
consecutive N Odd no. and Even no.
class EvenOdd_Avg{
int n = Integer.parseInt(args[0]);
int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;
if(n%2==0){
cntEven++;
sumEven = sumEven + n;
else{
cntOdd++;
sumOdd = sumOdd + n;
n--;
int evenAvg,oddAvg;
evenAvg = sumEven/cntEven;
oddAvg = sumOdd/cntOdd;
33
Program 30 - Display Triangle as follow.
1
2 3
4 5 6
7 8 9 10 ... N */
class Output1{
int c=0;
int n = Integer.parseInt(args[0]);
if(c!=n){
c++;
System.out.print(c+" ");
else
break loop1;
System.out.print("\n");
34
Extra Programs 1 - Write a program to Find whether
number is Prime or Not.
class PrimeNo{
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
flag = 1;
break;
if(flag==0)
35
Program 2 - Write a program to find whether no. is
palindrome or not.
Example :
class Palindrome{
int reverse=0,remainder;
if(reverse == n)
else
36
Program 3 - Display Triangle as follow
1 0
1 0 1
0 1 0 1 */
class Output2{
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