Tuesday, April 23, 2013
Wednesday, April 17, 2013
Factorial Example in Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FactorialEx {
public static void main(String[] args) throws NumberFormatException,
IOException {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader bu = new BufferedReader(input);
System.out.println("Enter Integer value:");
int n = Integer.parseInt(bu.readLine());
int fact = 1;
for (int i = n; i >= 1; i--) {
fact = fact * i;
}
System.out.println("Factorial of " + n + " is : " + fact);
}
}
/*
Output:
Enter Integer value:
5
Factorial of 5 is : 120
*/
Tuesday, April 16, 2013
To find the first repeated character in a string
Example:
java
public class FirstRepeatedCharacter {
/**
* @param args
*/
public static void main(String[] args) {
String str ="welcome";
for(int i=0;i<=str.length()-1;i++){
int count = 0;
for(int j=i+1;j<str.length();j++){
if( str.charAt(i) == str.charAt(j)){
count= count + 1;
break;
}
}
if(count != 0){
System.out.println("The first repeated character in a welcome is : " + str.charAt(i));
break;
}
}
}
}
/*
Output:
The first repeated character in a welcome is: e
*/
java
The repeated character in java is : a
public class FirstRepeatedCharacter {
/**
* @param args
*/
public static void main(String[] args) {
String str ="welcome";
for(int i=0;i<=str.length()-1;i++){
int count = 0;
for(int j=i+1;j<str.length();j++){
if( str.charAt(i) == str.charAt(j)){
count= count + 1;
break;
}
}
if(count != 0){
System.out.println("The first repeated character in a welcome is : " + str.charAt(i));
break;
}
}
}
}
/*
Output:
The first repeated character in a welcome is: e
*/
Monday, April 15, 2013
Find the first non repeated character in a string
To find the first non repeated character in a string
Example: madam
The non repeated character in madam is : d
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NonRepeatCharacter {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(input);
System.out.println("Enter String value :");
String str = br.readLine();
for(int i=0;i<str.length();i++){
int count = 0;
for(int j=0;j<str.length();j++){
if(i!=j && str.charAt(i) == str.charAt(j)){
count = count +1 ;
break;
}
}
if(count == 0){
System.out.println("The first non repeated character is : " +str.charAt(i));
break;
}
}
}
}
/*
Output:
Enter String value :
character
The first non repeated character is : h
Sunday, April 14, 2013
Floyd's Triangle Examples
/*
1. Example:
1
01
101
0101
*/
public class FloydsTriangle{
/**
* @param args
*/
public static void main(String[] args) {
for(int i=1;i<5;i++){
for(int j=1;j<=i;j++){
if( i == j){
System.out.print("1");
} else if( (i+j)%2 == 0){
System.out.print("1");
}else{
System.out.print("0");
}
}
System.out.println(" ");
}
}
}
/*
Output:
1
01
101
0101
01
101
0101
*/
---------------------------------------------------------------------------------------------------
2.Example :
1
22
333
4444
55555
55555
4444
333
22
1
---------------------------------------------------------------------------------------------------------
3.Example
---------------------------------------------------------------------------------------------------
2.Example :
1
22
333
4444
55555
55555
4444
333
22
1
public class FloydsTriangle2{
/**
* @param args
*/
public static void main(String[] args) {
/**
* @param args
*/
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(i);
}
System.out.println("");
}
for(int i=5;i>0;i--){
for(int j=1;j<=i;j++){
System.out.print(i);
}
System.out.println("");
}
}
}
3.Example
1
22
333
4444
55555
public class FloydsTriangle2{
/**
* @param args
*/
public static void main(String[] args) {
/**
* @param args
*/
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(i);
}
System.out.println(" ");
}
}
}
--------------------------------------------------------------------------------------
4. Example
1
12
123
1234
12345
--------------------------------------------------------------------------------------
4. Example
1
12
123
1234
12345
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println(" ");
}
----------------------------------------------------------------------------------------------
Check the given number is Armstrong number are not ?
An Armstrong number is a Three digit number such that the
sum of the cubes of its digit is equal to the number itself
Ex : 153 as 153= 1+ 125+27
which 1^3+5^3+3^3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Armstrong {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
System.out.println("Enter n value :");
String st = br.readLine();
int n = Integer.parseInt(st);
int n1 = n;
int sum = 0;
while(n>0){
int r = n%10;
sum = sum+(r*r*r);
n =n/10;
}
if(n1 == sum){
System.out.println("The given number is Armstrong number");
}else{
System.out.println("The given number is not Armstrong number");
}
}
}
/*Output :
Enter n value :
153
The given number is Armstrong number
------------------------------------------------------
Enter n value :
235
The given number is not Armstrong number
*/
Fibonacci Series Example
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FibonacciSeries {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
System.out.println("Enter n value:");
int n = Integer.parseInt(br.readLine());
int a;
int b=0;
int c=1;
for(int i=1 ; i<=n ; i++){
System.out.print(b + " ");
a = b;
b = c;
c = a+b;
}
}
}
/*
Output:
Enter n value:
10
0 1 1 2 3 5 8 13 21 34
*/
Swapping of two numbers without using temporary variable
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Swapping {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
System.out.println("Enter a and b values");
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());
System.out.println( "Before Swapping : A value = " + a + " B value =" + b);
/*without temporary variable*/
a = a + b;
b = a - b;
a = a - b;
System.out.println( "After swapping: A value = " + a + " B value =" + b);
}
}
/*
Output:
Enter a and b values
12
43
Before Swapping : A value = 12 B value =43
After swapping: A value = 43 B value =12
*/
Swap two numbers using a temporary variable
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Swapping {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
System.out.println("Enter a and b values");
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());
System.out.println( "Before Swapping : A value = " + a + " B value =" + b);
int temp;
temp = a;
a = b;
b= temp;
System.out.println( "After swapping: A value = " + a + " B value =" + b);
}
}
/*Output:
Enter a and b values
12
43
Before Swapping : A value = 12 B value =43
After swapping: A value = 43 B value =12
*/
Friday, April 12, 2013
To Check given Number Palindrome or not
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Palindrome {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader bu = new BufferedReader(input);
System.out.println("Enter n value ");
int n = Integer.parseInt(bu.readLine());
int y =n;
int temp = 0;
int sum = 0;
while(n > 0){
temp = n%10;
sum = sum*10 + temp;
n = n/10;
}
System.out.println("rever number " + sum);
if(sum == y ){
System.out.println("The given number is palindrome");
}else{
System.out.println("The given number is not palindrome");
}
}
}
Reverse String Word Example
1. Reverse String Word Example
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReverseStringWord {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
System.out.println("Enter the string ");
String str = br.readLine();
System.out.println(str);
String str1[] = str.split(" ");
for(int i=str1.length-1; i>=0;i--){
System.out.print(str1[i].toString() + " ");
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReverseStringWord {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
System.out.println("Enter the string ");
String str = br.readLine();
System.out.println(str);
String str1[] = str.split(" ");
for(int i=str1.length-1; i>=0;i--){
System.out.print(str1[i].toString() + " ");
}
}
}
Subscribe to:
Comments (Atom)