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