Professional Documents
Culture Documents
Java Programs
Java Programs
Programtoprintthefollowingtriangleofnumbers
2. Programtosimplejavaapplication,toprintthemessage,“Welcometo java”
public class HelloWorld {
public static void main(String[] args)
{
System.out.println("\n Welcometo java ");
}
3. Programtodisplaythemonthofayear.Monthsoftheyearshouldbeheldinan array.
import java.util.Calendar;
public class DisplayMonthOfYear {
public static void main(String[] args) {
//create Calendar instance
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
//create an array of months
String[] strMonths = new String[]{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
System.out.println("Current month is : " +
strMonths[now.get(Calendar.MONTH)
);
}
}
4. Programto findtheareaofrectangle.
package Area;
import java.util.Scanner;
import java.util.Scanner;
public class LargestofTwo {
private static Scanner sc;
public static void main(String[] args)
{
int number1, number2;
sc = new Scanner(System.in);
3. Program to find the area and circumference of the circle by accepting the radius
from the user.
import java.util.Scanner;
area = (float)(3.14*r*r);
circum = (float)(2*3.14*r);
System.out.println("\nArea = " +area);
System.out.println("Circumference = " +circum);
}
}