Showing posts with label Digit Manipulation. Show all posts
Showing posts with label Digit Manipulation. Show all posts

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

Tuesday, October 4, 2011

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