Posts

Showing posts from July, 2012

Java snippet to count number of words in a sentence

Java code snippet to count number of words in a sentence

Java code snippet to count number of spaces in a string

Java program to count no of consonants in a string

Java Snippet to count number of vowels in a string

Java program to count no of letters in a string

import java.util.*; class countLetters {     public static void main(String s[])     {         Scanner sc = new Scanner(System.in);         int count = 0;         char ch;                 System.out.println("Enter The String To Count No. Of Letters :  ");         String str = sc.nextLine();                 for(int i=0;i<str.length();i++)         {                 ch = str.charAt(i);                 if(ch!=' ')                 {                     count++;                 }                         }         System.out.println("The no of letters in given string is : " + count);     } }

Java program to check whether a year is leap or not

import java.util.*; class leapYear {     public static void main(String s[])     {         Scanner sc = new Scanner(System.in);         System.out.println("Enter a year to check whether it is leap year or not: ");         int yr= sc.nextInt();         if((yr % 4 == 0) && (yr % 100 == 0) && (yr % 400 == 0))         {             System.out.println("The year "+yr+" is a leap year.");         }         else         {             System.out.println("The year "+yr+" is not a leap year.");         }     } }

Java program to check whether a given number is palindrome or not

import java.util.*; class palindrome  {   public static void main(String s[] ){   try{   Scanner sc =new Scanner(System.in);   System.out.println("Enter number");   int num= sc.nextInt();   int n = num;   int rev=0;   System.out.println("Number: ");   System.out.println(" "+ num);   while(num>0){   int r=num%10;   num=num/10;   rev=rev*10+r;     }   System.out.println("After reversing the number: "+ " ");   System.out.println(" "+ rev);    if(n == rev){   System.out.print("Number is palindrome!");   }   else{   System.out.println("Number is not palindrome!");   }   }   catch(Exception e){   System.out.println("Out of range!");   }   } }