0% found this document useful (0 votes)
33 views12 pages

Java Star Pattern Program

The document contains code snippets for 6 Java programs: 1) A program to print a star pattern using nested for loops. 2) A program to add two numbers input by the user. 3) A program to count even and odd numbers input by the user. 4) A program to reverse a string input by the user. 5) A program demonstrating method overloading in Java. 6) A program using string methods like toUpperCase(), toLowerCase(), and trim().

Uploaded by

Anshul Jaiswal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views12 pages

Java Star Pattern Program

The document contains code snippets for 6 Java programs: 1) A program to print a star pattern using nested for loops. 2) A program to add two numbers input by the user. 3) A program to count even and odd numbers input by the user. 4) A program to reverse a string input by the user. 5) A program demonstrating method overloading in Java. 6) A program using string methods like toUpperCase(), toLowerCase(), and trim().

Uploaded by

Anshul Jaiswal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 1:- Write a program to print star pattern.

Code:-
package star_pattern;

public class StarPattern


{

public static void main(String args[])


{
for (int row = 5; row >= 1; --row)
{
for (int col = 1; col <= row; ++col)
{
[Link]("*");
}
[Link]();
}
}
}

1
Output:-

2
Program 2:- Write a program to add two numbers from
user.
Code:-
package addition;

import [Link];

public class Addition {


public static void main(String[] arg)
{
int a,b,c;
Scanner sc=new Scanner([Link]);
[Link]("Enter first number");
a=[Link]();
[Link]("Enter second number");
b=[Link]();
c=addition(a,b);
[Link](" Addition of two numbers is : "+c);
}
static int addition(int x,int y)
{
return x+y;
}
}

3
Output:-

4
Program 3:- Write a program to count even and odd
numbers.
Code:-
package odd_even;

import [Link];

public class EvenOdd {

public static void main(String[] args) {

Scanner reader = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();

if(num % 2 == 0)
[Link](num + " is even");
else
[Link](num + " is odd");
}
}

5
Output:-

6
Program 4:- Write a program to make reverse string in
java.
Code:-
package reverse;

import [Link];

public class ReverseString {


public static void main(String[] arg)
{
ReverseString rev=new ReverseString();
Scanner sc=new Scanner([Link]);
[Link]("Enter a string : ");
String str=[Link]();
[Link]("Reverse of a String is : "+[Link](str));
}
static String reverse(String s)
{
String rev="";
for(int j=[Link]();j>0;--j)
{
rev=rev+([Link](j-1));
}
return rev;
}

7
}

Output:-

8
Program 5:- Write a program for method overloading in
java.
Code:-
package overloading;

public class Overloading {


// Java program to demonstrate working of method
// overloading in Java.

// Overloaded sum(). This sum takes two int parameters


public int sum(int x, int y)
{
return (x + y);
}

// Overloaded sum(). This sum takes three int parameters


public int sum(int x, int y, int z)
{
return (x + y + z);
}

// Overloaded sum(). This sum takes two double parameters


public double sum(double x, double y)
{
return (x + y);

9
}

// Driver code
public static void main(String args[])
{
Overloading s = new Overloading();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}

Output:-

10
Program 6:- Write a program to show the use of
touppercase() to tolowercase() & trim().
Code:-
package StringMethods;

import [Link];

public class StringMethods {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a String ");


String userInputString = [Link]();

//print uppercase string


[Link]("Uppercase string "+
[Link]());

//print lowercase string


[Link]("Lowercase string
"+[Link]());

11
//print Trim string
[Link]("trim "+ [Link]());
}
}

Output:-

12

Common questions

Powered by AI

In the reversal string program, using a mutable variable (a String object appended with characters) enables efficient building of a reversed string during iteration. This approach is straightforward but can be less efficient in terms of memory and performance as strings are immutable, which means each concatenation creates a new string object. A better alternative might be using a StringBuilder, which is mutable, to concatenate strings efficiently .

The star pattern program demonstrates the use of nested loops, a fundamental iteration concept in Java. The outer loop iterates over the rows of stars, while the inner loop prints stars for each column in the current row. This decrementing pattern (5, 4, 3, 2, 1) showcases how loop control variables can decrement to achieve a desired pattern .

The Scanner class is advantageous for user input in Java due to its ability to parse primitive types and strings using regular expressions. It simplifies reading console input and provides methods for specific data types like nextInt() and nextLine(). These capabilities enable convenient and efficient handling of diverse input formats, improving user-interface interactions and data handling .

The primary method used for obtaining user input in the addition program is the Scanner class. It functions by creating a Scanner object associated with System.in, which reads user input from the console. The program uses methods like nextInt() to parse integers entered by the user .

Method overloading is demonstrated in the program through several sum() methods with different parameter types and counts. It highlights polymorphism by allowing methods with the same name to perform different operations based on signature changes (e.g., different parameter types or counts). This facilitates more readable and organized code while reusing method names for related functionality .

In the star pattern program, nested loops are used in a decrementing fashion, with the outer loop reducing the number of rows and an inner loop managing the number of stars per row, creating a triangular pattern. In contrast, the even/odd checker uses no nested loops, as it simply evaluates a single condition applied to user input. This contrast illustrates how nested loops can efficiently build complex output patterns versus straightforward conditional checks in other scenarios .

Method overloading provides the benefit of simplifying code by allowing the use of a single method name for related operations, enhancing readability and organization. However, it could introduce maintenance challenges, as developers need to ensure correct parameter matching, which can lead to potential confusion or errors if parameter types or counts are not clearly understood or documented .

In the Java program, toUpperCase() and toLowerCase() methods convert the given string into uppercase and lowercase letters, respectively, whereas the trim() method is used to remove leading and trailing whitespace. These methods simplify string formatting and cleanup, crucial for uniformity in data processing and presentation .

The reverse string program inverts a string by iterating over its length in reverse order, concatenating characters to form a new reversed string. This process uses direct character index manipulation via the charAt method within a loop. This approach is significant as it allows access to each character directly by its position, providing precise control over the order of characters in the resultant string .

The modulus operator (%) is crucial as it calculates the remainder of the division of one number by another. In the even/odd program, it is used to determine if a number is even by checking if the remainder when dividing by 2 is zero, thereby classifying the number as even; otherwise, it is odd .

You might also like