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

Thursday, October 27, 2011

Printing a pyramid pattern of digits #11

//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 :
1
22
333
4444
55555
666666   */


import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=1;
    for(i=1;i<=n;i++,k+=2)
    {
        for(j=1;j<=i;j++)
        {
            System.out.print(i);
        }
        System.out.println();
    }
}
}

//Author: Mayank Rajoria

Printing a dual pyramid pattern of '*' #4

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
/*Sample Input :7
Sample Output :

            *
          *A*
        *A*A*
      *A*A*A*
    *A*A*A*A*
  *A*A*A*A*A*
*A*A*A*A*A*A*   */



import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=1;
    for(i=1;i<=n;i++,k+=2)
    {
        for(j=n;j>i;j--)
        {
            System.out.print(" ");
        }
        for(j=1;j<=k;j++)
        {
            if(j%2==0)
            {
                System.out.print("A");
            }
            else
            {
                System.out.print("*");
            }
        }
        System.out.println();
    }
}
}

//Author: Mayank Rajoria

Printing a pyramid pattern of digits #10

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
/*Sample Input :7
Sample Output :

1234567
  123456
    12345
      1234
        123
          12
            1  */

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

//Author : Mayank Rajoria

Wednesday, October 26, 2011

Happy Diwali

Wishing all my fellow blog readers a very very Happy Diwali. Diwali, the festival of lights, commemorates Lord Rama's return to his kingdom of Ayodhya after completing 14 years' exile.
May this festival bring happiness to all your homes...

Tuesday, October 25, 2011

Printing a pyramid pattern of digits #9

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 7

Sample Output :
            7
          767
        76567
      7654567
    765434567
  76543234567
