Académique Documents
Professionnel Documents
Culture Documents
class Maxof2
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
if(i > j)
Else
}}
PROGRAM-2
class Minof2{
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
}}
PROGRAM-3
/* Write a program that will read a float type value from the keyboard and print the following output.
->Given Number.
*/
class ValueFormat
PROGRAM-4
*/
class RandomDemo
for(int i=1;i<=5;i++){
System.out.println((int)(Math.random()*100));
} }}
PROGRAM-5
*/
class SwitchDemo
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:
case 2:
case 1:
case 0:
System.out.println("Very Poor");
break;
default:
} }}
PROGRAM-6
class Sum_Product_ofDigit{
while(temp>0){
temp--;
temp = num;
result = 1;
temp--;
}}
PROGRAM-7
class Factorial
int result = 1;
while(num>0){
num--;
}
PROGRAM-8
class Reverse
while(num>0)
remainder = num%10;
num = num/10;
}
PROGRAM-9
Example :
Input - 8
Output - 1 1 2 3 5 8 13 21
*/
class Fibonacci{
System.out.println("*****Fibonacci Series*****");
for(int i=1;i<=num;i++){
f1 = f2;
f2 = f3;
f3 = f1 + f2;
} }}
PROGRAM-10
/* Write a program to find sum of all integers greater than 100 and
class SumOfDigit{
int result=0;
for(int i=100;i<=200;i++){
if(i%7==0)
result+=i;
}}
PROGRAM-11
Example:
Input - 5
Output - 1 2 3 4 5 */
class Join
for(int i=1;i<=num;i++){
System.out.println(result);
}}
PROGRAM-12
class MultiplicationTable{
System.out.println("*****MULTIPLICATION TABLE*****");
for(int i=1;i<=num;i++){
for(int j=1;j<=num;j++){
} System.out.print("\n"); } }}
PROGRAM-13
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);
}}
PROGRAM-14
/* Write a program to convert given no. of days into months and days.
Example :
Input - 69
class DayMonthDemo{
}}
PROGRAM-15
eg:
22
333
class Triangle{
for(int i=1;i<=num;i++){
for(int j=1;j<=i;j++){
System.out.print("\n");
} }}
PROGRAM-16
Example:
Input - 5
Output :
55555
4444
333
22
*/
class InvertTriangle{
for(int j=1;j<=num;j++){
System.out.print("\n");
num--;
} }}
PROGRAM-17
Example :
Input - 153
class Armstrong{
int check=0,remainder;
if(check == n)
else
}}
PROGRAM-18
class PrimeNo{
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
flag = 1;
break;
} }
if(flag==0)
}}
PROGRAM-19
Example :
class Palindrome{
int reverse=0,remainder;
if(reverse == n)
else
}}
PROGRAM-20
Example :
Input - 124
class SwitchCaseDemo{
try{
int reverse=0,remainder;
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
break;
case 6 :
break;
case 7 :
break;
case 8 :
break;
case 9 :
break;
default:
result="";
} }
System.out.println(result);
catch(Exception e)
} }}
PROGRAM-21
Example :
Input - 5
class HarmonicSeries{
num--;
}}
PROGRAM-22
/*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;
}}
PROGRAM-23
23
456
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");
} }}
PROGRAM-24
10
101
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");
} }}
PROGRAM-25
24
369
class Output3{
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print((i*j)+" ");
System.out.print("\n");
} }}
Program-26
( This program Demonstrates the proper use of Date functionality in common java programming
scenarios )
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
*/
Calendar c1 = Calendar.getInstance();
System.out.println("c1.getTime() : "+c1.getTime());
System.out.println("c1.get(Calendar.YEAR): " + c1.get(Calendar.YEAR));
c1.add(Calendar.DATE,40);
System.out.println();
System.out.println();
roll() does the same thing except you specify if you want to roll up (add 1)
or roll down (substract 1) to the specified Calendar field. The operation only
affects the specified field while add() adjusts other Calendar fields.
See the following example, roll() makes january rolls to december in the same
year while add() substract the YEAR field for the correct result
*/
Calendar c1 = Calendar.getInstance();
c1.set(1999, 0 , 20);
c1.roll(Calendar.MONTH, false);
c1.set(1999, 0 , 20);
c1.add(Calendar.MONTH, -1);
// substract 1 month
System.out.println();
System.out.println();
c1.set(1999, 0 , 20);
c2.set(1999, 0 , 22);
System.out.println();
System.out.println();
c1.set(1999, 6 , 20);
daysInMonths[1] += DateUtility.isLeapYear(year) ? 1 : 0;
System.out.println();
System.out.println();
try {
day = f.format(d1);
}
catch(Exception e) {
e.printStackTrace();
System.out.println();
System.out.println();
String dt = "20011223";
try {
sdf.setLenient(false);
dt1 = sdf.parse(dt);
dt2 = sdf.parse(invalidDt);
catch (ParseException e) {
System.out.println(e.getMessage());
catch (IllegalArgumentException e) {
System.out.println("Invalid date");
}
System.out.println();
System.out.println();
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
System.out.print(fm.format(c1.getTime())+" is ");
if(c1.before(c2)){
}else if(c1.after(c2)){
}else if(c1.equals(c2)){
System.out.println();
System.out.println();
return false;
addToDate();
subToDate();
daysBetween2Dates(); //The "right" way would be to compute the julian day number
of both dates and then do the substraction.
daysInMonth();
validateAGivenDate();
compare2Dates();
getDayofTheDate();
}
JAVA String Utility
( This program Demonstrates the proper use of Number Formats in common java programming
scenarios )
String Utility
/*
* NumberUtility.java
* Source: http://www.freejavaguide.com
*/
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
*/
/**
*/
return newFormat.format(bdNumber);
/* Method takes Object as parameter and removes commas from the parameter */
try {
if (inputNo.length() > 0) {
inputNo.deleteCharAt(inputNo.indexOf(","));
} else {
return 0.0;
return Double.parseDouble(inputNo.toString());
} catch (NumberFormatException e) {
return 0.0;
/* Some times its required to have a fixed set of decimal places for a
* number. We can set that by changing the precision number for a particular
*/
int precision) {
return "0.0";
if (bigDecimalString.contains(".")) {
afterDecimal = bigDecimalString.substring(bigDecimalString
.indexOf(".") + 1);
newFormattedString = bigDecimalString;
}
} else if (afterDecimal.length() > precision) {
newFormattedString = bigDecimalString.substring(0,
bigDecimalString.length() - length);
if (precision == 0) {
newFormattedString = newFormattedString.substring(0,
newFormattedString.indexOf("."));
} else {
newFormattedString = bigDecimalString;
} else {
if (precision > 0)
else
newFormattedString = bigDecimalString;
return newFormattedString;
System.out.println(NumberUtility.changeToDecimalFormat(new Integer(intVar)));
System.out.println(NumberUtility.changeToDecimalFormat(new Double(doubleVar)));
System.out.println(NumberUtility.changeToDecimalFormat(new Float(floatVar)));
System.out.println(NumberUtility.removeCommasFromNumber(commaString));
System.out.println(NumberUtility.changeToRequiredDecimals(bdNumber.toString(), 8));
}
Program-27
if (string1 == string2) {
} else {
Program-28
System.out.println(s);
}
Program-29
System.out.println(s);
Program-30
System.out.println(s);
Program-31
System.out.println(s);
}
Program-32
System.out.println(s);
Program-33
System.out.println(s);
Program-34
char ch = '\u1234';
System.out.println(ch);
}
Program-35
System.out.println(s);
Program-36
System.out.println(s);