Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1. .

Programtoprintthefollowingtriangleofnumbers

public class myClass


{
public static void main(String[] args)
{
int i, j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
System.out.print(j);
System.out.println();
}
}
}

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;

public class AreaOfRctangle {


private static Scanner sc;

public static void main(String[] args) {


double width, height, Area, Perimeter;
sc = new Scanner(System.in);

System.out.println(" Please Enter the Width of a Rectangle = ");


width = sc.nextDouble();
System.out.println(" Please Enter the Height of a Rectangle = ");
height = sc.nextDouble();

Area = width * height;


Perimeter = 2 * (width + height);

System.out.format(" The Area of a Rectangle = %.2f\n",Area);


System.out.format(" The Perimeter of a Rectangle = %.2f\n", Perimeter);
}
}
PART A
1. ProgramtoassigntwointegervaluestoXandY.Usingthe“if‟statementth
eoutputoftheprogramshoulddisplayamessage whether Xis greaterthan Y.

1. Java Program to Find largest of Two Numbers

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);

System.out.print(" Please Enter the First Number : ");


number1 = sc.nextInt();

System.out.print(" Please Enter the Second Number : ");


number2 = sc.nextInt();

if(number1 > number2)


{
System.out.println("\n The Largest Number = " + number1);
}
else if (number2 > number1)
{
System.out.println("\n The Largest Number = " + number2);
}
else
{
System.out.println("\n Both are Equal");
}
}
}

2. Programtolistthefactorialofthenumbers1to10.Tocalculate the factorial value, use


while loop. (Hint: Fact of 4 =4*3*2*1)

public class Factorial{

public static void main(String[] args) {

//We will find the factorial of this number


int number = 10;
long fact = 1;
int i = 1;
while(i<=number)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}

3. Program to find the area and circumference of the circle by accepting the radius
from the user.
import java.util.Scanner;

public class CodesCracker


{
public static void main(String[] args)
{
float r, area, circum;
Scanner s = new Scanner(System.in);

System.out.print("Enter the Radius of Circle: ");


r = s.nextFloat();

area = (float)(3.14*r*r);
circum = (float)(2*3.14*r);
System.out.println("\nArea = " +area);
System.out.println("Circumference = " +circum);
}
}

You might also like