0% found this document useful (0 votes)
38 views5 pages

Java Lab Report

The document contains examples of Java programs to print patterns like a diamond, pyramid, right angle triangle with numbers. It also includes programs to calculate area and perimeter of basic shapes by taking user input for dimensions. Some examples display multiplication tables and find sum of natural, even and odd numbers in a range.

Uploaded by

tnandani524
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
Download as odt, pdf, or txt
0% found this document useful (0 votes)
38 views5 pages

Java Lab Report

The document contains examples of Java programs to print patterns like a diamond, pyramid, right angle triangle with numbers. It also includes programs to calculate area and perimeter of basic shapes by taking user input for dimensions. Some examples display multiplication tables and find sum of natural, even and odd numbers in a range.

Uploaded by

tnandani524
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1/ 5

9. Write a program in Java to display the pattern like a diamond.

*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
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("*");
}

// Move to the next line


System.out.println();
}

// Lower half of the diamond


for (int i = n- 1; i >= 1; 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("*");
}

// Move to the next line


System.out.println();
}
}
}
8. Write a program in Java to make such a pattern like a pyramid with a number which will
repeat the number in the same row.
1
22
333
4444
import java.util.Scanner;
public class Pattern {
public static void main(String args[]){
int n=4;

for (int i = 1; i <=n; i++) {


// Print spaces
for (int j = 1; j <= n- i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(i+"");
}
// Move to the next line
System.out.println();

}
}
}

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
}

// Move to the next line


System.out.println();
}
}
}
6. Write a program in Java to display the pattern like a right angle triangle with a number.
Test Data
Input number of rows : 10
Expected Output :
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910

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

// find the area of the circle


double area = Math.PI * Math.pow(radius, 2);

System.out.println("The area of the circle is: " + area);


}
}

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

// Calculate the perimeter of the circle


double perimeter = 2 * Math.PI * radius;

System.out.println("The perimeter of the circle is: " + perimeter);


}
}

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;

// Loop to display the tables


for (int i = 1; i <= tables; i++) {
System.out.println("Multiplication Table of " + i);

// Loop to display the table


for (int j = 1; j <= maxNumber; j++) {
int result = i * j;
System.out.println(i + " * " + j + " = " + result);
}

System.out.println();
}
}
}

2. Write a program to display the sum of natural numbers from 1 to 50.


public class Display {
public static void main(String[] args) {
int n = 50; // Upper limit

// finding the sum of natural numbers


int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}

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;

// Calculate the sum of even and odd numbers


for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
sumEven += i; // Add even number to sumEven
} else {
sumOdd += i; // Add odd number to sumOdd
}
}

System.out.println("Sum of even numbers from 1 to " + n + " is: " + sumEven);


System.out.println("Sum of odd numbers from 1 to " + n + " is: " + sumOdd);
}
}

You might also like