Pages

Java Program to Find Factorial

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
5! = 5  \times  4  \times  3  \times  2  \times  1 = 120.  \
The value of 0! is 1, according to the convention for an empty product


 import java.util.Scanner;
  public class Factorial {
  public static void main(String[] args) {
    int num  ,fact=1;
    System.out.println("enter number:");
    Scanner sc=new Scanner(System.in);

    num=sc.nextInt();
    if(num<0){
    System.out.println("only positive number !");
   }
   else{

    for(int i=1;i<=num;i++)
    {
     fact=fact*i;
    }
    System.out.println("the factorial of "+num + " is " +fact);
    }
   }
 }

Output:
=======

 enter number: 3
 the factorial of 3 is 6

Reading a String

The String class represents character strings. All string literals in Java programs, such as "abc" , are implemented as instances of this class.Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.


Reading a String
<
import java.util.Scanner;
public class ReadString {
 public static void main(String[] args) {
  System.out.println("enter a String: ");

  Scanner sc =new Scanner(System.in);
while(true){
String s=sc.nextLine();
               s=s.toLowerCase();
if(s.indexOf("hari")!=-1){
System.out.println("Hi,hari!");
}
else{
System.out.println("Who are you?");
}
break;
}
}

}


OutPut:
Enter a String:  hari
Hi,hari!

Program to find out the Repetition of character in a String

 Repetition of character in a String

import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String : ");
String value = sc.nextLine();
//System.out.println();
System.out.print("Enter the Charcter : ");
String ch = String.valueOf((sc.next().charAt(0)));
int count=0;
for(int i=0;i<value.length();i++)
{
if((String.valueOf(value.charAt(i))).equalsIgnoreCase(ch))
{
count++;
}
}
System.out.println("The character : "+ ch +" is present "+count+" times");
}
}

Output :
Enter the String : hello
Enter the Charcter : l
The character : l is present 2 times
 


Program to find count of vowels and consonant

Program to find count of vowels and consonant


import java.util.Scanner;
public class Vowel {
public static void main(String[] args) {
System.out.println("enter a string:");
Scanner sc=new Scanner(System.in);
String v=sc.nextLine();
int len=v.length();
char[] vw={'a','e','i','o','u'};
int count=0;
for(int i = 0 ; i< len;i++)
{
for(int j = 0; j<5;j++)
{
if((String.valueOf(v.charAt(i))).equalsIgnoreCase((String.valueOf(vw[j]))))
{
count++;
}
 }
    }
System.out.println("In this String there are "+count+" vowels and "+(len-count)+" consonant.");
   }
 }

Reverse a String in java

1.//Program to write  reverse a string



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

String s="abcd";
String rs="";
for (int i=s.length()-1;i>=0;i--){
rs=rs+s.charAt(i);
}
System.out.println(s);
System.out.println(rs);
}
}

OutPut :

abc
cba

2. Using StringBuffer(String string) method, reverse the buffer and then converts the buffer into a String with the help of toString()


public class StringReverseExample{
   public static void main(String[] args){
      String string="abcdef";
      String reverse = new StringBuffer(string).
      reverse().toString();
      System.out.println("\nString before reverse:
      "+string);
      System.out.println("String after reverse:
      "+reverse);
   }
}

3. Another  


public class Reverse {

public String reverseString(String s){
if (s.length()<=1){
return s;
}
else 
{
char c=s.charAt(0);
return reverseString(s.substring(1))+c;
}
}
}

class StringReverse{
public static void main(String[] args){
Reverse r=new Reverse();
String s1="abcde";
System.out.println("Given String="+ (s1));
System.out.println("Reverse String="+ r.reverseString(s1));
}
}

Output:

Given String=abcde
Reverse String=edcba

4. String Input from Keyword 


import java.util.Scanner;
public class A {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String : ");
String value=sc.nextLine();
int len = value.length();
char[] ch = new char[len];
for(int i = 0;i<len;i++)
{
ch[i]= value.charAt(len-1-i);
}
String result = new String (ch);
System.out.println(result);
}
}