Program to demonstrate DataInputStream class

import java.io.DataInputStream;   // to load DataInputStream class

class P22A
{   
    public static void main(String args[])
    {
        int x,y;
        // creating object of class DataInputStream.
                   DataInputStream in = new DataInputStream(System.in);

        try
           {

            System.out.print(" Enter a number  : ");
            x = Integer.parseInt(in.readLine());
   
            if(x>=0)
            {
                 y=1;
                 if(x==0)     // nested if
                    y=0;
            }
            else
                y=-1;

            System.out.println(" x = "+x+"   y = "+y);
        }
        catch(Exception e) {  System.out.println("I/O Error");   }
    }
}

/* Note : This program might give some Note at compile time, ignore it and go ahead. */

READ MORE - Program to demonstrate DataInputStream class

Program to illustrate Harmonic series : 1 + 1/2 + 1/3 + ......+ 1/n

Program to illustrate Harmonic series : 1 + 1/2 + 1/3 + ......+ 1/n

import java.io.DataInputStream;   // to load DataInputStream class


class P21
{
    public static void main(String args[])
    {
        int n;
        float temp,sum=0.0F;

        // creating object of class DataInputStream.
                DataInputStream in = new DataInputStream(System.in);
       
        System.out.println("Harmonic series : 1 + 1/2 + 1/3 + ......+ 1/n \n ");

            try
           {

        System.out.print(" Enter a number  : ");
        n = Integer.parseInt(in.readLine());
       
        for(int i=0;i<n;i++)           
            sum= sum + (float)1/(i+1);
       
        System.out.println("\nHarmonic series for "+n+" is : "+sum);

        }
        catch(Exception e) {  System.out.println("I/O Error");   }
    }
}

/* This program might give some Note at compile time, ignore it and go ahead.*/
READ MORE - Program to illustrate Harmonic series : 1 + 1/2 + 1/3 + ......+ 1/n

Program to illustrate the study of approximate level of intelligence of a person

 Program to illustrate the study of approximate level of intelligence of a person

class P19
{
    public static void main(String args[])
    {
        int y;
        double i,x;

        System.out.println("  i \ty \tx");
        System.out.println("----------------------");
        for(y=1;y<=2;y++)
            for(x=5.5;x<=12.5;x=x+0.5)
            {
                i=2+(y+0.5*x);
                System.out.println(" "+i+"\t"+y+"\t"+x);
            }
    }
}


       
READ MORE - Program to illustrate the study of approximate level of intelligence of a person

Program to display whether a number is prime or not

Program to display whether a number is prime or not

class P17
{
    public static void main(String args[])
    {
        int i,num;

        num = 23;
        System.out.print(" The Number "+num+" is ");
        i=2;
        while(i<=num-1)
        {
            if(num%i==0)
            {
                System.out.print("Not a prime Number.");
                break;
            }
            i++;
        }
        if(i==num)
            System.out.print("Prime Number.");
    }
}
READ MORE - Program to display whether a number is prime or not

Program to check whether the number is an Armstrong number

 Program to check whether the number is an Armstrong number

class P16
{
    public static void main(String args[])
    {
        int sum=0,ch,i,j,num,no;

        no= 153;
        num=no;

        do
        {
            sum=sum+(no%10)*(no%10)*(no%10);
            no=no/10;
        }
        while(no!=0);

        if(sum==num)
            System.out.println("The number "+num+" is an Armstrong number ");
        else
            System.out.println("The number "+num+" is not an Armstrong number ");
    }
}

       
READ MORE - Program to check whether the number is an Armstrong number

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|