Tuesday, 18 November 2014

Use Boolean in java programming

In java programming we use primitive type call Boolean. It can only return two possible value that is true and false. In programming some time it’s important to create an output that can use to control other operations, Boolean is help us on it.
Now let’s write a program
public class useBooleans {
public static void main(String args[])
{
boolean a;
a=true;//set a as true
System.out.println("a is "+a);
a=false;//set a as false
System.out.println("a is "+a);
}
}
Let’s see the output of this program
a is true
a is false
As we can see that when we set the value of variable a as ‘true’ it returns a as ‘true’. Same happen for ‘false’. Now if we are use this value in programming with conditional operators like ‘if-else’ or ‘while’ then it makes proper use of Boolean Variables. Let’s write a program
 public class useBooleans {
public static void main(String args[])
{
boolean a;
a=true;//set a as true
if(a)
{
System.out.println("This is executed when a "+a);
}
a=false;//set a as false
if(a)
{
System.out.println("This is executed when a "+a);
}
}
}
Let’s see the output of this program
This is executed when a true
In above program we can see that ‘if’ is executed when variable a is true. In programming relational operators return Boolean variable. For example we can write a program
public class useBooleans {
public static void main(String args[])
{
int a=5,b=9;
System.out.println("a>b is "+(a>b));
}
}
Let’s see the output of above program
a>b is false
in above program we can see that (a>b) is printed as false.

Sunday, 16 November 2014

Use Character Data type in java programming

In java we use ‘char’ to store the character data type in variables. Maybe it looks same like ‘char’ we use in c and c++ programming. But it’s different. In c and c++ programming we use ‘char’ that is 8 bit wide. Btu for java programming it is 16 bit wide. As java use Unicode to define characters. Unicode is define a fully international character set that defines all character found in human languages. As java is made to use in worldwide it is significant to use Unicode.
Let’s write a program
public class useChar {
public static void main(String args[])
{
char chr1,chr2;
chr1 = 66;
chr2 = 'B';
System.out.println("chr1 and chr2 presnts "+chr1+" and "+chr2);
}
}
Let’s see output of above program
chr1 and chr2 presnts B and B
In above program we can see that ‘chr1’ hold a number but it print a character. It’s happen because of ASCII value. The number 66 represent character ‘B’. The ASCII character set occupies the first 127 values in the Unicode character set.
We can do arithmetic operation with char value as it is can also be thought of as an integer type. Let’s try an example
public class useChar {
public static void main(String args[])
{
char ch;
ch = 65;
System.out.println("chr presnts "+ch);
ch++;
System.out.println("chr presnts "+ch);
}
}
Output of above program
chr presnts A
chr presnts B
In above program we use increment operator where ch is a character type. It shows that it’s possible to do arithmetic operations with character data types.

Saturday, 15 November 2014

Find area of a circle in java programming

Using arithmetic operation in java programming we can calculate area of a circle. To calculate the area of circle we use a mathematical operation. That is if a circle have radios of ‘r’ and area of circle is ‘a’ then the equation become
a = pi * r2
Here pi is constant whose value is 3.1416 approximately. Now let’s write the program to find out the area of any circle.
public class circleArea {
public static void main(String args[]) {
double pi, r, a;
r = 5;
pi = 3.1416;
a = pi * r * r;
System.out.println("area of your circle " + a);
}
}
In above program we create a public class ‘circleArea’. Now in program all variable taken as double for proper calculation. We take the value of radius ‘r’ is 5. Now let’s see the output of above program
area of your circle 78.54



Use floating point data types in java

