9. Write a program in Java to display the pattern like a diamond.
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
import [Link];
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++) {
[Link](" ");
}
// Print asterisks
for (int k = 1; k <= 2 * i - 1; k++) {
[Link]("*");
}
// Move to the next line
[Link]();
}
// Lower half of the diamond
for (int i = n- 1; i >= 1; i--) {
// Print spaces
for (int j = 1; j <=n - i; j++) {
[Link](" ");
}
// Print asterisks
for (int k = 1; k <= 2 * i - 1; k++) {
[Link]("*");
}
// Move to the next line
[Link]();
}
}
}
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 [Link];
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++) {
[Link](" ");
}
for (int j = 1; j <= i; j++) {
[Link](i+"");
}
// Move to the next line
[Link]();
}
}
}
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 [Link];
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++) {
[Link](number + " ");
number++; // Increment the number
}
// Move to the next line
[Link]();
}
}
}
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 [Link];
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++) {
[Link](j+ " ");
}
[Link]();
}
}
}
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 [Link];
public class Area {
public static void main(String args[]){
Scanner scanner = new Scanner([Link]);
[Link]("height of a triangle");
double height = [Link]();
[Link]("base of a triangle");
double base= [Link]();
//to find area of a triangle
double area= 0.5 * base * height;
[Link]("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 [Link];
public class Area {
public static void main(String args[]){
Scanner scanner = new Scanner([Link]);
[Link](" radius of the circle: ");
double radius = [Link]();
// find the area of the circle
double area = [Link] * [Link](radius, 2);
[Link]("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 [Link];
public class Area {
public static void main(String args[]){
Scanner scanner = new Scanner([Link]);
[Link](" radius of the circle: ");
double radius = [Link]();
// Calculate the perimeter of the circle
double perimeter = 2 * [Link] * radius;
[Link]("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++) {
[Link]("Multiplication Table of " + i);
// Loop to display the table
for (int j = 1; j <= maxNumber; j++) {
int result = i * j;
[Link](i + " * " + j + " = " + result);
}
[Link]();
}
}
}
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;
}
[Link]("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
}
}
[Link]("Sum of even numbers from 1 to " + n + " is: " + sumEven);
[Link]("Sum of odd numbers from 1 to " + n + " is: " + sumOdd);
}
}