Pages

get our extension

Search program

Monday, 29 April 2013

Java Program to Find Factorial Of a Number Using Recursion ..


package com.alok;
import java.util.Scanner;
class Cal
{
int fact(int n)
{


if(n==0 || n==1)
return 1;
else
return fact(n-1)*n;

}
}

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

Scanner sc=new Scanner(System.in);
System.out.println("Enter number for factorial");
int n=sc.nextInt();
Cal c=new Cal();
int f=c.fact(n);
System.out.println("Factorial of "+n+" is : "+f);

}
}

O/P :-


Enter number for factorial
6
Factorial of 6 is : 720



Java Program To Reverse String Using Command Line Argument ....


package com.alok;

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

O/P :- 


Java Program to Reverse String Without using reverse Function.....


package com.alok;

import java.util.Scanner;

public class String_reverse
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String :");
String n=sc.nextLine();
String rev="";
int len=n.length();
for(int i=len-1;i>=0;i--)
{
rev=rev+n.charAt(i);
}
                System.out.println("Reverse of Given String is :");
System.out.println(""+rev);
}
}

O/P:- 

Enter String :
ALOK KUMAR
Reverse of Given String is :
RAMUK KOLA


Find N Fibonacci Series (Using Recursion) .....


package com.alok;
import java.util.Scanner;

class calc
{
int fibo(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
}
public class Fibonacci
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter fibonacci Term :");
int n=sc.nextInt();
System.out.println("Fibonacci Series is :\n");
calc c=new calc();
for(int i=0;i<n;i++)
{
System.out.print("   "+c.fibo(i));
}
}
}

O/P:-


Enter fibonacci Term :
8
Fibonacci Series is :

   0   1   1   2   3   5   8   13


Find Prime Number upto N ...


package com.alok;
import java.io.*;

class Prime_check
{
  public static void main(String[] args) throws Exception
  {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Enter Limit:");
 int num = Integer.parseInt(br.readLine());
 System.out.println("Prime numbers: ");
 for (int i=1; i < num; i++ )
 {
 int j;
 for (j=2; j<i; j++)
 {
 int n = i%j;
  if (n==0)
  {
 break;
  }
 }
  if(i == j)
  {
  System.out.print("  "+i);
  }
  }
  }
}

O/P:-


Enter Limit:
50
Prime numbers:
  2  3  5  7  11  13  17  19  23  29  31  37  41  43  47

Tuesday, 16 April 2013

Java Program to Accept Different types of Input(int, byte, short, long, float, double, char, string, boolean) From Keyboard and displaying the entered values on monitor


package com.alok;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Accepting_input_from_keyword
{
public static void main(String[] args)throws IOException
{
/*Accepting Different type of Input(integer,float,double,short,
  long, byte, char, string, boolean) values from Keyboard*/

Boolean bul=false;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string:");
String str=br.readLine();
System.out.println("Enter a integer:");
int n=Integer.parseInt(br.readLine());
System.out.println("Enter float value:");
Float ft=Float.parseFloat(br.readLine());
System.out.println("Enter short value:");
Short sht=Short.parseShort(br.readLine());
System.out.println("Enter a Double value:");
Double dub=Double.parseDouble(br.readLine());
System.out.println("Enter a long value:");
long log=Long.parseLong(br.readLine());
System.out.println("Enter a Byte value:");
Byte bit=Byte.parseByte(br.readLine());
System.out.println("Enter a character:");
char ch=(char)br.read();


//Displaying values on console

                System.out.println("-------------------------");
                System.out.println("Entered Values are :");
                System.out.println("-------------------------");
System.out.println("Integer value is: "+n);
System.out.println("Float value is: "+ft);
System.out.println("Short value is: "+sht);
System.out.println("Double value is: "+dub);
System.out.println("Long value is: "+log);
System.out.println("Byte value is: "+bit);
System.out.println("Character value is: "+ch);
System.out.println("String value is: "+str);
System.out.println("Boolean value is: "+bul);

}

}

OUTPUT:-



Enter a string:
ALOK
Enter a integer:
1234
Enter float value:
12.1234567
Enter short value:
23
Enter a Double value:
32.123456789098765
Enter a long value:
1234567
Enter a Byte value:
21
Enter a character:
a
-------------------------
Entered Values are :
-------------------------
Integer value is: 1234
Float value is: 12.123457
Short value is: 23
Double value is: 32.12345678909877
Long value is: 1234567
Byte value is: 21
Character value is: a
String value is: ALOK
Boolean value is: false






Range of Primitive Data types(Byte,short,int,long, float,double, boolean, char) in java




               PRIMITIVE DATA TYPES IN JAVA

byte:
1 signed byte (two's complement). Covers values from -128 to 127.

short:
2 bytes, signed (two's complement), -32,768 to 32,767

int:
4 bytes, signed (two's complement). -2,147,483,648 to 2,147,483,647. Like all numeric types ints may be cast into other numeric types (byte, short, long, float, double). When lossy casts are done (e.g. int to byte) the conversion is done modulo the length of the smaller type.

long:
8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

float:
4 bytes. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Like all numeric types floats may be cast into other numeric types (byteshortlongintdouble). When lossy casts to integer types are done (e.g. float to short) the fractional part is truncated and the conversion is done modulo the length of the smaller type.

double:
            8 byte. Covers a range from 4.94065645841246544e-324d to 
            1.79769313486231570e+308d (positive or negative). 

boolean:
1-bit. May take on the values true and false only. true and false are defined constants of the language and are not the same as True and FalseTRUE and FALSE, zero and nonzero, 1 and 0 or any other numeric value. Booleans may not be cast into any other type of variable nor may any other variable be cast into a boolean.

char:
2 bytes, unsigned, Unicode, 0 to 65,535



Data Type Range
byte -128 to 127
short -32,768 to 32767
int -2,147,483,648 to 2,147,483,647
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative)
double 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative)
Boolean true and false
char 0 to 65,535