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