Category Archives: Fun with JAVA

Making a Calculator

import java.io.*;
public class Calcsi
{
public static void main(String[] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“********* 100 Digit Calculator *********”);
System.out.println(“Enter the equation”);
String eq = br.readLine();  //enter the equation you want to solve.
int num1 = 0,  i,  j, k = 0, l = 0, flag = 0;
char a[] = new char[]{‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’};  //array of characters to store the probable digits which maybe present in a no.
int num[] = new int[100];  //array for storing the nos in the equation
char symb[] = new char[100];   //array for storing the operands in the equation
if(eq.charAt(0) == ‘-‘)  //checking if the first no. is negative or not.
{
flag++;
i = 1;
} // if
else
i = 0;

loop1: for(; i < eq.length(); i++)
{
for(j = 0; j <= 9; j++)       //loop for checking the digits of a no.
{
if(eq.charAt(i) == a[j])
{

num1 = num1 * 10 + (int)(a[j] – 48);   //framing the no.
continue loop1;
}// if
}// loop 2

if(num1 != 0)
{

num[l] = num1;   //storing the first eq. no. in the array of nos.
l++;
num1 = 0;
if(flag > 0)     //if no. is negative
{
num[l – 1] = num[l – 1] – (num[l – 1] * 2);   //logic for converting a no from positive to negative.
flag = 0;
}// inner if
}// outer if

if(eq.charAt(i)== ‘+’)   // checking the next operation is addition or not
{
symb[k] = ‘+’;   //storing the operand in the operand array
k++;
if(eq.charAt(i + 1) == ‘-‘)  //checking if the next no in the eq. is negative or not
{
flag++;
i++;
continue;  //Going up to frame the next no.
}// inner if
}// outer if
if(eq.charAt(i)== ‘-‘)   //checking if the next operation is negative or not
{
symb[k] = ‘-‘;
k++;
if(eq.charAt(i + 1) == ‘-‘)   // Checking if the next no is negative or not
{
flag++;
i++;
continue; // Going up to frame the next no.
}// inner if
}// outer if
if(eq.charAt(i)== ‘/’)    // Checking if the next operation is division or not
{
symb[k] = ‘/’;
k++;
if(eq.charAt(i + 1) == ‘-‘) // Checking if the next no. is negative or not
{
flag++;
i++;
continue; // Going up to frame the next no.
}// inner if
}// outer if
if(eq.charAt(i)== ‘*’)  // Checking if the next operation is multiplication
{
symb[k] = ‘*’;
k++;
if(eq.charAt(i + 1) == ‘-‘)  // Checking if the next no is negative
{
flag++;
i++;
continue;  // Going up to frame the next number.
}// inner if
}// outer if
if(eq.charAt(i)== ‘=’)  //Checking if the next operation to perform is to get the answer
{
symb[k] = ‘=’;
k++;

}// if
}// loop 1
int sum = num[0];
for(i = 1; i < l; i++) // Loop for finding the answer
{
if(symb[i-1] == ‘+’)
sum = sum + num[i];  // Adding

if(symb[i-1] == ‘-‘)
sum = sum – num[i];  // Subtracting

if(symb[i-1] == ‘*’)
sum = sum * num[i];  // Multiplying

if(symb[i-1] == ‘/’)
sum = sum / num[i];  // Dividing
}// for

if(symb[i – 1] == ‘=’) // Finding the answer
System.out.print(sum);
}// Main
}// Class

Arrays

What are Arrays ?

I hope most of you know that what are arrays but do you really know it ?
Arrays are named memory location in which we can store a no. of elements of the same type in a single location or variable.

They are like boxes in which we can fill different thing instead of placing each object of same type in different places. They help us to ease the trouble of making different variables to store different values of same type. For example if you want to store first 100 natural nos, then you will have to make 100 different variables to store each natural no separately. But here arrays come to our rescue by helping us in storing all the first 100 nos to store in a single variable.

Syntax to declare and initialize an array.

int a[] = new int[size of array];

for(int i = 0; i < a.length; i++)

a[i] = Integer.parseInt(br.readLine());

Same way you can do this for other data types.

Now for static initialization

int a[] = new int[]{elements};

or

int a[] = {elements};

WAP(Write a program) to convert a binary no. to decimal form ;)

import java.io.*;
public class bin_dec
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String num = “10101111011110011110000000”;
int i, ab = 0, a, b ;
System.out.println(“No. in binary form = “+num);
b = num.length();
for(i = 0; b != 0; i++)
{
a = b-1;
ab = (int)(ab + ((num.charAt(a)- ‘0’) * Math.pow(2, i)));
– – b;
}
System.out.println(“No. in decimal form = “+ab);
}
}

Friends, this can be done by simple and single loop but when you will enter a long no. then the answer would be quite big because of which integer or long won’t be able to hold their value and the program will thus be incorrect.

Thats why i have taken the answer in string which can hold almost any amount of value any big amount of memory can be stored in a string type. Thus above program will always give you the correct answer. :D enjoy…if you have not understood then contact me at facebook https://www.facebook.com/rishabh.gagan.9… (y)