Thursday, October 30, 2014

Java Strings Pro



Java program to compare two strings

import java.util.Scanner;
class CompareStrings
{
   public static void main(String args[])
   {
      String s1, s2;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the first string");
      s1 = in.nextLine();
      System.out.println("Enter the second string");
      s2 = in.nextLine();
      if ( s1.compareTo(s2) > 0 )
         System.out.println("First string is greater than second.");
      else if ( s1.compareTo(s2) < 0 )
         System.out.println("First string is smaller than second.");
      else   
         System.out.println("Both strings are equal.");
   }
}

Interface in Java

interface Info 
{
  static final String language = "Java";  
  public void display();
}
class Simple implements Info 
{
 public static void main(String []args) 
{
    Simple obj = new Simple();
    obj.display();
  }  
  public void display() 
 {
    System.out.println(language + " is awesome");
  } 
}

Java program to check palindrome

import java.util.*;
class Palindrome
{
 public static void main(String args[])
 {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
      System.out.println("Enter a string to check if it is a palindrome");
      original = in.nextLine();
      int length = original.length();
      for ( int i = length - 1; i >= 0; i-- )
         reverse = reverse + original.charAt(i);
      if (original.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
         System.out.println("Entered string is not a palindrome.");
   }
} 

Java program to reverse a string

import java.util.*;
class ReverseString
{
 public static void main(String args[])
{
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
      System.out.println("Enter a string to reverse");
      original = in.nextLine();
      int length = original.length();
      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);
      System.out.println("Reverse of entered string is: "+reverse);
   }
}

Java program to print Floyd's triangle

import java.util.Scanner;
class FloydTriangle
{
public static void main(String args[])
          {
      int n, num = 1, c, d;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows of floyd's triangle you want");
      n = in.nextInt();
      System.out.println("Floyd's triangle :-");
      for ( c = 1 ; c <= n ; c++ )
      {
         for ( d = 1 ; d <= c ; d++ )
         {
            System.out.print(num+" ");
            num++;
         }
         System.out.println();
      }
   }
}

Java program to check armstrong number

import java.util.*;
class ArmstrongNumber
{
 public static void main(String args[])
 {
      int n, sum = 0, temp, r;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter a number to check if it is an armstrong number");      
      n = in.nextInt();
      temp = n;
 
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10; 
      }
       if ( n == sum )
         System.out.println("Entered number is an armstrong number.");
      else
         System.out.println("Entered number is not an armstrong number.");         
   }
}

Java program print prime numbers

import java.util.*;
class PrimeNumbers
{
 public static void main(String args[])
 {
      int n, status = 1, num = 3;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of prime numbers you want");
      n = in.nextInt();
      if (n >= 1)
      {
         System.out.println("First "+n+" prime numbers are :-");
         System.out.println(2);
      }
      for ( int count = 2 ; count <=n ;  )
      {
         for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
         {
            if ( num%j == 0 )
            {
               status = 0;
               break;
            }
         }
         if ( status != 0 )
         {
            System.out.println(num);
            count++;
         }
         status = 1;
         num++;
      }         
   }
}

No comments:

Post a Comment