Your ultimate guide

Java/C# _ Java Programs

1. Fibonacci Series:

    The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes like 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... and so on.


Example Program:

public class FibonacciSeries

{

    public static void main(String[] args)

    {

        int x=0,y=1,z;

        System.out.println("How many number you want print");

        Scanner maxi = new Scanner(System.in);

        int maximum = maxi.nextInt();

        System.out.println(x);

        System.out.println(y);

        for(int i=0;i<maximum-2;i++)

        {

            z=x+y;

            System.out.println(z);

            x=y;

            y=z;

        }

    }

}



2. Palindrome Number:

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Examples include "madam", "racecar" and "56765".


Example Program:

public class PalindromeNumber

{

    public static void main(String[] args)

    {

        Scanner pN= new Scanner(System.in);

        //pN = new Scanner(System.in);

        System.out.println("Enter any Number to Check it is Palinfrome or Not:");

        int number=pN.nextInt();

        int x,sum=0,y;

        y=number;

        while(number>0)

        {

            x=number%10;

            sum = (sum*10)+x;

            number = number/10;

         }

        if(y == sum)

        System.out.print("Enter Number is a Polindrome Number");

        else

        System.out.print("Enter Number is Not a Polindrome Number");

    }

}


3. Palindrome String:


Example Program:

public class PalindromeOnString

{

    public static void main(String[] args)

    {

        Scanner in = new Scanner(System.in);

        System.out.println("Enter a string to check if it is a palindrome string or not");

        String originalString = in.nextLine();

        String reverseString="";

        for(int i=originalString.length()-1;i>=0;i--)

        {

            reverseString=reverseString+originalString.charAt(i);

        }

        if(originalString.equals(reverseString))

            System.out.println("Entered string is palindrome");

        else

            System.out.println("Entered string is not palindrome");

    }

}


4. Prime Number:

    A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. Prime numbers are divisible by only 1 and themselves, making them a key concept in number theory and mathematics in general. Examples of prime numbers include 2, 3, 5, 7, 11, and 13.


Example Program:

public class PrimeNumber

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number to check if it is prime number or not ");

        int number= sc.nextInt();

        int count=0;

        for(int i=1;i<=number;i++)

        {

            if(number%i==0)

                count++;

        }

        if(count==2)

            System.out.println(number+" is a prime number");

        else

            System.out.println(number+" is not a prime number");

    }

}


5. Reverse Of a String:

public class ReverseOfaString

{

    public static void main(String[] args)

    {

        String newString="Testing Colleges";

        String reverseString="";

        for(int i=newString.length()-1;i>=0;i--)

        {

            reverseString=reverseString+newString.charAt(i);

        }

        System.out.println("Original String : "+newString);

        System.out.println("Reverse_ String : "+reverseString);

      }

 }


6. Reverse Of a String without changing word position:

public class RevOfStringWithOut {

    public static void main(String[] args) {

        String s = "Testing Colleges";

        String ch[] = s.split(" ");

        System.out.println("Given String : "+s);

        System.out.print ("Reverse of a String : ");

        for (String chr : ch)

        {

            String reverse = "";

            for (int i = chr.length() - 1; i >= 0; i--)

            {

                reverse = reverse + chr.charAt(i);

            }

            System.out.print(reverse + " ");

        }

    }

}

7. Armstrong Number:

    An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. Other examples include:371, 407, 1634, 8208, 9474


Example Program

public class ArmstrongNumber {

    public static void main(String[] args) {

        int a,count=0;

        double arm =0;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number to check if it is Armstrong number or not ");

        int n= sc.nextInt();

        int temp=n,temp2=n;

        // Counting Numbers

        while(n>0)

        {

            n = n/10;

            count++;

        }

        System.out.println("Total Number of Digits : "+count);

        //Performing Operation

        while(temp>0)

        {

            a=temp%10;

            arm=arm+Math.pow(a, count);

            temp=temp/10;

        }

        if(arm==temp2)

            System.out.println(temp2+" is a armstrong number ");

        else

            System.out.println(temp2+" is not a armstrong number ");

    }

}

8. Largest Number:

Example Program 1:

public class LargestNumber {

    public static void main(String args[]) {

        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};

        int largest = numbers[0];

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

            if (numbers[i] > largest) {

                largest = numbers[i];

            }

        }

        System.out.println("Largest number is: " + largest);

    }

}


Example Program 2:

    Arrays.sort() : Arrays.sort() is a method in the Java programming language that can be used to sort an array of elements in ascending order.

public class LargestNumber2 {

    public static int getLargest(int[] a,int total)

    {

        Arrays.sort(a);

        return a[total-1];

        // If you want second Largest number then use "return a[total-2]"

    }

    public static void main(String args[])

    {

        int a[]={1,2,5,6,3,2};

        int total = a.length;

        System.out.println("Largest: "+getLargest(a,total));

    }

}


9. Separation of String and Numbers from a Given String:

Character Class:

    In the Java programming language, the Character class is a wrapper class for the primitive data type char. It provides several useful methods for working with characters, such as:

  • isDigit(char ch): returns true if the specified character is a digit (0-9).
  • isLetter(char ch): returns true if the specified character is a letter.
  • isLetterOrDigit(char ch): returns true if the specified character is a letter or digit.
  • isLowerCase(char ch): returns true if the specified character is a lowercase letter.
  • isUpperCase(char ch): returns true if the specified character is an uppercase letter.
  • toLowerCase(char ch): returns the lowercase equivalent of the specified character if it is an uppercase letter.
  • toUpperCase(char ch): returns the uppercase equivalent of the specified character if it is a lowercase letter.

Example Program:

public class SeparationStrAndNum {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.println("Enter a string with alphabets and numbers");

        String str = in.nextLine();

        String number = "";

        String letter = "";

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

        {

            char a = str.charAt(i);

            if (Character.isDigit(a))

            {

                number = number + a;

                continue;

            }

            if (Character.isLetter(a))

            {

                letter = letter + a;

            }

        }

        System.out.println("Alphabets in string:" + letter);

        System.out.println("Numbers in String:" + number);

    }

}


10. Find a Word Start with A, given String:

StringBuilder Class:

        In the Java programming language, the StringBuilder class is a mutable sequence of characters. It provides several useful methods for working with strings, such as:

    • append(String str): appends the specified string to the end of this string builder.
    • insert(int offset, String str): inserts the specified string at the specified position in this string builder.
    • replace(int start, int end, String str): replaces the characters in a substring of this string builder with characters in the specified string.
    • delete(int start, int end): removes the characters in a substring of this string builder.
    • reverse(): reverses the order of characters in this string builder.
    • toString(): returns a string representation of the contents of this string builder.

    Example Program:

    public class WordstartwithA {

        public static String beginWithA(String wordlist)

        {

            StringBuilder sb = new StringBuilder();//represents a mutable sequence of characters

            String myStr = wordlist;

            for (String b : myStr.split(" "))

            {

                if (b.startsWith("a") || b.startsWith("A"))

                {

                    sb.append(b + " ");

                }

            }

            return sb.toString();

        }

        public static void main(String[] args)

        {

            System.out.println(beginWithA("abc ABC animal bus car"));

        }

    }


    (or)


    public class FindWord {

        public static void main(String[] args) {

            String inputString = "Apple ant bat cat ancestor";

            String[] words = inputString.split(" ");

            for (String word : words) {

                if (word.startsWith("A") || word.startsWith("a")) {

                    System.out.println(word);

                }

            }

        }

    }


    (or)


    public class FindWord {

        public static void main(String[] args) {

            String inputString = "Apple ant bat cat ancestor";

            String[] words = inputString.split(" ");

            for (String word : words) {

                if (word.charAt(0)=='A' || word.charAt(0)=='a') {

                    System.out.println(word);

                }

            }

        }

    }


    No comments:

    Post a Comment