Monday, January 23, 2017

Lottery Odds

Program to calculate Lottery Odds of a Lottery Draw :

Lottery OddsIn a typical 6/49 game, each player chooses six non-duplicate numbers from a range of 1-49. If the six numbers on a ticket match the numbers drawn by the lottery, the ticket holder is a jackpot winner—regardless of the order of the numbers. The probability of this happening is 1 in 13,983,816.

Following is the code to find lottery odds:

import java.util.*;
public class Lottery 
{

   
    public static void main(String[] nt) 
    {
        
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Total Numbers");
        int a=in.nextInt();
        System.out.println("Enter Total Numbers to draw");
        int b=in.nextInt();
        double lottery=1;
        for(int i=1;i<=b;i++)
        {
            lottery=lottery*(a-i+1)/i;     //combination
        }
        System.out.println("Total Lottery odds are "+lottery);
    }
    

}

Output:

Sunday, January 22, 2017

Fibonacci Sequence Java

Following code will help you understand:

import java.util.*;
public class Fibonacci {
    public static void main(String[] nt)
    {   int a=0,b=1,next;
        Scanner in=new Scanner(System.in);
        { System.out.println("Enter Number of Terms");
        int n=in.nextInt();
        if(n<1)
        {
            System.out.println("Invalid No of Terms !! Please Try Again");
        }
        
        else if(n==1)
        {     System.out.println("Terms are");
               System.out.println(a);
        }
        else
        {   System.out.println("Terms are");
            System.out.print(a+" "+b+" ");
            for(int i=3;i<=n;i++)
            {
                next=a+b;
                System.out.print(next+" ");
                a=b;b=next;
            }
            System.out.println();
        }
    
     }
}

}

Output:


Prime Numbers Upto N Java

Following code prints all prime numbers upto user defined N:

import java.util.*;
public class Prime {
    public static void main(String[] nt)
    {
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Max Limit");
        int n=in.nextInt();
        int a=2,p=0;
        do
        {
            for(int c=2;c<=a/2;c++)
            {
                if(a%c==0)
                {
                    p=1;
                    break;
                }
            }
            if(p==0)
            {
                System.out.print(a+" ");
            }
            a++;
            p=0;
        }
        while(a<=n);
    }

}

Output:


Number Pyramid Pattern JAVA

Following is the code for pyramid pattern:


import java.util.*;
public class Pattern 
{
    
    public static void main(String[] nt)
    {
      
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Number of rows");
        int x=in.nextInt();
        int a=x+5;
        System.out.println("Pattern is");
        for(int i=1;i<=x;i++)
        {      for(int j=a;j!=0;j--)
            {
                System.out.print(" ");
            }
         for(int j=1;j<=i;j++)
        {  
            System.out.print(i+" ");
        }
        System.out.println();
        a--;
    }
    }
    
}

Output:

Right Angle Number Triangle

Following program accepts no of rows and print a triangle pattern as given below:

Test Data
Input number of rows :5
 Output :
1
12
123
1234
12345

import java.util.*;
public class Inches 
{
    
    public static void main(String[] nt)
    {
      
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Number of rows");
        int x=in.nextInt();

        System.out.println("Pattern is");
        for(int i=1;i<=x;i++)
    {   for(int j=1;j<=i;j++)
        {  
            System.out.print(j);
        }
        System.out.println();
    }
    }
    
}
Output:


Display weekday from day number using Switch Java

Following will help you understand:

/import java.util.*;
public class Weekday
{
   
    public static void main(String[] nt)
    {
     
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Day Number");
        int a=in.nextInt();
        dayname(a);
       
    }
    public static void dayname(int c)
    {  String weekday;
        switch(c)
                {
                    case 1:weekday="Monday"; break;
                    case 2:weekday="Tuesday"; break;
                    case 3:weekday="Wednesday"; break;
                    case 4:weekday="Thrusday"; break;
                    case 5:weekday="Friday"; break;
                    case 6:weekday="Saturday"; break;
                    case 7:weekday="Sunday"; break;
                    default:System.out.println("Invalid day number");return;
                   
                }
        System.out.println("Weekday is "+weekday);
    }
   
}

Output:


Saturday, January 21, 2017

Addition of Two Binary Numbers

Following program accepts two binary numbers and gives the sum of that two binary numbers

/import java.util.*;
public class Table
{
    public static void main(String[] nt)
    {   int[] sum=new int [20];
        Scanner in=new Scanner(System.in);
        System.out.println("Enter Two Binary Numbers");
        long binary1=in.nextLong();
        long binary2=in.nextLong();
        int i=0,carry=0;
        while(binary1!=0||binary2!=0)
        {sum[i++]=(int)((binary1%10+binary2%10+carry)%2);
         carry=(int)((binary1%10+binary2%10+carry)/2);
         binary1=binary1/10;
         binary2=binary2/10;
        }
        if(carry!=0)
        {
            sum[i]=carry;
        }
        System.out.print("Sum is: ");
        while(i>=0)
        {
            System.out.print(sum[i--]);
        }
        System.out.println();
       
    }
   
}

Output: