Java Lab Report
Java Lab Report
Java Lab Report
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
import java.util.Scanner;
public class Pattern {
public static void main(String args[]){
int n=7;
// Upper half
for (int i = 1; i <=n; i++) {
// Print spaces
for (int j = 1; j <= n- i; j++) {
System.out.print(" ");
}
// Print asterisks
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
// Print asterisks
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
}
}
}
7. Write a program in Java to make such a pattern like right angle triangle with number
increased by 1.
1
23
456
7 8 9 10
import java.util.Scanner;
public class Pattern {
public static void main(String args[]){
int n=4;
int number=1;
//outer loop
// Loop to print the triangle
for (int i = 1; i <= n; i++) {
// Loop to print numbers
for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
number++; // Increment the number
}
import java.util.Scanner;
public class Pattern {
public static void main(String args[]){
int n=10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+ " ");
}
System.out.println();
}
}
}
1. Write a program to find the area of a triangle where height and breadth are taken from
the user. (area=1⁄2(base * height)
import java.util.Scanner;
public class Area {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("height of a triangle");
double height = scanner.nextDouble();
System.out.println("base of a triangle");
double base= scanner.nextDouble();
//to find area of a triangle
double area= 0.5 * base * height;
System.out.println("The area of the triangle is: " + area);
}
2. Write a program to find the area of a circle where radius is taken from the user.
(area=πr^2).
import java.util.Scanner;
public class Area {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print(" radius of the circle: ");
double radius = scanner.nextDouble();
3. write a program to find the perimeter of a circle where radius is taken from the user.
(perimeter=2πr)
import java.util.Scanner;
public class Area {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print(" radius of the circle: ");
double radius = scanner.nextDouble();
2.1. Write a program to display the multiplication table from 1 to 5.(each table must be upto
10).
public class Display {
public static void main(String[] args) {
int tables = 5;
int maxNumber = 10;
System.out.println();
}
}
}
System.out.println("The sum of natural numbers from 1 to " + n + " is: " + sum);
}
}
3. Write a program to display the sum of even and odd numbers from 1 to 10.
public class Display {
public static void main(String[] args) {
int n = 10; // Upper limit
int sumEven = 0;
int sumOdd = 0;