Wednesday, August 19, 2015

Java program to check if a year entered by user is a Leap year or not

/*
Given a year, return true if it is a leap year otherwise
return false. We can check that by noting that years that are multiples of 100 are not leap years,
unless they are also multiples of 400.
*/

import java.util.*;
public class program
{
    public static void main(String args[])
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the year you want to check:- ");
        int year = s.nextInt();
        if(test(year))
        {
            System.out.println("It is a leap year");
        }
        else
        {
            System.out.println("It is not a leap year");
        }
    }
    
public static boolean test(int year)
{
boolean is_divisible_by_4_and_not_by_100 = (year%4==0 && year%100!=0);
boolean divisible_by_400 = (year%400==0);
return (is_divisible_by_4_and_not_by_100 || divisible_by_400);
}

}

//Author: Mayank Rajoria


Thursday, June 25, 2015

A new blog with all my current projects!

Its been a long time since I have added any new programs to this blog and a major reason for it is that I haven't been programming in Java much lately. I moved on to C++ for competitive programming and also learnt swift for iOS development. I have now started a new blog Mayank's Corner where I now post my new updates and information about my projects. So go and give it a view!

https://mayanksworkshop.wordpress.com

Saturday, March 24, 2012

What is Java ?

Java is an Object Oriented Programming (OOP) language developed primarily by James Gosling and his colleagues at Sun Micro Systems. This language was initially called Oak.
In 1991, the Sun Micro Systems developed this complete language as a part of research work to develop software for consumer electronics. It was developed as a full-fledged programming language in which one can accomplish the same sort of tasks and solve similar problems like other languages like C++, BASIC etc. The main significance of Java is that it is platform independent, it has the capability to be moved easily from one system to another.

A Java program can be written in two ways :-

  1. Java Application : A java program developed by the user.
  2. Java Applet : Java programs that can be downloaded from the internet and can be run on user's system.


Basic features of the Java language :-

  1. It is an object oriented language.
  2. It is both compiled and interpreted.
  3. Its programs are platform independent.
  4. It is multithreaded language.
  5. It does not require any preprosser (#) or inclusion of header files.

Wednesday, March 21, 2012

Java program to print a particular line of the Pascal's Triangle


/*This Java program accepts a number from the user and
 * then prints the particular line of the
 * pascal's triangle onto the screen. More information
 * about pascal's triangle: http://en.wikipedia.org/wiki/Pascal's_triangle
 Sample :
 Enter the line number : 6
 1.0 5.0 10.0 10.0 5.0 1.0
 */

import java.util.*;

public class pascal
{
    public static void main()
    {
         Scanner sc=new Scanner(System.in);
         System.out.print("Enter the line number : ");
         int l=sc.nextInt();
         double a[]=new double[l];
         a[0]=1;
         int j,k;
         for(j=1;j<l;j++)
         {
             a[j]=1;
             for(k=j-1;k>0;k--)
             {
                 a[k]=a[k]+a[k-1];
             }
             a[0]=1;
         }
         for(j=0;j<l;j++)
         {
             System.out.print(a[j]+" ");
         }
    }
}

//Author : Mayank Rajoria

Wednesday, December 21, 2011

A Java program to print the largest and the smallest number among the 10 numbers entered

/*
 * This Java program accepts 10 integers and stores them in an
 * array. Then it prints the biggest and the smallest number
 * entered.
 */

import java.util.*;
class largest_smallest
{
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 10 numbers");
        int a[]=new int[10];
        int i;
        int small=0,larg=0;
        for(i=0;i<10;i++)
        {
            a[i]=sc.nextInt();
            if(i==0)
            {
                larg=a[0];
                small=a[0];
            }
           
            if(a[i]<small)
            {
                small=a[i];
            }
            if(a[i]>larg)
            {
                larg=a[i];
            }
        }
        System.out.println("Largest term = "+larg);
        System.out.println("Smallest term = "+small);
    }
}

//Author : Mayank Rajoria

Java program to print the initial letters of the words present in the string entered

/* This Java program accepts a String from the
 * user containing multiple words and then prints
 * a line containing all the initial letters of the
 * words present in the string separated by a dot.
 *
 * Sample Input: My name is Mayank
 * Sample Output: M.n.i.M.
 */

