Pages

CONTROL STRUCTURES looop

Test whether a character is vowel or not
import java.io.*;
class ChTest
{
          public static void main(String args[])throws IOException
          {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                   char ch;
                   System.out.print("Enter any character: ");
ch=(char)br.read( );
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
                   System.out.println("Vowel");
                   else
                   System.out.println("Constant");
          }
}
D:\prr\Core java>javac ChTest.java
D:\prr\Core java>java ChTest
Enter any character: s
Constant
D:\prr\Core java>java ChTest
Enter any character: o
Vowel
// test whether a number is perfect squire or not
import java.io.*;
class PerefectSquare1
{
          public static void main(String args[])throws IOException
          {
BufferedReader br=new BufferedReader(new
             InputStreamReader(System.in));
                   int i=0,x=0;
                   System.out.print("enter any number: ");
                   i=Integer.parseInt(br.readLine( ));
                   x=(int)Math.sqrt(i);
                   if(i==(x*x))
                   System.out.println("Perfect square ");
                   else
                   System.out.println("Not a perfect Square ");
          }
}

D:\prr\Core java>javac PerefectSquare1.java
D:\prr\Core java>java PerefectSquare1
Enter any number: 5
Not a perfect Square
D:\prr\Core java>java PerefectSquare1
Enter any number: 9
Perfect square
// given point lies inside the circle or out side the circle or on the circle
import java.io.*;
class PointLies
{
          public static void main(String args[])throws IOException
          {
BufferedReader br=new BufferedReader(new
   InputStreamReader(System.in));
                   int x=0,y=0,r=0;
                   System.out.println("Enter x & y co ordinates : ");
                   x=Integer.parseInt(br.readLine( ));
                   y=Integer.parseInt(br.readLine( ));
                   System.out.println("Enter Radius :");
                   r=Integer.parseInt(br.readLine( ));
                   int a=(x*x)+(y*y);
                   int b=(r*r);
                   if(a==b)
                             System.out.println("Point lies on the Circle");
                   else              if(a>b)
                             System.out.println("Point lies out side the Circle");
                   else
                             System.out.println("Point lies in side the Circle");
          }
}
D:\prr\Core java>javac PointLies.java
D:\prr\Core java>java PointLies
Enter x & y co ordinates :
10
20
Enter Radius :
4
Point lies out side the Circle
// to find the Factorial value
import java.io.*;
class Factorial
{
          public static void main(String args[])throws IOException
          {
                   BufferedReader br=new BufferedReader(new
             InputStreamReader(System.in));
                   int fact=1;
                   System.out.print("Enter any Number : ");
                   int n=Integer.parseInt(br.readLine( ));
                   if(n==0)
                   System.out.println("Factorial of 0 is 1");
                   while(n>0)
                   {
                             fact=fact*n;
                             n =1;
                   }
                   System.out.println("Factorial ="+fact);
          }
}

D:\prr\Core java>javac Factorial.java
D:\prr\Core java>java Factorial
Enter any Number : 5
Factorial =120
// sum of the digits
import java.io.*;
class SumDigits
{
          public static void main(String args[])throws IOException
          {
                   BufferedReader br=new BufferedReader(new
             InputStreamReader(System.in));
                   int n=0,sum=0,d=0;
                   System.out.print("Enter any Number : ");
                   n=Integer.parseInt(br.readLine( ));
                   do
                   {
                             d=n%10;
                             sum+=d;
                             n=n/10;
                   }
                   while(n>0);
                   System.out.println("Sum of digits ="+sum);
          }
}
D:\prr\Core java>java SumDigits
Enter any Number : 236
Sum of digits =11
/* all the 4 number digits Square of the 1st 2 digits are perfect squire & also last 2 digits */
import java.io.*;
class PerfectSquare
{
          public static void main(String args[])
          {
                   int nl=0,nr=0,temp=0,x=0,y=0,z=0;
                   for(int i=1000;i<=9999;i++)
                   {
                             temp=i;
                             x=(int)Math.sqrt(temp);
                             {
                                      if(temp==x*x)
                                      {
                                                nl=temp/100;
                                                nr=temp%100;
                                                y=(int)Math.sqrt(nl);
                                                z=(int)Math.sqrt(nr);
                                                if((nl==y*y)&&(nr==z*z))
                                                          System.out.print(temp+”\t”);
                                      }
                             }
                   }
          }
}
D:\prr\Core java>javac PerfectSquare.java
D:\prr\Core java>java PerfectSquare
1600         1681       2500         3600       4900         6400       8100