0% found this document useful (0 votes)
104 views

Program 1:-Write A Program To Print Star Pattern. Code

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)
104 views

Program 1:-Write A Program To Print Star Pattern. Code

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
You are on page 1/ 12

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)
{
System.out.print("*");
}
System.out.println();
}
}
}

1
Output:-

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

import java.util.Scanner;

public class Addition {


public static void main(String[] arg)
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
c=addition(a,b);
System.out.println(" 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 java.util.Scanner;

public class EvenOdd {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = reader.nextInt();

if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}

5
Output:-

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

import java.util.Scanner;

public class ReverseString {


public static void main(String[] arg)
{
ReverseString rev=new ReverseString();
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string : ");
String str=sc.nextLine();
System.out.println("Reverse of a String is : "+rev.reverse(str));
}
static String reverse(String s)
{
String rev="";
for(int j=s.length();j>0;--j)
{
rev=rev+(s.charAt(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();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}

Output:-

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

import java.util.Scanner;

public class StringMethods {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a String ");


String userInputString = scanner.nextLine();

//print uppercase string


System.out.println("Uppercase string "+
userInputString.toUpperCase());

//print lowercase string


System.out.println("Lowercase string
"+userInputString.toLowerCase());

11
//print Trim string
System.out.println("trim "+ userInputString.trim());
}
}

Output:-

12

You might also like