import java.util.*;
import java.lang.Object;
class initialLetter
{
    public static void main()
    {
        String s;
        System.out.println("Enter a String");
        Scanner sc=new Scanner(System.in);
        s=sc.nextLine();
        s=" "+s;
        int l=s.length();
        String ans="";
        char c='n';
        int i;
        for(i=0;i<l;i++)
        {
            c=s.charAt(i);
            if(c==' ')
            {
                ans=ans+s.charAt(i+1)+".";
            }
        }
        System.out.println("The final String: "+ans);
    }
}

//Author : Mayank Rajoria

Thursday, December 15, 2011

A Java program to print the biggest and the smallest integer from the 10 integers entered. (Java)

/**
 * This Java program accepts 10 integers and stores them in an
 * array. Then it prints the biggest and the smallest number
 * entered.
 */

import java.util.*;
class lergestSmallest
{
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 10 numbers");
        int a[]=new int[10];
        int i;
        int small=0,larg=0;
        for(i=0;i<10;i++)
        {
            a[i]=sc.nextInt();
            if(i==0)
            {
                larg=a[0];
                small=a[0];
            }
           
            if(a[i]<small)
            {
                small=a[i];
            }
            if(a[i]>larg)
            {
                larg=a[i];
            }
        }
        System.out.println("Largest term = "+larg);
        System.out.println("Smallest term = "+small);
    }
}

//Author : Mayank Rajoria

A Java program to print all prime numbers between 1 and 1000.

/**
 * This Java program to print all prime numbers between 1 and 1000.
 */

class prime1To1000
{
    public static void main()
    {
        int i,k,j;
        for(i=1;i<1000;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                System.out.println(i);
            }
        }
    }
}

//Author : Mayank Rajoria

A Java program to print all prime numbers between 1 and 100.


/**
 * This Java program to print all prime numbers between 1 and 100.
 */
class prime1To100
{
    public static void main()
    {
        int i,k,j;
        for(i=1;i<100;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                System.out.println(i);
            }
        }
    }
}

//Author : Mayank Rajoria
//https://mayanksworkshop.wordpress.com

A Java program to print the sum of all prime numbers between 1 and 1000

/**
 * This Java program calculates and prints the sum of all prime numbers
 * between 1 and 1000.
 */

class prime1To1000
{
    public static void main()
    {
        int i,k,j;
        double sum=0;
        for(i=1;i<1000;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                sum=sum+i;
            }
        }
        System.out.println("Sum = "+sum);
    }
}

//Author : Mayank Rajoria

A Java program to print the sum of all prime numbers between 1 and 100


/*
 * This Java program calculates and prints the sum of all prime numbers
 * between 1 and 100.
 */
class prime1To100
{
    public static void main()
    {
        int sum=0,i,k,j;
        for(i=1;i<100;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                sum=sum+i;
            }
        }
        System.out.println("Sum = "+sum);
    }
}

//Author : Mayank Rajoria

Friday, December 9, 2011

Sum of the series (1!)+(2!)+(3!)+...+(n!) in Java


/*Problem: Write a program in Java to find the sum of the series
 * using function name fact(int) to return the factorial
 * of the number in order to obtain each term.
 * The series :-
 * S=(1!)+(2!)+(3!)+...+(n!)
 */

import java.util.*;
class series3
{
    public static void main()
    {
        System.out.print("Enter the value of n : ");
        Scanner sc=new Scanner(System.in);
        int n,i;
        n=sc.nextInt();
        double S=0,f=0;
        for(i=1;i<n;i++)
        {
            f=fact(i);
            S=S+f;
        }
        System.out.println("The sum ="+S);
    }
   
    public static double fact(int num)
    {
        int f=1,j;
        for(j=1;j<=num;j++)
        {
            f=f*j;
        }
        return(f);
    }
}

//Author: Mayank Rajoria

Sum of the series (1/1!)+(1/2!)+(1/3!)+...+(1/n!) in Java

/*Problem: Write a program in Java to find the sum of the series
 * using function name fact(int) to return the factorial
 * of the number in order to obtain each term.
 * The series :-
 * S=(1/1!)+(1/2!)+(1/3!)+...+(1/n!)
 */