7654321234567 */
//For a left aligned version of this pattern
//visit: Java Programs: Printing a pyramid pattern of digits 8 (Java)

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=n,l=1;
    for(i=1;i<=n;i++)
    {
        for(j=n;j>=i+1;j--)
        {
            System.out.print(" ");
        }
        k=n;
        for(j=1;j<=i;j++,k--)
        {
            System.out.print(k);
        }
        k=k+2;
        for(j=1;j<=i-1;j++,k++)
        {
            System.out.print(k);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #8

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6

Sample Output :
6
656
65456
6543456
654323456
65432123456 */
//For a center aligned version of this pattern
//visit : Java Programs: Printing a pyramid pattern of digits 9 (Java)

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=n,l=1;
    for(i=1;i<=n;i++)
    {
        k=n;
        for(j=1;j<=i;j++,k--)
        {
            System.out.print(k);
        }
        k=k+2;
        for(j=1;j<=i-1;j++,k++)
        {
            System.out.print(k);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Monday, October 24, 2011

Printing a pyramid pattern of digits #7

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6

Sample Output :
          1
        121
      12321
    1234321
  123454321
12345654321 */

import java.util.*;
class pattern10

{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of lines you want : ");
    int n=sc.nextInt();
    int i,j,k;
    for(i=1;i<=n;i++)
    {
        for(k=i;k<n;k++)
        {
            System.out.print(" ");
        }
        for(j=1;j<=i;j++)
        {
            System.out.print(j);
        }
        for(j=i-1;j>=1;j--)
        {
            System.out.print(j);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #6

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

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

//Author : Mayank Rajoria

Printing a pyramid pattern of digits #5

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6
Sample Output :
1
121
12321
1234321
123454321
12345654321 */

import java.util.*;
class pattern
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of lines you want : ");
    int n=sc.nextInt();
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            System.out.print(j+" ");  //If you want you may remove the '+" "' part.
        }
        for(j=i-1;j>=1;j--)
        {
            System.out.print(j+" "); //If you want you may remove the '+" "' part.
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Sunday, October 23, 2011

IARCS Problem : Base b Arithmetic

/*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/BASE
 *
 *Note that this solution provided below
 *is in Java and not in C or C++
 */
import java.util.*;
class basebIARCS
{
public static void main()
{
    System.out.println("Enter the required information");
    Scanner sc=new Scanner(System.in);
    int b=sc.nextInt();
    int d1=sc.nextInt();
    int d2=sc.nextInt();
    int n1[]=new int[d1];
    int n2[]=new int[d2];
    int i,s1=0,s2=0,m;
    for(i=0;i<d1;i++)
    {
        n1[i]=sc.nextInt();
    }
    m=0;
    for(i=d1-1;i>=0;i--,m++)
    {
        n1[i]=n1[i]*((int)(Math.pow(b,m)));
        s1=s1+n1[i];
    }
   
    for(i=0;i<d2;i++)
    {
        n2[i]=sc.nextInt();
    }
    m=0;
    for(i=(d2-1);i>=0;i--,m++)
    {
        n2[i]=n2[i]*(int)(Math.pow(b,m));
        s2=s2+n2[i];
    }
   
    int pro=s1*s2;
    int q=pro,r;
    String ans="";
    int c=0;
    while(q!=0)
    {
        r=q%b;
        ans=r+" "+ans;
        q=q/b;
        c++;
    }
    System.out.println(c);
    System.out.println(ans);
}
}
//Author : Mayank Rajoria

Convert a decimal (base 10) number to any base (Java)

//This Java program accepts a decimal number
//i.e. base 10 number from the user and then
//accepts another number from the user and then
//converts the first number to a number whose base
//is the numerical value of the second number.
//NOTE : The second number should be
//between 2 and 10 only.
import java.util.*;
class convertToBase
{
public static void main()
{
System.out.print("Enter the decimal number : ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.print("Enter the base you would like to convert the number to : ");
int b=sc.nextInt();
int q=n,r;
String ans="";
while(q!=0)
{
r=q%b;
ans=r+ans;
q=q/b;
}
System.out.println(ans);
}
}
//Author : Mayank Rajoria

Finding quotient and remainder of two numbers (Java)

//This Java program accepts two numbers
//from the user and then  prints the quotient
//obtained by dividing the first number by the
//second and also prints the remainder.
import java.util.*;
class remainderAndQuotient
{
static void main()
{
int n,q,a,k=1,r;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 numbers");
n=sc.nextInt();
a=sc.nextInt();
r=n%a;
q=n/a;
System.out.println("Quotient : "+q);
System.out.println("Remainder : "+r);
}
}
//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

Sunday, October 16, 2011

Printing a dual pyramid pattern of '*' #3

//This Java program accepts a number from the
//user and prints a dual pyramid using '*' depending
//on the number entered by the user
/* Sample Input :8
Sample Output :
* ******
** *****
*** ****
**** ***
***** **
****** *    */

import java.util.*;
class pattern7
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the size of pattern you want : ");
    int n=sc.nextInt();
    int i,j;
    for(i=1;i<=(n-2);i++)
    {
        for(j=1;j<=(n);j++)
        {
            if(j==(i+1))
            {
                System.out.print(" ");
            }
            else
            {
                System.out.print("*");
            }
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a dual pyramid pattern of '*' #2

//This Java program accepts a number from the
//user and prints a dual pyramid using '*' depending
//on the number entered by the user
/*Sample input :6
Sample Output :
  *****
* ****
** ***
*** **
**** *
*****  
*/


import java.util.*;
class pattern
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the size of pattern you want : ");
    int n=sc.nextInt();
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i==j)
            {
                System.out.print(" ");
            }
            else
            {
                System.out.print("*");
            }
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #4

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input :4
Sample Output :
1
0 1
1 0 1
0 1 0 1 */

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,c=1,k=-1;
    int a=1,b=-1;
    for(i=1;i<=n;i++)
    {
        c=a;
        k=b;
        for(j=1;j<=i;j++)
        {
            System.out.print(c+" ");
            c=c+k;     
            k=k*(-1);  
        }
        System.out.println();
        a=a+b;     
        b=b*(-1);
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #3

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
//Sample Input :6
//Sample Output :
//1   2   3   4   5   6 
//7   8   9   10   11 
//12  13  14  15 
//16  17  18 
//19  20 
//21
import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int c=1;
    int i,j,k=1;
    for(i=n;i>=1;i--)
    {
        for(k=1;k<=i;k++)
        {
            System.out.print(c+"  ");
            c++;
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Wednesday, October 12, 2011

Finding a prime and palindrome number after a given number (Java)

//This Java program accepts a number 'n' from the user.
//This program then finds  the next smallest integer 'm' ≥'n' such
//that 'm' is a prime number and 'm' is a palindrome.
//Palindrome : An integer is said to be a palindrome if it is equal
//to its reverse. Eg 10201 is a palindrome.
//Sample Input : 45
//Sample Output : 101
import java.util.*;
class primePalindrome
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter a number");
    int n=sc.nextInt();
    int i,j,k,temp,n2=0,l=0;
    for(i=n;l==0;i++)
    {
        k=0;
        n2=0;
        for(j=2;j<i;j++)
        {
            if(i%j==0)
            {
                k=1;
                break;
            }
        }
        if(k==0)
        {
            temp=i;
            while(temp>0)
            {
                n2=(n2*10)+(temp%10);
                temp=temp/10;
               
            }
            if(n2==i)
            {
                System.out.println("The next prime and palindrome number is "+i);
                break;
               
            }
        }
    }
}
}
//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

Tuesday, October 4, 2011

Printing a pyramid pattern of digits #1

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
//Sample Input :5
//Sample Output :
//1 
//2  3 
//4  5  6 
//7  8  9  10 
//11  12  13  14  15 
import java.util.*;
class pattern
{
public static void main()
{
    int i,j,k;
    System.out.println("Enter the number of lines you want");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    k=1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            System.out.print(k+"  ");
            k++;
        }
        System.out.println();
    }
}
}
Author : Mayank Rajoria

Finding the sum of a number and the number formed by reversing its digits (Java)

//This Java program accepts a number from the
//user and stores it. The program then finds
//the sum of the original number and the
//number formed by reversing its digits.
//For example if 521 is entered, the sum of 521
//and 125 is found out and printed.
import java.util.*;
class sumOfOriginalAndReverseNumber
{
public static void main()
{
    System.out.println("Enter a number");
    Scanner sc=new Scanner(System.in);
    int num_orig=sc.nextInt();
    int num_new=0,n=num_orig;
    while(n>0)
    {
        num_new=(num_new*10)+(n%10);
        n=n/10;
    }
    System.out.println("The reversed number is "+num_new);
    int sum=num_orig+num_new;
    System.out.println("The sum of the original and reversed number is "+sum);
}
}
//Author : Mayank Rajoria

Displaying the reverse of a number (Java)

//This Java program accepts a number from the
//user and the prints the reverse of the
//number.
//Sample Input : 591
//Sample Output : 195
import java.util.*;
class reverseOfNumber
{
public static void main()
{
    System.out.println("Enter a number");
    Scanner sc=new Scanner(System.in);
    int num_orig=sc.nextInt();
    int num_new=0,n=num_orig;
    while(n>0)
    {
        num_new=(num_new*10)+(n%10);
        n=n/10;
    }
    System.out.println("The reversed number is "+num_new);
}
}
//Author : Mayank Rajoria

Friday, September 30, 2011

Finding whether a number is an Automorphic number (Java)

//This Java program accepts a number from the user and
//the tells the user whether the number is automorphic
//or not.
//An automorphic number is a number whose square "ends"
//in the same digits as number itself. eg 762 =5776
import java.util.*;
class automorphic
{
    public static void main(String Args[])
    {
        System.out.println("Enter a number");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int d=digits(n);
        if(n==((n*n)%Math.pow(10,d)))
        {
            System.out.println("The number is automorphic");
        }
        else
        {
            System.out.println("The number is not automorphic");
        }
    }
   
    public static int digits(int a)
    {
        int k=0;
        while(a>0)
        {
            a=a/10;
            k++;
        }
        return(k);
    }
}
//Author : Mayank Rajoria

Monday, September 26, 2011

Changing the Format of date(Java)

//This Java program accepts the date from the user in American
//format and then converts it to the European format.
//Sample input : 11/27/2011
//Output : The edited Date is : 27.11.2011
import java.util.*;
class changingDateFormat
{
public static void main(String Args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the date in month/day/year format");
String date=sc.next();
String day="",month="",year="";
int l=date.length();
int i,k=0;
char c;
String new_Date="";
for(i=0;i<l;i++)
{
c=date.charAt(i);
if((c!='/')&&(k==0))
{
month=month+c;
}
else if((c!='/')&&(k==1))
{
day=day+c;
}
else if((c!='/')&&(k==2))
{
year=year+c;
}
else if(c=='/')
{
k++;
}
}
new_Date=day+"."+month+"."+year;
System.out.println("The edited Date is : "+new_Date);
}
}
//Author : Mayank Rajoria

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

Accepting a number from user until a negative number is entered (Java)

//This Java program keeps accepting numbers from the
//user till the user enters a negative number. This program
//can by modified to do something meaningful with the
//entered data.
import java.io.*;
class noNegative
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.println("Enter the numbers");
n=Integer.parseInt(br.readLine());
for(;n>=0;)
{
n=Integer.parseInt(br.readLine());
}
System.out.println("You entered a negative number.");
}
}
//Author : Mayank Rajoria

Accepting numbers from user until a zero is entered(Java)

//This Java program simply keeps taking a number
//from the user unless the user enters a Zero. You could
//edit the program so that all these numbers are
//stored and something meaningfull is
//done of it.
import java.io.*;
class noZero
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.println("Enter the numbers");
n=Integer.parseInt(br.readLine());
for(;n!=0;)
{
n=Integer.parseInt(br.readLine());
}
System.out.println("You entered a zero :(");
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #2

//This Java program prints the following pattern
//1
//11
//111
//1111
//11111
class pattern
{
public static void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(1);
}
System.out.println();
}
}
}
//Author : Mayank Rajoria

Sunday, September 25, 2011

Printing a dual pyramid pattern of '*' #1

//This Java program accepts a number from the
//user and prints a dual pyramid using 'a' of
//the number of layers entered by the user
//Sample input : 4
//Sample output :     *
//                           * *
//                          * * *
//                         * * * *
//                          * * *
//                           * *
//                            *

import java.util.*;
class pattern
{
public static void main(String Args[])
{
int c,k,n,x,l;
System.out.println("Enter a number");
Scanner ob=new Scanner(System.in);
x=ob.nextInt();
l=x;
for(n=1;n<=x;n++)
{
for(c=l;c>1;c--)
{
System.out.print(" ");
}
for(k=1;k<=n;k++)
{
System.out.print("* ");
}
System.out.println();
l--;
}

int y;
for(n=x;n>1;n--)
{
for(y=n;y<=x;y++)
{
System.out.print(" ");
}
for(k=1;k<n;k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
//Author : Mayank Rajoria

Saturday, September 24, 2011

Vowels in a String (Java)

//This Java program accepts a String from
//the user and  then prints out the vowels
//present in it.
import java.util.*;
class vowel
{
    public static void main(String Args[])
    {
        System.out.println("Enter a string");
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        System.out.println("The vowels are :-");
        vowel(s);
    }
   
    public static void vowel(String st)
    {
        int i;
        for(i=0;i<st.length();i++)
        {
            if((st.charAt(i)=='a')||(st.charAt(i)=='u')||(st.charAt(i)=='o')||(st.charAt(i)=='i')||(st.charAt(i)=='e'))
            {
                System.out.println(st.charAt(i));
            }
        }
    }
}
//Author : Mayank Rajoria

Separating negative and positive integers from an Array (Java)

//This Java program accepts 10 integers and stores them in an
//array and then first prints the negative integers and
//then the positive integers in the way they are present in
//the array.
import java.util.*;
class separate
{
    public static void main(String Srgs[])
    {
        System.out.println("Enter 10 numbers");
        int a[]=new int[10];
        Scanner sc=new Scanner(System.in);
        int i;
        for(i=0;i<10;i++)
        {
            a[i]=sc.nextInt();
        }
        System.out.println("The new Sorted Array is :-");
        for(i=0;i<10;i++)
        {
            if(a[i]<0)
            {
                System.out.println(a[i]);
            }
        }
        for(i=0;i<10;i++)
        {
            if(a[i]>0)
            {
                System.out.println(a[i]);
            }
        }
    }
}
//Author : Mayank Rajoria

Finding HCF and LCM of two numbers (Java)

//This java program accepts two  from
//the user and prints their HCF and LCM
import java.util.*;
class hcfandlcm
{
    public static void main(String Args[])
    {
        System.out.println("Enter 2 numbers");
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();

        int h=1;
        int p=m*n;
        for(int i=2;i<p;i++)
        {
            if((m%i==0)&&(n%i==0))
            {
                h=i;
            }
        }
        int l=p/h;
        System.out.println("HCF="+h+" and LCM="+l);
    }
}
  

Finding HCF of two numbers

//This Java program accepts 2 numbers from
//the user and prints their HCF.
import java.util.*;
class hcf
{
    public static void main(String Args[])
    {
        System.out.println("Enter 2 numbers");
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();

        int h=1;
        int p=m*n;
        for(int i=2;i<p;i++)
        {
            if((m%i==0)&&(n%i==0))
            {
                h=i;
            }
        }
        System.out.println("HCF="+h);
    }
}
//Author : Mayank Rajoria

Friday, September 23, 2011

Check whether a number is Prime or not

//This java program accepts a number from the user
// and checks whether it is a prime number or not.
import java.util.*;
class primeNumber
{
    public static void main(String Args[])
    {
        System.out.println("Enter a number");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int ans=check(n);
        if(ans==1)
        {
            System.out.println("The number is a Prime number");
        }
        else if(ans==0)
        {
            System.out.println("The number is not a Prime number");
        }
    }
   
    public static int check(int a)
    {
        int i,k=1;
        for(i=2;i<a;i++)
        {
            if(a%i==0)
            {
                k=0;
                break;
            }
        }
        return(k);
    }
}
//Author : Mayank Rajoria

A menu driven arithmetic calculator

//This program is a menu based arithmetic calculator
//which is capable of accepting input from
//user.
import java.util.*;
class calculator
{
public static void main(String Args[])
{
int a;double n1,n2;
double f;
System.out.println("*********************Calculator*********************");
System.out.println("\n\n\n1. Addition\n2. Subtraction\n3. Division\n4. Multiplication\n5. Exit");
System.out.print("\nEnter the serial no. of the operation you would like to perform : ");
Scanner ob=new Scanner(System.in);
a=ob.nextInt();
switch(a)
{
case 1:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1+n2;
    System.out.println("Final Answer is : "+f);
    break;
case 2:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1-n2;
    System.out.print("Final Answer is : "+f);
    break;
case 4:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1*n2;
    System.out.print("The product is : "+f);
    break;
case 3:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1/n2;
    System.out.print("The Quotient is : "+f);
    break;
case 5:
    break;
default:
    System.out.println("Enter the correct no.");
    break;
}
}
}
//Author : Mayank Rajoria

Simple Arithmetic functions

//This program depicts the simple
//arithmetic functions of addition, subtraction, multiplication and
//division
import java.util.*;
class calculator
{
public static void main(String Args[])
{
int num1=250,num2=20;
double sum,product,difference,quotient;
sum=num1+num2;
product=num1*num2;
difference=num1-num2;
quotient=num1/num2;
System.out.println("Sum ="+sum);
System.out.println("Product="+product);
System.out.println("Difference="+difference);
System.out.println("Quotient="+quotient);
}
}

Let's begin, A Hello World Program

//This is a hello world program that prints the following: "Hello World!" and
//"Remember Java is awesome".
class helloWorld                //Program starts
{
public static void main(String Args[])
{
  System.out.println("Hello World!");
  System.out.println("Remember Java is awesome");
}
}

My first post!!

Hello People,
     This is my blog where I will be posting all kinds of java programs so that everyone can benefit from them. Hope this helps you guys in some way or the other.
      Always remember Computers and Programming is awesome. :)