0% found this document useful (0 votes)
6 views5 pages

Java Programs Basic Updated

The document contains five Java programs demonstrating various programming concepts. Program 1 accepts user input and prints a greeting, Program 2 checks if a string is a palindrome, Program 3 counts vowels and consonants in a string, Program 4 showcases method overloading, and Program 5 illustrates array and vector operations. Each program includes example outputs to illustrate their functionality.

Uploaded by

hrishiyuga
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)
6 views5 pages

Java Programs Basic Updated

The document contains five Java programs demonstrating various programming concepts. Program 1 accepts user input and prints a greeting, Program 2 checks if a string is a palindrome, Program 3 counts vowels and consonants in a string, Program 4 showcases method overloading, and Program 5 illustrates array and vector operations. Each program includes example outputs to illustrate their functionality.

Uploaded by

hrishiyuga
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: Accept Input using Scanner and Print Output

import [Link];

public class InputExample {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Enter your age: ");
int age = [Link]();
[Link]("Hello " + name + ", you are " + age + " years old.");
[Link]();
}
}

Output:
Enter your name: Somesh
Enter your age: 18
Hello Somesh, you are 18 years old.
Program 2: Check Whether String is Palindrome
import [Link];

public class PalindromeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String rev = new StringBuilder(str).reverse().toString();
if ([Link](rev))
[Link](str + " is a Palindrome.");
else
[Link](str + " is not a Palindrome.");
[Link]();
}
}

Output:
Enter a string: radar
radar is a Palindrome.
Program 3: Count Number of Vowels and Consonants
import [Link];

public class CountVowelsConsonants {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
int vowels = 0, consonants = 0;
for (char ch : [Link]()) {
if ([Link](ch)) {
if ("aeiou".indexOf(ch) != -1)
vowels++;
else
consonants++;
}
}
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);
[Link]();
}
}

Output:
Enter a string: Hello World
Vowels: 3
Consonants: 7
Program 4: Demonstrate Method Overloading
public class MethodOverloadingExample {
static int add(int a, int b) {
return a + b;
}

static double add(double a, double b) {


return a + b;
}

static String add(String a, String b) {


return a + b;
}

public static void main(String[] args) {


[Link]("Sum of integers: " + add(10, 20));
[Link]("Sum of doubles: " + add(5.5, 3.2));
[Link]("Concatenation of strings: " + add("Hello ", "World"));
}
}

Output:
Sum of integers: 30
Sum of doubles: 8.7
Concatenation of strings: Hello World
Program 5: Demonstrate Array and Vector Operations
import [Link].*;

public class ArrayVectorExample {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
[Link]("Array elements:");
for (int num : arr) {
[Link](num + " ");
}

Vector<Integer> vec = new Vector<>();


[Link](10);
[Link](20);
[Link](30);
[Link]("\n\nVector elements:");
for (int num : vec) {
[Link](num + " ");
}
}
}

Output:
Array elements:
1 2 3 4 5

Vector elements:
10 20 30

You might also like