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