import java.util.*;
class series2
{
    public static void main()
    {
        System.out.print("Enter the value of n : ");
        Scanner sc=new Scanner(System.in);
        int n,i;
        n=sc.nextInt();
        double S=0,f=0;
        for(i=1;i<n;i++)
        {
            f=fact(i);
            S=S+(1/f);
        }
        System.out.println("The sum ="+S);
    }
   
    public static double fact(int num)
    {
        int f=1,j;
        for(j=1;j<=num;j++)
        {
            f=f*j;
        }
        return(f);
    }
}

//Author: Mayank Rajoria
//https://mayanksworkshop.wordpress.com

Sum of series (1*2)+(2*3)+(3*4)+...+((n-1)*n) in Java

/*Problem: Write a program in Java to find the sum of the series * using function name product(int,int) to return the product
 * of two numbers in order to obtain each term.
 * The series :-
 * S=(1*2)+(2*3)+(3*4)+...+((n-1)*n)
 */
import java.util.*;
class series1
{
    public static void main()
    {
        int n,i;
        System.out.print("Enter the value of n : ");
        Scanner sc=new Scanner (System.in);
        n=sc.nextInt();
        int S=0,pro=0;
        for(i=1;i<n;i++)
        {
            pro=product(i,i+1);
            S=S+pro;
        }
        System.out.println("The sum is "+S);
    }
   
    public static int product(int n1,int n2)
    {
        int mul;
        mul=n1*n2;
        return(mul);
    }
}
/*Output:
 Enter the value of n : 11
 The sum is 440
 */

//Author : Mayank Rajoria

Finding the factroial of a number using recursion (Java)

/* This  Java program uses 2 functions : main and fact. The main
 * function accepts a number from the user, calls the
 * fact function and prints the value(the answer) which it
 * recieves from the fact function.
 *
 * The fact function at first reciaves a number from the
 * main function and the keeps calling itself with a number,
 * 1 less then the number it first recieved.
 */

/*Output wndow :-
Enter a number : 10
The factorial of 10 = 3628800.0
/*
import java.util.*;
class factorialRecursion
{
    public static void main()
    {
        System.out.print("Enter a number : ");
        int n;
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        double ans=fact(n);
        System.out.println("The factorial of "+n+" = "+ans);
    }
  
    public static double fact(int num)
    {
        if(num==1)
        {
            return(1);
        }
        else
        {
            return(num*(fact(num-1))); //Calling itself again with a different value.
        }
    }
}
//Author : Mayank Rajoria

Wednesday, November 30, 2011

A program to print the longest common substring of the two given strings (Java)

//This Java program accepts two strings
//from the user and then prints the longest common
//substring of the two given strings.
//SUBSTRING: A substring of a string is a string that can
//be obtained by removing some characters from
//the beginning or the end of the given string. For example
// "uter" is a substring of "computer".
//
//Sample Output of program :-
/*
Enter the two strings :
i like computer
put it down

Longest Common Substring is :-
put
*/

import java.util.*;
class longestCommonSubstring
{
public static void main()
{
    System.out.println("Enter the two strings : ");
    String a,b;
    Scanner sc=new Scanner(System.in);
    a=sc.nextLine();
    b=sc.nextLine();
    int m=a.length();
    int n=b.length();
    int ar[][]=new int[m+1][n+1];
    int i;
    int j;
    for(i=0;i<m+1;i++)   
    for(j=0;j<n+1;j++)
    ar[i][j]=0;
   
   
    int l=0,x=0,y=0;
    for(i=1;i<m+1;i++)
    {
        for(j=1;j<n+1;j++)
        {
            if(a.charAt(i-1)==b.charAt(j-1))
            {
                ar[i][j]=ar[i-1][j-1]+1;
                if(ar[i][j]>=l)
                {
                    l=ar[i][j];  //l holds value of the largest number in the square which
                    x=i;         //is also the length of longest substring.x and y store its
                    y=j;         //co-ordinates.
                }
            }
        }
    }
   
    String lcs="";
    int k=0;
    for(i=l;i>=1;i--)     //Storing the largest common substring
    {
        if(ar[x][y]!=0)
        {
            lcs=a.charAt(x-1)+lcs;
            x--;y--;
        }
    }
    System.out.println("\nLongest Common Substring is :-");
    System.out.println(lcs);          //Printing the largest common substring
}
}

