Friday, 17 June 2016

Program to print 1 to 10 using Recursion.

Explanation :
Recursion means calling a method within itself until it returns a value.

Code:

public class Print
{
       public static void recursive(int n)
      {
        if(n <= 10)
       {
              System.out.println(n);
              recursive(n+1);
       }
       }

       public static void main(String args[])
      {
       recursive(1);
       }
 }

Program to reverse a string without using String API.

Explanation :
If str ="Hello world" then output will be "dlrow olleH".

Code:

public class Reverse
{
       public static void main(String[] args)
      {
           String str="Hello world";
           String rev="";
          for(int i=str.length()-1;i>=0;--i)
          {
            revs +=str.charAt(i);
           }

           System.out.println(rev);
}
}

Program to swap two numbers without using Temporary Variable.

Hint:
Use Addition and Subtraction Operators.

Explanation :
If n=4,m=5 then output will be n=5,m=4.

Code:

public class Swap
{
       public static void main(String args[])
      {
         int x = 10;
         int y = 20;
         x = x+y;
         y = x-y;
         x = x-y;
        System.out.println("After swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
     }
}

Program to know whether a number is Even or Odd.

Explanation :
If n=2 then it is an even Number.
If n=21 then it is an odd Number.

Code:

public class EvenOdd 
{
     public static void main(String[] args)
    {
     Scanner in = new Scanner(System.in);
     System.out.println("Enter a number :");
     int n = in.nextInt();
     if(n%2==0)
        System.out.println(n+" is an even number.");
     else
        System.out.println(n+" is an odd number.");
    }
}

Program to print Floyd Triangle.

Explanation :
If n=4 then output will be
1
2 3
4 5 6
7 8 9 10

Code:

import java.util.Scanner;
public class FloydTriangle
{
public static void main(String[] args)
{
     Scanner in = new Scanner(System.in);
     System.out.println("Enter the number of rows: ");
     int r = in.nextInt();
     int n=0;
     for(int i=0; i<r; i++)
     {
        for(int j=0; j<=i; j++)
        {
           System.out.print(++n+" ");
        }
        System.out.println();
        }
     in.close();
}
}

Program to know whether a number is Perfect Number or not.

Explanation :
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
The first perfect number is 6, because 1, 2 and 3 are its proper positive divisors, and 1 + 2 + 3 = 6.

Code:

import java.util.Scanner;
public class PerfectNumber
{
    static void calc(int n)
    {
     int a=0;
      for(int i=1;i<=n/2;i++)
      {
        if(n%i == 0)
        {
           a=a+i;
        }
      }
      if(a == n) System.out.println(n+" is a perfect number");
      else System.out.println(n+" is not a perfect number");
    }

    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a Number:");
        int n=sc.nextInt();
        calc(n);
        sc.close();
    }
}

Program to know whether a number is Magic Number or not.

Explanation :
If n=1729 then find the sum of digits of the given number i.e,
(1 + 7 + 2 + 9 =19). Reverse of 19 is 91.
Then (19 X 91 = 1729).
If the obtained product value and the given input are same, then the given number is a magic number.

Code:

import java.util.Scanner;
public class MagicNumber
{
    public static void main (String args[])
    {
        int num, sum, rev;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the value for num:");
        num=sc.nextInt();
        MagicNumber m=new MagicNumber();
        sum = m.sumOfDigits(num);
        rev = m.reverse(sum);
        if (sum < 10)
        {
                if ((sum * sum) == num)
                {
                    System.out.println(num+" is a magic number");
                }
                else
                {
                    System.out.println(num+" is not a magic number");
                }
              
        }
        else if ((sum * rev) == num)
        {
            System.out.println(num+" is a magic number");
        }
        else
        {
            System.out.println(num+" is not a magic number");
        }
        sc.close();
    }
   
    public int sumOfDigits(int n)
    {
        int s = 0;
        while (n > 0)
        {
                s = s + (n % 10);
                n = n / 10;
        }
        return s;
    }

    public int reverse(int num)
    {
        int rev = 0;
        while (num > 0)
        {
                rev = (rev * 10) + (num % 10);
                num = num / 10;
        }
        return rev;
    }
}

Program to find the Second Maximum digit in a number without using Arrays.

Explanation :
1) If n=12 then output will be 1.
2) If n=71233568 then output will be 7.

Code:

import java.util.Scanner;
public class SecondMax
{
public static void main(String args[])
{
    Scanner sc=new Scanner(System.in);
    int max = 0,r=0;
    int secondmax = 0;
    System.out.println("Enter any number :");
    int n = sc.nextInt();
    int num=n;
     
    while(num % 10 != 0)
    {
        r = num % 10;
        if(r > max)
        {
            secondmax=max;
            max = r;
        }
        else if(r<max && r>secondmax)
        {
            secondmax=r;
        }
       
        num /= 10;
    }
    System.out.println("Maximum is :"+max);
    System.out.print("Second Maximum digit in "+n+" is :"+secondmax);
    sc.close();
}
}

Program to find the Lucky number of a Person based on Date Of Birth.

Explanation :
If Input is 15-Mar-1995 then the output will calculated as :
(1+5)+(0+3)+(1+9+9+5)=33 which will proceed to give output as 3+3=6 which is the Lucky Number.

