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. :)