When we are about to use a real number that have fractional precisions, we use floating point data types. In java programing there are two type of floating point types, they are ‘float’ and ‘double’. We are about to know them more briefly one by one.
Float 
The data type float specifies a single-precision value that uses 32 bits of storage. It is useful for small value calculations. We write ‘float’ before any variable to declare as float.
Let’s see an example
public class usefloat {
public static void main(String[] args)
{
float a=25.345f,b=12.3654f,c;
c=a*b;
System.out.println(c);
}
}
Output of above program
313.40106
Double
The data type double specifies a faster precision than single precision uses 64 bits to store a value. 'double’ have been optimized for high-speed mathematical calculations. When you need to maintain accuracy over any iterative calculations, or are manipulating large-valued of real numbers, double is the best choice.
Let’s see an example
public class usedouble {
public static void main(String args[]) {
double pi, r, n;
r = 5.32;
pi = 3.1416;
n = pi * r * r;
System.out.println("area of circle " + n);
}
}
In above program we calculate radius of a circle. We write ‘double’ before any variable to declare as a double. Now let’s see the Output of above program
area of circle 88.91481984000002

Monday, 10 November 2014

Use integer data types in java programming

In java programming we use four integer types - 'byte’, ‘short‘, ‘int’, ‘long’. All of this integers holds positive and negative values. We are going to know about them one by one with examples.

byte
The smallest type integer in java is byte. This is a signed 8-bit type and it has a range from –128 to 127. Byte is useful when working with raw binary data. We write ‘byte’ before variable to declare them as byte.
Let’s write a program to use byte
public class usebyte {
public static void main(String args[])
{
byte a=12,b=20;
System.out.print(a+b);
}
}
Output of above program
32

short
short is a signed 16-bit type and it has a range from –32,768 to 32,767. We write ‘short’ before variable to declare it as short. It is probably the least-used Java type.
Let’s write a program to use short
public class useshort {
public static void main(String args[])
{
short a=120,b=200;
System.out.print(a+b);
}
}
Output of above program
320

int
The most commonly used integer type is int. It is a signed 32-bit type that has a range From  –2,147,483,648 to 2,147,483,647. int is the best choice when time to use integer.
Let’s write a program to use int
public class useint {
public static void main(String args[])
{
int a=320,b=600;
System.out.print(a+b);
}
}
Output of above program
920

long
long is a signed 64-bit type and that has a range from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. We use long when ‘int’ is unable to hold the range of integer value.
Let’s write a program to use long
public class uselong {
public static void main(String args[])
{
long a=620,b=1200;
System.out.print(a+b);
}
}
Output of above program
1820

Wednesday, 22 October 2014

Use pow and sqrt functions in C programming

In programming some time we need to use some mathematical operations. In math.h header defines various mathematical functions. Some important of them are pow and sqrt.. function of pow and sqrt is already defined in math.h header file. Let see how to use them
POW
Function pow is used for find out power of any integer. For example if we need to find out 25 then we use it.
Now let’s write the code
#include<stdio.h>
#include<math.h>
void main()
{
    float x,y,z;
    printf("enter the value");
    scanf("%f",&x);
    printf("enter the power");
    scanf("%f",&y);
    z=pow(x,y);
    printf("the answer is: %.2f",z);
}
The output of above program is
enter the value 2
enter the power 5
the answer is: 32.00
SQRT
Function sqrt used when we try to find out square root of any integer. For example if we need to find out √3 then we use it.
Now let’s write the code
#include<stdio.h>
#include<math.h>
void main()
{
    float x, z;
    printf("enter the value");
    scanf("%f",&x);
    z=sqrt(x);
    printf("the answer is: %.2f",z);
}
The output of above program is
enter the value 3
the answer is: 1.73

Friday, 17 October 2014

Find Factorial of a number using C programming

Find Factorial of a number using C programming
In mathematics, the factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n. For example,
    5! = 5 X 4 X 3 X 2 X 1 = 120.
Also factorial of 0! Is 1.
We are going to find out factorial by using C programming. Let’s see the code
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,k=1;
    printf("Enter your number");
    scanf("%d",&i);
    for(j=1;j<=i;j++)
    {
        k=k*j;
    }
    printf("Factorial of number %d is %d",i,k);
}
In above program we use for loop to calculate the factorial. When we insert any value in this loop that value will be multiplied by all other integer value less than inserted value until loop ends.
Now let’s see the output
Enter your number 5
Factorial of number 5 is 120