Code:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class LuckyNumber
{
      public static void main(String[] args)
      {
         int n,rem,sum = 0;
         ArrayList<String> s=new ArrayList<String>();
         String s2;
         int date,year,month;
        
         //Taking Date as Input
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter the Date in the 15-Mar-1995 Format:");
         String day=sc.nextLine();
         StringTokenizer st=new StringTokenizer(day,",/- ");
         while(st.hasMoreTokens())
         {
             s2 =st.nextToken();
             s.add(s2);//Adding Date In the ArrayList as String Objects
         }
         date=Integer.parseInt(s.get(0));//Getting the Date
         s2=s.get(1);//Getting the Month from the Date
         s2.toLowerCase();
         year=Integer.parseInt(s.get(2));//Getting Year
        
         if(s2=="jan") month =1;
         else if(s2=="feb") month =2;
         else if(s2=="mar") month =3;
         else if(s2=="apr") month =4;
         else if(s2=="may") month =5;
         else if(s2=="jun") month =6;
         else if(s2=="jul" || s2=="july") month =7;
         else if(s2=="aug") month =8;
         else if(s2=="sep" || s2=="sept") month =9;
         else if(s2=="oct") month =10;
         else if(s2=="nov") month =11;
         else  month =12;
        
         n=date+month+year;
        
         while(n > 0)
         {
                     while(n != 0)
                     {
                          rem = n%10;
                          sum = sum+rem;
                          n=n/10;
                     }
                     if(sum > 9)
                     {
                            n = sum;
                            sum = 0;
                     }
          }
          System.out.println("Your Lucky Number is: "+sum);  
          sc.close();
    }  
}

Program to find the Sum of digits of a Given Number until a Single digit is obtained.

Explanation :
1) If n=12 then output will be 1+2=3.
2) If n=78 then output will be 7+8=15 which will further proceed to give the result as 1+5=6.

Code:

import java.util.Scanner;
public class SumOfDigits
{
    public static void main(String[] args)
    {    
         int sum=0,rem=0;
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter any Number:");
         int n=sc.nextInt();
          while(n > 0)
          {
                     while(n != 0)
                     {
                          rem = n%10;
                          sum = sum+rem;
                          n=n/10;
                     }
                     if(sum > 9)
                     {
                            n = sum;
                            sum = 0;
                     }
           }
          System.out.println("Sum: "+sum);
          sc.close();
    }  
}

Program to find the Previous date of an Entered Date.

Explanation :
1)If input = 12/12/1995 then output will be 11/12/1995.
2)If input = 1/1/1995 then output will be 31/12/1994.
3)If input = 31/11/1995 then output will be Invalid as November has only 30 days.
4)If input = 29/02/1995 then output will be Invalid as 1995 is not a Leap year.
5)If input = 1/03/1996 then output will be 29/02/1996 as 1996 is a Leap year.

Code:

import java.util.*;
public class Previousday
{
   public static void display(int d,int m,int y)
   {
       System.out.print("Previous day of Entered date is:"+d+ "-"+m+"-"+y);
   }
 
   public static void main(String args[])
   {
      System.out.println("Enter the date in DD-MM-YYYY Format:");
      Scanner sc=new Scanner(System.in);
      String s=sc.next();
      sc.close();
      StringTokenizer st=new StringTokenizer(s,"-/ ");
      int d=Integer.parseInt(st.nextToken());
      int m=Integer.parseInt(st.nextToken());
      int y=Integer.parseInt(st.nextToken());
      if(d==1)
      {
          if(m==1)
          {
              d=31;m=12;y=y-1;display(d,m,y);
          }
          else if(m==3)
          {
              if((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)))
              {
                  d=29;m-=1;display(d,m,y);
              }
              else
              {
                  d=28;m-=1;display(d,m,y);
              }
          }
          else if(m==2 || m==4 ||m==6 ||m==8 ||m==9 || m==11)
          {
              d=31;m=m-1;display(d,m,y);
          }
          else if(m==5||m==7||m==10||m==12)
          {
              d=30;m=m-1;display(d,m,y);
          }
          else System.out.print("Invalid date Format");
      }
      else if(d>1 && d<=31)
      {
          if(m==2 && d<=29)
          {
              if(d==29)
              {
                 if((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)))
                 {
                   d=d-1;display(d,m,y);
                 }
                 else System.out.print("Invalid date Format");
              }
              else
              {
                  d=d-1;display(d,m,y);
              }
          }
          else if((m==1 ||m==3||m==5||m==7||m==8||m==10||m==12) && d<=31)
          {
              d=d-1;display(d,m,y);
          }
          else if((m==4||m==6||m==9||m==11) && d<=30)
          {
              d=d-1;display(d,m,y);
          }
          else System.out.print("Invalid date Format");
      }
      else System.out.print("Invalid date Format");
   }
}

Program to know whether an Year is a Leap year or not.

Code:

public class Leapyear
{
        public static void main(String[] args)  
       {               
               Scanner sc=new Scanner(System.in);
                int year = sc.nextInt();
                if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
                        System.out.println("Year " + year + " is a leap year");
                else
                        System.out.println("Year " + year + " is not a leap year");
        }
}