Java Program to count number of vowels in a string


Solution #1:

  1. package com.javatutorial;
  2.  
  3. public class CountNumberofVowels {
  4.  
  5.  public static void main(String[] args) {
  6.   
  7. System.out.println("Please enter a String");
  8. Scanner in = new Scanner(System.in);                  
  9.  String input = in.nextLine();
  10.  
  11.      char[] Chararray = input.toCharArray();
  12.  
  13.  int count = 0;
  14.  
  15.  for (char ch : Chararray) {
  16.       switch (ch) {
  17.              case 'a':
  18.              case 'e':
  19.              case 'i':
  20.              case 'o':
  21.              case 'u':
  22.              case 'A':
  23.              case 'E':
  24.              case 'I':
  25.              case 'O':
  26.              case 'U':
  27.                  count++;
  28.      break;
  29.      default:
  30.             
  31.      }
  32.  }
  33.        System.out.println("Number of vowels in String "+count);
  34.  
  35. }

  36. }


Output:

  1. Please enter a String
  2. Instance Of Java
  3. Number of vowels in String 6
     
Solution #2:

  1. package com.javatutorial;
  2.  
  3. public class CountNumberofVowels {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.  int vowels = 0, digits = 0, blanks = 0; char ch; 

  8. System.out.println("Please enter a String");
  9. Scanner in = new Scanner(System.in);                  
  10. String testString= in.nextLine();
  11.  
  12.  
  13. for(int i = 0; i < testString.length(); i ++)
  14. {
  15.  
  16.   ch = testString.charAt(i);
  17.  
  18.   if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||   
  19.       ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
  20.           vowels ++;
  21.        else if(Character.isDigit(ch))
  22.           digits ++;
  23.      else if(Character.isWhitespace(ch))
  24.            blanks ++;
  25.   }
  26.  
  27.     System.out.println("Vowels : " + vowels);
  28.     System.out.println("Digits : " + digits);
  29.     System.out.println("Blanks : " + blanks);           
  30.    
  31.  
  32. }

  33. }
Output:

  1. Please enter a String
  2. vowels and consonants
  3. Vowels : 6
  4. Digits : 0
  5. Blanks : 2

Java Program to Count the number of occurrences of a char in a String?



Solution #1:

  1. package com.javatutorial;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CountNumberofChars {
  6.  
  7.  public static void main(String[] args) {
  8.   
  9.  String str = "";
  10.  
  11.  Scanner in= new Scanner(System.in);
  12.  System.out.println("Please enter a String");
  13.  
  14.  str=in.nextLine();
  15.  
  16.  System.out.println("Please enter a Character");
  17.  String chr=in.next();
  18.  
  19.  int charCount = str.length() - str.replaceAll("a", "").length();
  20.  
  21.  System.out.println("Number of occurances of given character:"+charCount);
  22.  
  23. }

  24. }



Output:

  1. Please enter a String
  2. Java
  3. Please enter a Character
  4. a
  5. Number of occurances of given character:2


Solution #2:


  1. package com.javatutorial;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CountNumberofChars {
  6.   
  7. public static int countOccurrences(String find, String string)
  8. {
  9. int count = 0;
  10. int indexOf = 0;
  11.  
  12. while (indexOf > -1)
  13. {
  14.     indexOf = string.indexOf(find, indexOf + 1);
  15.     if (indexOf > -1)
  16.             count++;
  17.       }
  18. return count;
  19.  }

  20.  public static void main(String[] args) {
  21.   
  22.  int charCount=countOccurrences("a", "Instance of Java");
  23.  
  24.  System.out.println("Number of occurrences of given character:"+charCount);
  25.  
  26. }

  27. }

Output:

  1. Number of occurrences of given character: 3

Java program To Count the number of words in a String




  1. package com.javatutorial;
  2.  
  3. public class CountNumberofWords {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7. String s="";
  8. int count=0;
  9.  
  10. Scanner in = new Scanner(System.in);
  11. System.out.println("Please enter a String");
  12.  s=in.nextLine();
  13.  
  14. char ch[]= new char[s.length()];    
  15.  
  16. for(int i=0;i<s.length();i++)
  17. {
  18.  
  19.     ch[i]= s.charAt(i);
  20.  
  21.     if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
  22.         count++;
  23.  
  24. }
  25. System.out.println("Number of words in given String: "+count);
  26.  
  27. }

  28. }


Output:

  1. Please enter a String
  2. Java Tutorial
  3. Number of words in given String: 2

Pattern Program in java Part-3


 Program #1:  java program to print pyramid of stars using for loop in below format


*
**
***
****
*****
******
*******
********
*********
**********




  1. package com.learnJavaOnline;
  2. public class PrintStarsFormat {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8. for (int row = 1; row <= 10; row++) {
  9.  
  10.     for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
  11.       System.out.print("*");
  12.     }
  13.  
  14.     System.out.println(); 
  15.  }
  16.  
  17. }
  18.  
  19. }
Output:
  1. *
  2. **
  3. ***
  4. ****
  5. *****
  6. ******
  7. *******
  8. ********
  9. *********
  10. **********

            

Program #2: java program to print pyramid of stars using for loop in below format



**********
*********
********
*******
******
*****
****
***
**
*



  1. package com.learnJavaOnline;
  2. public class PrintStarsPyramid {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8. for(int i=numberOfStars; i>0 ;i--){
  9.  
  10.     for(int j=0; j < i; j++){
  11.  
  12.           System.out.print("*");
  13.     }
  14.  
  15.     System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }
Output:
  1. **********
  2. *********
  3. ********
  4. *******
  5. ******
  6. *****
  7. ****
  8. ***
  9. **
  10. *



Program #3 java program to print pyramid of stars using for loop in below format


*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*




  1. package com.learnJavaOnline;
  2. public class PrintStarsPyramid {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8.  for(int i=1; i<= numberOfStars ;i++){
  9.  
  10. for(int j=0; j < i; j++){
  11.  
  12.   System.out.print("*");
  13.  
  14. }
  15.  
  16. System.out.println("");
  17.  
  18. }
  19.  
  20. for(int i=numberOfStars; i>0 ;i--){
  21.  
  22. for(int j=0; j < i; j++){
  23.   System.out.print("*");
  24. }
  25.  
  26. System.out.println("")}
  27.  
  28. }
  29.  
  30. }
Output:
  1. *
  2. **
  3. ***
  4. ****
  5. *****
  6. ******
  7. *******
  8. ********
  9. *********
  10. **********
  11. **********
  12. *********
  13. ********
  14. *******
  15. ******
  16. *****
  17. ****
  18. ***
  19. **
  20. *




Print Pascals triangle using java program


Pattern Program in java Part-2

#4 Java Program to print Numbers in Below pattern

1 2 3 4 5 6 7 8 9
 1 2 3 4 5 6 7 8
  1 2 3 4 5 6 7
   1 2 3 4 5 6
    1 2 3 4 5
     1 2 3 4
      1 2 3
       1 2
        1
        




  1. package com.learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c1,c2;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c1 = 1; c1 <r; c1++) {
  10.   System.out.print(" ");
  11. }
  12.  
  13. for (c2 = 1; c2 <= 10 - r; c2++) {
  14.     System.out.print(c2 + " ");
  15. }
  16.  
  17. System.out.println("");
  18. }
  19.  
  20. }
  21.  
  22. }


Output:
  1. 1 2 3 4 5 6 7 8 9  
  2. 1 2 3 4 5 6 7 8 
  3.   1 2 3 4 5 6 7 
  4.    1 2 3 4 5 6 
  5.     1 2 3 4 5 
  6.      1 2 3 4 
  7.       1 2 3 
  8.        1 2 
  9.         1
            
#5 Java Program to Print Numbers in Below pattern:

        1
       1 2
      1 2 3
     1 2 3 4
    1 2 3 4 5
   1 2 3 4 5 6
  1 2 3 4 5 6 7
 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10




  1. package com.learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c1,c2;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c1 = 1; c1 <10-r; c1++) {
  10.   System.out.print(" ");
  11. }
  12.  
  13. for (c2 = 1; c2 <= r; c2++) {
  14.     System.out.print(c2 + " ");
  15. }
  16.  
  17. System.out.println("");
  18. }
  19.  
  20. }
  21.  
  22. }

Output:
  1.         1 
  2.        1 2 
  3.       1 2 3 
  4.      1 2 3 4 
  5.     1 2 3 4 5 
  6.    1 2 3 4 5 6 
  7.   1 2 3 4 5 6 7  
  8. 1 2 3 4 5 6 7 8 
  9. 1 2 3 4 5 6 7 8 9 
  10. 1 2 3 4 5 6 7 8 9 10
            

Print Pascals triangle using java program




Pattern Program in java Part-1

#1 Java Program To print Numbers in Below pattern:

              1
             2 3
            4 5 6
           7 8 9 10
         11 12 13 14 15




  1. package com.javatutorial;
  2. public class NumbersFormat {
  3.  
  4.  public static void main(String[] args) {
  5.  
  6.  int num=15;
  7.  int temp=1;
  8.      
  9.  for (int i = 1; i <= num; i++)
  10.  {
  11.  
  12.   for (int k = i; k <num; k++)
  13.    System.out.print(" ");
  14.    for (int j =1; j <= i; j++){
  15.  
  16.     System.out.print("" +temp+ " ");
  17.      temp++;
  18.  
  19.  if(temp>15){
  20.        break;
  21.  }
  22.  
  23.  }
  24.  
  25.   System.out.println();
  26.  
  27. if(temp>15){
  28.      break;
  29.   }
  30.  
  31. }
  32.  
  33. }

  34. }
Output:

  1.               1
                 2 3
                4 5 6
               7 8 9 10
              11 12 13 14 15





# Java Program to print Numbers in Below Format:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10


  1. package com.learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c = 1; c <= r; c++) {
  10.  
  11. System.out.print(c + " ");
  12.  
  13. }
  14.  
  15. System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }
Output:
  1. 1 2 
  2. 1 2 3 
  3. 1 2 3 4 
  4. 1 2 3 4 5 
  5. 1 2 3 4 5 6 
  6. 1 2 3 4 5 6 7 
  7. 1 2 3 4 5 6 7 8 
  8. 1 2 3 4 5 6 7 8 9 
  9. 1 2 3 4 5 6 7 8 9 10


#3 Java Program to print numbers in below format

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


  1. package com.learnJavaOnline;
  2. public class NumbersFormat {
  3. public static void main(String[] args) {
  4.  
  5. int r, c;
  6.  
  7. for (r = 1; r <= 10; r++) {
  8.  
  9. for (c = 1; c <= 10-r; c++) {
  10.  
  11. System.out.print(c + " ");
  12.  
  13. }
  14.  
  15. System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }

Output:
  1. 1 2 3 4 5 6 7 8 9 
  2. 1 2 3 4 5 6 7 8 
  3. 1 2 3 4 5 6 7 
  4. 1 2 3 4 5 6 
  5. 1 2 3 4 5 
  6. 1 2 3 4 
  7. 1 2 3 
  8. 1 2 
  9. 1



Print Pascals triangle using java program

Select Menu