Translate

Monday 9 July 2012

Write java code to print following pattern

..........
.........
........
.......
......
.....
....
...
..
.
Solution:

class Pattern4
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
System.out.print(".");
}
System.out.println();
}
}
}



Write java code to reverse a given string using "StringBuffer" class


import java.io.*;

class ReverseString
{
public static void main(String args[]) throws IOException
{
String str;
System.out.println("Enter a string");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
StringBuffer sb=new StringBuffer(str);
String revstr=new String(sb.reverse());
System.out.println("Reverse of "+str+" is "+revstr);
}

}

Write java code to reverse a given number


import java.io.*;

class ReverseNum
{
public static void main(String args[]) throws IOException
{
int num,rem,revnum=0,originalnum;
System.out.println("Enter a number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
originalnum=num;
while(num>0)
{
rem=num%10;
num=(num-rem)/10;
revnum=revnum*10+rem;
}
System.out.println("Reverse of "+originalnum+" is "+revnum);
}
}

Saturday 7 July 2012

Write java code to find the Smallest of 3 numbers using ternary operator


import java.io.*;

class Small
{
public static void main(String args[]) throws IOException
{
int num1,num2,num3,small_num;
System.out.println("Enter first number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num1=Integer.parseInt(br.readLine());
System.out.println("Enter second number");
num2=Integer.parseInt(br.readLine());
System.out.println("Enter third number");
num3=Integer.parseInt(br.readLine());
small_num = (num1 < num2) ?( (num1<num3) ? num1:num3):(num2<num3?num2:num3);
System.out.println("The Smallest Number is: "+small_num);
}

}

Write java code to find the Biggest of 3 numbers using ternary operator


import java.io.*;

class Big
{
public static void main(String args[]) throws IOException
{
int num1,num2,num3,big_num;
System.out.println("Enter first number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num1=Integer.parseInt(br.readLine());
System.out.println("Enter second number");
num2=Integer.parseInt(br.readLine());
System.out.println("Enter third number");
num3=Integer.parseInt(br.readLine());
big_num = (num1 > num2) ?( (num1>num3) ? num1:num3):(num2>num3?num2:num3);
System.out.println("The Biggest Number is: "+big_num);
}

}

Write java code to obtain absolute value of given number using "ternary(?)" operator


import java.io.*;

class Ternary
{
public static void main(String args[]) throws IOException
{
int num,k;
System.out.println("Enter a number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
k = num < 0 ? -num : num;   //get absolute value of num
System.out.println("Absolute value of "+num+" is "+k);
}

}

The ? operator

Java includes a special ternary (three-way) operator that can replace certain types of if-then-else statements. This operator is the ?, and it works in Java much like it does in C,C++, and C#. It can seem somewhat confusing at first, but the ? can be used very effectively once mastered. The ? has this general form:

expression1 ? expression2 : expression3

Here, expression1 can be any expression that evaluates to a boolean value. If expression1 is true, then expression2 is evaluated; otherwise expression3 is evaluated. The result of the ? operation is that of the expression evaluated. Both expression2 and expression3 are required to return the same type, which can't be void.
Here is an example of the way that the ? is employed:

ratio = denom == 0 ? 0 : num/denom;

When Java evaluates this assignment expression, it first looks at the expression to the left of the question mark. If denom equals zero, then the expression between the question mark and the colon is evaluated and used as the value of the entire ? expression. If denom does not equal zero, then the expression after the colon is evaluated and used for the value of the entire ? expression. The result produced by the ? operator is then assigned to ratio.

Friday 6 July 2012

Write java code to check whether given number is an Armstrong number or not


import java.io.*;

class ArmStrong
{
public static void main(String args[]) throws IOException
{
int num,rem,sum=0,originalnum;
System.out.println("Enter a number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
originalnum=num;
while(num>0)
{
rem=num%10;
num=(num-rem)/10;
sum=sum+(rem*rem*rem);
}
if(originalnum==sum)
System.out.println("Armstrong Number!");
else
System.out.println("Not an Armstrong Number!");
}

}

Thursday 5 July 2012

Write java code to print following pattern

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

Solution:
class Pattern3
{
public static void main(String args[]) 
{
for(int i=1;i<6;i++)
{
for(int j=1;j<=i;j++)
{
if(j==1)
{
for(int k=6-i;k>0;k--)
{
System.out.print(" ");
}
}
System.out.print("* ");
}
System.out.println();
}
}
}

Write java code to print following pattern:

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

Solution:

class Pattern2
{
public static void main(String args[])
{
for(int i=1;i<6;i++)
{
for(int j=1;j<=i;j++)
{
if(j==1)
{
for(int k=6-i;k>0;k--)
{
System.out.print(" ");
}
}
System.out.print("*");
}
System.out.println();
}
}

}

Write java code to check whether given number is prime or not


import java.io.*;

class CheckPrime
{
public static void main(String args[]) throws IOException
{
int num,i;
System.out.println("Enter a number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
for(i=2;i<num;i++)
{
if(num%i==0)
{
System.out.println("Not Prime");
break;
}
}
if(i==num)
{
System.out.println("Prime");
}
}

}

Write java code to print Fibonacci series


import java.io.*;

class Fibonacci
{
public static void main(String args[]) throws IOException
{
int limit;
System.out.println("Enter Limit:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
limit=Integer.parseInt(br.readLine());
int series[]=new int[limit];
series[0]=0;
series[1]=1;
for(int i=2;i< limit;i++)
{
series[i]=series[i-1]+series[i-2];
}
System.out.println("Fibonacci Series upto " + limit);
                for(int i=0; i< limit; i++)
{
                       System.out.println(series[i]);
}

}

}

Wednesday 4 July 2012

Write java code to print following pattern:

1
12
123
1234
12345
123456
1234567
12345678
123456789

Solution:

class Pattern1
{
public static void main(String args[])
{
for(int i=1;i<10;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}

Write java code to check whether the given string is palindrome or not

import java.io.*;
class Palindrome
{
public static void main(String args[]) throws IOException
{
String str;
System.out.println("Enter a string");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
StringBuffer sb=new StringBuffer(str);
String revstr=new String(sb.reverse());
if(revstr.equalsIgnoreCase(str))
System.out.println("Palindrome String!");
else
System.out.println("Not a Palindrome String!");
}
}

Write java code to check whether the given number is palindrome or not

import java.io.*;
class Palindrome
{
public static void main(String args[]) throws IOException
{
int num,rem,revnum=0,originalnum;
System.out.println("Enter a number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
originalnum=num;
while(num>0)
{
rem=num%10;
num=(num-rem)/10;
revnum=revnum*10+rem;
}
if(originalnum==revnum)
System.out.println("Palindrome Number!");
else
System.out.println("Not a Palindrome Number!");
}
}

Write java code to calculate factorial of any given number using recursion

import java.io.*;
class Factorial
{
public static void main(String args[]) throws IOException
{
int num;
System.out.println("Enter a number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
if(num==0)
{
System.out.println("Factorial of 0 is: 1");
}
else
{
System.out.println("Factorial of "+num+" is: "+fact(num));
}
}
public static int fact(int num)
{
if(num==1)
return num;
else
return num*fact(num-1);
}
}

Tuesday 3 July 2012

Write java code to calculate factorial of any given number

import java.io.*;
class Factorial
{
public static void main(String args[]) throws IOException
{
int num,f=1;
System.out.println("Enter a number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
if(num==0)
{
System.out.println("Factorial of 0 is: 1");
}
else
{
for(int i=num;i>0;i--)
{
f=f*i;
}
System.out.println("Factorial of "+num+" is: "+f);
}
}
}