Showing posts with label Real Life Situations. Show all posts
Showing posts with label Real Life Situations. Show all posts

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


Wednesday, December 21, 2011

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

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

Monday, October 17, 2011

IARCS Problem : A Board Game

/*This program is a solution to the question
 * given by IARCS at their website.
 *
 * The link to the question is:http://www.iarcs.org.in/inoi/contests/oct2004/Basic-2.php
 *
 *Note that this solution provided below
 *is in Java and not in C or C++
 */
import java.util.*;
class BoardGame
{
public static void main()
{
    System.out.println("Enter the required info : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int m=sc.nextInt();
    char mark[]=new char[n];
    int i;
    String s;
    for(i=0;i<n;i++)
    {
        s=sc.next();
        mark[i]=s.charAt(0);
    }
   
    int v,pos=0,k=1;
    for(i=0;i<m;i++)
    {
        v=sc.nextInt();
        if(mark[pos]=='+')
        {
            if((pos+1+v)<=n)
            {
                pos=pos+v;
            }
        }
        else if(mark[pos]=='-')
        {
            if((pos+1-v)>=1)
            {
                pos=pos-v;
            }
        }
        if(pos==0)
        {
            k++;
        }
    }
    System.out.println(k);
}
}

//Author : Mayank Rajoria

IARCS Problem : BCCS Elections (Java)

/*This program is a solution to the question
 * given by IARCS at their website.
 *
 * The link to the question is : http://opc.iarcs.org.in/index.php/problems/BCCSELEC
 *
 *Note that this solution provided below
 *is in Java and not in C or C++
 */
import java.util.*;
class BCCSElection
{
public static void main()
{
    System.out.println("Enter the number of people");
    Scanner sc=new Scanner(System.in);
    int c=sc.nextInt();
    int n=sc.nextInt();
    int votes[]=new int[c];
    int i,v;
    for(i=0;i<c;i++)
    {
        votes[i]=0;
    }
    for(i=0;i<n;i++)
    {
        v=sc.nextInt();
        votes[v-1]++;
    }
    int k=0,j=0,max=0;
    for(i=1;i<=3;i++)
    {
        for(j=0;j<c;j++)
        {
            if(votes[j]>=k)
            {
                k=votes[j];
                max=j;
            }
        }
        votes[max]=0;
        k=0;
    }
    System.out.println(max+1);
}
}

//Author : Mayank Rajoria

Saturday, October 8, 2011

Finding total possible cable connections between buildings, given their height (Java)

/*The Problem : A cable TV operator is trying to save money by running cables
directly at roof level between pairs of buildings on the same side of the street.
A cable can be connected between the rooftops of two buildings A and B
if no building between them is strictly taller than either A or B.

For instance, suppose there are ?ve buildings on the street with the following heights
(in feet): 160, 145, 153, 170, 180. Let us label the ?ve buildings A, B, C, D and E. In
this case, six pairs of buildings can be connected by a TV cable at roof level: (A,B),
(C,D), (A,C), (B,C), (D,E), (A,D).

In each of the following cases, you are given the heights of the buildings on the street and
you have to compute the number of pairs of buildings that can be directly connected
by a TV cable at roof level. In the example above, the answer is 6.*/

import java.util.*;
class buildingHeightCable
{
public static void main()
{
    System.out.println("Enter the number of buildings");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i;
    int h[]=new int[n];
    for(i=0;i<n;i++)
    {
        h[i]=sc.nextInt();
    }
    int j,k=0;
    int f;
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            k=0;
            for(f=i+1;f<j;f++)
            {
                if((h[f]>h[i])||(h[f]>h[j]))
                {
                    k=1;
                }
            }
            if(k==0)
            {
                count++;
                System.out.println(h[i]+"  "+h[j]);
            }
            k=0;
        }
    }
    System.out.println("Total pairs = "+count);
}
}
//Author of program : Mayank Rajoria

Monday, September 26, 2011

Coin Toss result

//This Java program accepts a number from the user
//and then performs that many coin tosses and prints
//the result of each toss and finally prints
//the total number of heads and tails.
import java.util.*;
class toss
{
public static void main(String Args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of tosses you want");
int n=sc.nextInt();
int head=0,tail=0,i;
int result;
for(i=1;i<=n;i++)
{
result=(int)(Math.random()*2);
if(result==0)
{
System.out.println("Heads");
head++;
}
else if(result==1)
{
System.out.println("Tails");
tail++;
}
}
System.out.println("Final Result :-");
System.out.println("Total Heads = "+head+" Total Tails = "+tail);
}
}
//Author : Mayank Rajoria