[go: up one dir, main page]

0% found this document useful (0 votes)
12 views4 pages

Sumasoftprooogram

The document contains a collection of Java programs categorized into basic, string-based, number-based, array-based, and sorting/searching programs. Each category includes examples such as printing 'Hello, World!', swapping numbers, reversing strings, checking for prime numbers, and implementing bubble sort. The programs demonstrate fundamental programming concepts and algorithms in Java.

Uploaded by

Yogesh Sabale
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)
12 views4 pages

Sumasoftprooogram

The document contains a collection of Java programs categorized into basic, string-based, number-based, array-based, and sorting/searching programs. Each category includes examples such as printing 'Hello, World!', swapping numbers, reversing strings, checking for prime numbers, and implementing bubble sort. The programs demonstrate fundamental programming concepts and algorithms in Java.

Uploaded by

Yogesh Sabale
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/ 4

1.

Basic Programs

1. Print "Hello, World!"

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

2. Swap two numbers without using a third variable

public class SwapNumbers {


public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping: a = " + a + ", b = " + b);
}
}

3. Check if a number is even or odd

public class EvenOdd {


public static void main(String[] args) {
int num = 7;
if (num % 2 == 0)
System.out.println(num + " is Even");
else
System.out.println(num + " is Odd");
}
}

---

2. String-Based Programs

4. Reverse a string

public class ReverseString {


public static void main(String[] args) {
String str = "Hello";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println("Reversed String: " + reversed);
}
}

5. Check if a string is a palindrome

public class PalindromeString {


public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
if (str.equals(rev))
System.out.println("Palindrome");
else
System.out.println("Not a Palindrome");
}
}

6. Count vowels and consonants in a string

public class CountVowelsConsonants {


public static void main(String[] args) {
String str = "Java";
int vowels = 0, consonants = 0;
str = str.toLowerCase();
for (char c : str.toCharArray()) {
if ("aeiou".indexOf(c) != -1)
vowels++;
else if (Character.isLetter(c))
consonants++;
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
}
}

---

3. Number-Based Programs

7. Check if a number is prime

public class PrimeNumber {


public static void main(String[] args) {
int num = 7;
boolean isPrime = num > 1;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(num + " is " + (isPrime ? "Prime" : "Not Prime"));
}
}

8. Find the factorial of a number

public class Factorial {


public static void main(String[] args) {
int num = 5, fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);
}
}

9. Generate Fibonacci series


public class Fibonacci {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}
}
}

10. Check if a number is an Armstrong number

public class ArmstrongNumber {


public static void main(String[] args) {
int num = 153, sum = 0, temp = num;
while (temp > 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
System.out.println(num + " is " + (sum == num ? "Armstrong" : "Not Armstrong"));
}
}

---

4. Array-Based Programs

11. Find the largest element in an array

public class LargestElement {


public static void main(String[] args) {
int[] arr = {10, 20, 5, 25, 30};
int max = arr[0];
for (int num : arr) {
if (num > max)
max = num;
}
System.out.println("Largest Element: " + max);
}
}

12. Find duplicate elements in an array

import java.util.HashSet;
public class DuplicateElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 5, 1};
HashSet<Integer> set = new HashSet<>();
System.out.print("Duplicate Elements: ");
for (int num : arr) {
if (!set.add(num))
System.out.print(num + " ");
}
}
}

---

5. Sorting and Searching

13. Bubble Sort

import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 1, 4, 2, 8};
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}

14. Binary Search

import java.util.Arrays;
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {2, 3, 4, 10, 40};
int key = 10, left = 0, right = arr.length - 1;
Arrays.sort(arr);
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
System.out.println("Element found at index: " + mid);
return;
} else if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
System.out.println("Element not found");
}
}

You might also like