Pratical NO 5

You might also like

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

Practical No.

5
Name & Roll no Dhanishtha-22202C0002
Title of Experiment Develop programs to demonstrate use of looping statement
‘for’
Resources required Hardware: -Laptop Software: MS Word

Date of Performance

Date of Submission

Marks out of 10

Signature

1. Develop a program to print command line argument using for loop


public class oddeven
{
public static void main(String key[])
{
int x=Integer.parseInt(key[0]);
if (x%2 ==0)
{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number");
}}}
2. Write any program using if conditions with for loop
public class EvenNumbersWithForLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the limit for even numbers: ");
int limit = scanner.nextInt();
System.out.println("Even numbers up to " + limit + ":");
for (int i = 0; i <= limit; i++) {
if (i % 2 == 0) {
System.out.print(i + " ");
}}}}

3. Write any program to display pyramids of stars/patterns using


increment/decrement
public class StarPyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows for the pyramid: ");
int rows = scanner.nextInt();
System.out.println("Pyramid of stars:");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println(); // Blank line for separation
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}}}

Conclusion : We learn to use of looping statement ‘for’

You might also like