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

Bufferzero

The document contains various Java programs demonstrating fundamental programming concepts such as sorting arrays, encapsulation, calculating sums of even and odd numbers, checking for leap years, generating Fibonacci series, swapping numbers, checking for palindromes and prime numbers, finding the second largest number in an array, printing array elements, and determining string lengths.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Bufferzero

The document contains various Java programs demonstrating fundamental programming concepts such as sorting arrays, encapsulation, calculating sums of even and odd numbers, checking for leap years, generating Fibonacci series, swapping numbers, checking for palindromes and prime numbers, finding the second largest number in an array, printing array elements, and determining string lengths.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Sorting

public class SortAsc


{
public static void main(String[] args)
{

int [] arr = {5, 2, 8, 7, 1};


int temp = 0;
for (int i = 0; i < arr.length; i++)
{
for (int j = i+1; j < arr.length; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

System.out.println("Elements of array sorted in ascending order: ");


for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}

Encapsulation in Java

class Person {
// Encapsulating the name and age
private String name;
private int age;

public String getName()


{
return name;
}

public void setName(String name)


{
this.name = name;
}

public int getAge()


{
return age;
}

public void setAge(int age)


{
this.age = age;
}
}

public class Main {

public static void main(String[] args)


{

Person person = new Person();


person.setName("John");
person.setAge(30);

System.out.println("Name: " + person.getName());


System.out.println("Age: " + person.getAge());
}
}

Program to find sum of elements in a given array


class array_sum
{
public static void main(String[] args)
{
int[]arr= {5,8,9,7,6,2};
int sum=0;
for(int i=0;i<arr.length;i++)
{
sum=sum+arr[i];
}
System.out.println(sum);
}
}
Even num sum in array
class even_array_sum
{
public static void main(String[] args)
{
int[]arr= {5,8,9,7,6,2};
int sum=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]%2==0)
{
sum=sum+arr[i];
}
System.out.println(sum);
}
}
}

Odd num sum in array


class even_array_sum
{
public static void main(String[] args)
{
int[]arr= {5,8,9,7,6,2};
int sum=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]%2==1)
{
sum=sum+arr[i];
}
System.out.println(sum);
}
}
}

Java program to find if the given number is a leap year?


import java.util.Scanner;
public class LeapYear
{
public static void main(String[] args)
{
int year;

System.out.println("Enter an Year :: ");

Scanner sc = new Scanner(System.in);

year = sc.nextInt();

if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))

System.out.println("Specified year is a leap year");

else
System.out.println("Specified year is not a leap year");
}
}

Write a program to display 20 Fibonacci series in java

public class Fibonacci


{
public static void main(String[] args)
{
int n = 20, firstTerm = 0, secondTerm = 1;
for (int i = 1; i <= n; ++i)
{
System.out.print(firstTerm + ", ");

int nextTerm = firstTerm + secondTerm;


firstTerm = secondTerm;
secondTerm = nextTerm;
}

}
Program to swap two numbers without using the third variable

X= 25 (First number), Y= 23 (second number)


Swapping Logic:
X = X + Y = 25 +23 = 48
Y = X - Y = 48 - 23 = 25
X = X -Y = 48 - 25 = 23
and the numbers are swapped as X =23 and Y =25.
Using scanner class
class Swap
{
public static void main(String a[])
{
System.out.println("Enter the value of x and y");

Scanner sc = new Scanner(System.in);


int x = sc.nextInt();
int y = sc.nextInt();

System.out.println("before swapping numbers: "+x +" "+ y);

x = x + y;
y = x - y;
x = x - y;
System.out.println("After swapping: "+x +" " + y);
}
}

Without using scanner class


class Swap
{
public static void main(String a[])
{

int x = 25;
int y = 23;

System.out.println("before swapping numbers: "+x +" "+ y);


x = x + y;
y = x - y;
x = x - y;
System.out.println("After swapping: "+x +" " + y);
}
}

Palindrome Program in Java

class PalindromeExample
{
public static void main(String args[])
{
int r,sum=0,temp;
int n=454;

temp=n;

while(n>0)
{
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}

if(temp==sum)
System.out.println("palindrome number ");

else
System.out.println("not palindrome");
}

Prime Number Program in Java

Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or
itself only.
public class prime_or_not
{
public static void main(String[] args)
{
int n = 10;
int count = 0;

for (int j = 1; j <= n / 2; j++)


{
if (n % j == 0)
{
count++;
}
}

if (count > 1)
{
System.out.println("The number is not prime");
}
else
{
System.out.println("The number is prime");
}
}
}

Java program to find the 2nd largest number in an array

public class second_large


{
public static void main(String[] args)
{
int temp;
int array[] = {10, 20, 25, 63, 96, 57};

for(int i = 0; i<array.length; i++ )


{
for(int j = i+1; j<array.length; j++)
{

if(array[i]>array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("Second largest number is:: "+array[array.length-2]);
}
}

Program to print the elements of an array

public class PrintArray


{
public static void main(String[] args)
{

int [] arr = {1, 2, 3, 4, 5};


System.out.println("Elements of given array: ");

for (int i = 0; i < arr.length; i++)


{
System.out.print(arr[i] + " ");
}
}
}

java program to find the length of a string


public class LengthExample
{
public static void main(String args[])
{
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());
System.out.println("string length is: "+s2.length());
}
}

You might also like