//Author : Mayank Rajoria

A Java program to find the length of the longest non decreasing subsequence from a group of integers (Java)

//This Java program first accepts a number from the user
//and then accepts that many integers. The program then, using
//dynamic programming, finds the length of the longest
//subsequence of integers such that they are not in
//decreasing order.
//SEQUENCE : A subsequence is a sequence that can
//be derived from another sequence by deleting some
//elements without changing the order of the remaining
//elements. For example, the sequence <1,4,6> is a
//subsequence of <2,1,5,6,4,7,6>.

//Sample Output :-
/*Enter number of integers : 8
Enter the integers :-

-2
5
3
4
10
7
9
12

Longest non-decreasing sequence is of length : 6
*/

import java.util.*;
class longestNonDecreasingSequence
{
public static void main()
{
    int n;
    System.out.print("Enter number of integers : ");
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    int a[]=new int[n];
    int i;
    System.out.println("Enter the integers :-\n");
    for(i=0;i<n;i++)a[i]=sc.nextInt();
    int s[]=new int[n];
    for(i=0;i<n;i++)s[i]=1;
   
    int j,k=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        {
            if(a[i]>=a[j])
            {
                k=s[j]+1;
                if(s[i]<=k)
                {
                    s[i]=k;
                }
            }
        }
    }
    int ans=a[0];;
    for(i=0;i<n;i++)
    {
        if(s[i]>=ans)
        {
            ans=s[i];
        }
    }
    System.out.print("\nLongest non-decreasing sequence is of length : "+ans);
}
}

//Author : Mayank Rajoria

Sunday, November 20, 2011

Printing a pyramid pattern of digits #12

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
/*Sample Input :6
Sample Output :
                0
             1 0 1
          2 1 0 1 2
       3 2 1 0 1 2 3
    4 3 2 1 0 1 2 3 4
 5 4 3 2 1 0 1 2 3 4 5   */

import java.util.*;
class pattern
{
public static void main()
{
    System.out.println("Enter a number");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=(2*(n-i));j++)
        {
            System.out.print(" ");
        }
       
        for(j=i;j>=0;j--)
        {
            System.out.print(j+" ");
        }
       
        for(j=1;j<=i;j++)
        {
            System.out.print(j+" ");
        }
        System.out.println();
    }
}
}

//Author : Mayank Rajoria

Saturday, November 12, 2011

Using Selection Sort to sort an array (Java)

//This Java program depicts the use of Selection
//sort technique to arrange the integers in
//an array in ascending order.

import java.util.*;
class selectionSort
{
static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter 10");
    int a[]=new int[10];
    int i;
    for(i=0;i<10;i++)
    {
        a[i]=sc.nextInt();
    }
    int min=0,j,k;

    for(i=0;i<10;i++)
    {
        for(j=i;j<10;j++)
        {
            if(a[i]>a[j])
            {
                k=a[i];
                a[i]=a[j];
                a[j]=k;
            }
        }
    }

    for(i=0;i<10;i++)
    {
        System.out.println(a[i]);
    }
}
}

//Author : Mayank Rajoria

Using Bubble Sort for an array (Java)

//This Java program depicts the use of Bubble
//sort technique to arrange the integers in
//an array in ascending order.

import java.util.*;
class bubbleSort
{
static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter 10 numbers :-");
    int a[]=new int[10];
    int i;
    for(i=0;i<10;i++)
    {
        a[i]=sc.nextInt();   //Accepting numbers
    }
    int min=0,j,k;

    for(i=0;i<10;i++)
    {
        for(j=0;j<9-i;j++)
        {
            if(a[j]>a[j+1])
            {
                k=a[j];           
                a[j]=a[j+1];   
                a[j+1]=k;      
            }
        }
    }

    for(i=0;i<10;i++)
    {
        System.out.println(a[i]); //Printing the numbers
    }
}
}

//Author : Mayank Rajoria