HOW TO PRINT AGE PATTEN IN JAVA PROGRAM||WRITE AGE CODE|| SINGHANIA CODER.

HOW TO PRINT AGE PATTEN IN JAVA PROGRAM||WRITE AGE CODE|| SINGHANIA CODER.


To print the age pattern in Java, I'll assume that you want a pattern or code that prints someone's age, or displays a pattern based on age (for example, age printed in a specific format). I'll create a simple Java program to demonstrate how to print a person's age, and I'll also show a basic example of a pattern based on age.



AGE PROGRAM...


import java.util.Scanner;

public class age {
    public static void main(String[] args) {
        System.out.println("ENTRE YOUR AGE");
        Scanner in = new Scanner(System.in);
        int age =in.nextInt();

        if(age<=0){
            System.out.println(" invalid input");
        }else
        if(age >=1 && age <= 5){
            System.out.println(" infant child");
        }else
        if( age >=6 && age <= 17){
            System.out.println("child");
        }else
        if( age >=18 && age <=50 ){
            System.out.println(" adults");

        }else
        if(age >=51 && age <=100){
            System.out.println("Old man");
        }
    }
}



OTHER WAY....


To print the age pattern in Java, I'll assume that you want a pattern or code that prints someone's age, or displays a pattern based on age (for example, age printed in a specific format). I'll create a simple Java program to demonstrate how to print a person's age, and I'll also show a basic example of a pattern based on age.

Example 1: Print a person's age

This simple Java program will ask for the user’s age and print it.

import java.util.Scanner;
public class AgePattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user to enter their age
System.out.print("Enter your age: ");
int age = scanner.nextInt();
// Print the age
System.out.println("Your age is: " + age);
}
}

Example 2: Print a pattern based on age (Triangle pattern)

This example will print a simple triangle pattern based on the entered age. For example, if the user enters 5, the pattern will print 5 rows, with each row having an increasing number of stars.

import java.util.Scanner;
public class AgePattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user to enter their age
System.out.print("Enter your age: ");
int age = scanner.nextInt();
// Print a pattern based on age
for (int i = 1; i <= age; i++) {
// Print stars in each row
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Move to the next line
System.out.println();
}
}
}

Example 3: Print age in a specific format

If you want a more complex pattern or a different way to represent the age, such as printing the digits of the age in a pattern, here's another approach.

import java.util.Scanner;

public class AgePattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Ask the user to enter their age
        System.out.print("Enter your age: ");
        int age = scanner.nextInt();
        
        // Convert the age into a string to print each digit
        String ageStr = String.valueOf(age);
        
        // Print the age in a pattern
        for (int i = 0; i < ageStr.length(); i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(ageStr.charAt(i) + " ");
            }
            System.out.println();
        }
    }
}

Example 4: Display Age Pattern Using ASCII Art (Optional)

You can also create more complex patterns with ASCII characters. Here's a simple example for an "age pyramid" using your age.

import java.util.Scanner;

public class AgePattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Ask the user to enter their age
        System.out.print("Enter your age: ");
        int age = scanner.nextInt();
        
        // Print an age pyramid pattern
        for (int i = 1; i <= age; i++) {
            for (int j = i; j < age; j++) {
                System.out.print(" "); // Print spaces to align
            }
            for (int k = 1; k <= i; k++) {
                System.out.print(i + " "); // Print age as a pattern
            }
            System.out.println();
        }
    }
}

Explanation of Code

  • Scanner: Used to take input from the user.
  • Loops: Used to print patterns in various formats like stars, numbers, or specific age-based patterns.

Running the Code

  1. Compile the code: javac AgePattern.java
  2. Run the program: java AgePattern

This will print the pattern or age in the format you wish. Let me know if you have a specific pattern in mind, and I can help you adjust the code accordingly.