Thursday, October 30, 2014

Java Nested If Pro



Biggest of 3 Numbers using Nested If
 
import java.util.*;
class Biggest3
{
public static void main(String args[])
          {             
               int n1, n2, n3, big;
               Scanner scan= new Scanner(System.in);
               System.out.println("Please Enter No 1: ");
               n1=scan.nextInt();
               System.out.println("Please Enter No 2: ");
               n2=scan.nextInt();
               System.out.println("Please Enter No 3: ");
               n3=scan.nextInt();
               if(n1>n2)
               {
                 if(n1>n3)
                      big=n1;
                  else
                     big=n3;
               }
               else
               {
                 if(n2>n3)
                      big=n2;
                 else
                      big=n3;
               }
               System.out.println("Biggest No: " + big);
         }
    }
 
Biggest of 3 Numbers using Logical Operators
 
import java.util.*;
class Biggest3
{
public static void main(String args[])
{             
               int n1, n2, n3, big;
               Scanner scan= new Scanner(System.in);
               System.out.println("Please Enter No 1: ");
               n1=scan.nextInt();
               System.out.println("Please Enter No 2: ");
               n2=scan.nextInt();
               System.out.println("Please Enter No 3: ");
               n3=scan.nextInt();
               if(n1>n2 && n1>n3)
                  big=n1;
               else if(n2>n1 && n2>n3)
                  big=n2;
               else
                  big=n3;
               System.out.println("Biggest No: " + big);
          }
    }
      
Prime Number
 
import java.util.*;
class Prime
          {
public static void main(String args[])
{             
               int n, i, res;
               boolean flag=true;
               Scanner scan= new Scanner(System.in);
               System.out.println("Please Enter a No.");
               n=scan.nextInt();
               for(i=2;i<=n/2;i++)
               {
                           res=n%i;
               if(res==0)
               {
                           flag=false;
                           break;
                           }
               }
               if(flag)
                           System.out.println(n + " is Prime Number");
               else
                           System.out.println(n + " is not Prime Number");
   }
}

Fibonacci Series
 
class Fibonacci
{
public static void main(String args[])
{             
               int prev, next, sum, n;
               prev=next=1
   for(n=1;n<=10;n++)
   {
               System.out.println(prev);
               sum=prev+next;
               prev=next;
               next=sum;
     }
   }
}
      
Prime numbers

import java.util.Scanner;
public class PrimeNumbers
{
 public static void main(String[] args)
{
       Scanner s = new Scanner(System.in);
       System.out.print("Enter the first number number : ");
       int start = s.nextInt();
       System.out.print("Enter the second number number : ");
       int end = s.nextInt();
       System.out.println("List of prime numbers between " + start + " and " + end);
       for (int i = start; i <= end; i++)
        {
           if (isPrime(i))
            {
               System.out.println(i);
           }
       }
   }
    public static boolean isPrime(int n)
     {
       if (n <= 1)
       {
           return false;
       }
       for (int i = 2; i < Math.sqrt(n); i++)
        {
           if (n % i == 0)
           {
               return false;
           }
       }
       return true;
   }
}

Print the prime numbers
 
import java.util.Scanner;
class prime
{
public static void main(String[] args)
{
               int n,p;
               Scanner s=new Scanner(System.in);
               System.out.println(“Enter upto which number prime numbers are needed”);
               n=s.nextInt();
   for(int i=2;i
   {
               p=0;
               for(int j=2;j
               {
                           if(i%j==0)
                           p=1;
               }
               if(p==0)
                           System.out.println(i);
               }
   }
}

Find whether number is Prime or Not

class PrimeNo
{
public static void main(String args[])
{
               int num = Integer.parseInt(args[0]);
               int flag=0;
               for(int i=2;i
                {
                           if(num%i==0)
                           {
                                       System.out.println(num+" is not a Prime Number");
                                       flag = 1;
                                       break;
                           }
               }
               if(flag==0)
                           System.out.println(num+" is a Prime Number");
   }
}

No comments:

Post a Comment