Java Programs

1. Java program to determine the sum of the following harmonic series for a given value of n: 1+1/2+1/3+. . . +1/n 

import java.io.*; 
class  Harmonic 

            public static void main(String[] args)throws IOException 
            { 
            BufferedReader br = new BufferedReader ( new InputStreamReader ( System.in ) ); 
                        String s; 
                        int n; 
                        float i,sum=0; 
                        System.out.print("\n Enter a number for the limit of harmonic series : "); 
                        s = br.readLine(); 
                        n = Integer.parseInt(s); 
                        for(i=1;i<=n;i++) 
                        { 
                                    sum+=1/i; 
                        } 
                        System.out.println("\n The Sum of Harmonic Series is  =  " + sum); 
            } 
}

 

2.  program for temparature conversion 

import java.io.*;
class pr1
{
    public static void main(String args[])throws IOException
    {
      DataInputStream in=new DataInputStream(System.in);
      int n,fh=37,cg=61,cal,ch;
      System.out.println("Enter Degrees");
      n=Integer.parseInt(in.readLine());
      System.out.println("1.Fohrenheit to Centigrade\n\n2.Centigrade to Fohrenheit\n");
      ch=Integer.parseInt(in.readLine());
      if(ch==1)
      {
        cal=n+cg;
        System.out.println(n+" Fohrenheit is "+cal+" Centigrade");
     }
     else
    {
       cal=n-cg;
       System.out.println(n+" Centigrade is "+cal+" Fohrenheit");
    }
  }
}

 

3.  program for sum and average of first counting numbers 

import java.io.*;
class suint
{
    public static void main(String args[])throws IOException
    {
       DataInputStream in=new DataInputStream(System.in);
       int n,sum=0,k;
       float avg;
       n=Integer.parseInt(in.readLine());
       for(int i=1;i<=n;i++)
        {
            k=Integer.parseInt(in.readLine());
            sum+=k;
        }
        avg=(float)sum/(float)n;
        System.out.println("sum of n no's is:"+sum);
        System.out.println("avarage of n no's:"+avg);
       }
}

 

4.  program for finding of factorial of a given number 


import java.io.*;
class facto
{
     public static void main(String args[])throws IOException
     {
        DataInputStream in=new DataInputStream(System.in);
        int n,fact=1;
        n=Integer.parseInt(in.readLine());
        for(int i=1;i<=n;i++)
         {
            fact=fact*i;
         }
        System.out.println(n+" Factorial is:"+fact);
     }
}

 

5.  program for checking for Polindrome number 


import java.io.*;
class poli
{
   public static void main(String args[])throws IOException
   {
     DataInputStream in=new DataInputStream(System.in);
     int n,t,r,rev=0;
     n=Integer.parseInt(in.readLine());
     t=n;
     while(n>0)
     {
         r=n%10;
         rev=rev*10+r;
         n=n/10;
     }
     if(t==rev)
        System.out.println(rev+"No is Polindrom:");
     else
        System.out.println(rev+"No is not Polindrom:");
     }

 

6.  program for generating fibonacci series


import java.io.*;
class fibo
{
      public static void main(String args[])throws IOException
      {
         DataInputStream in=new DataInputStream(System.in);
         int n,a=0,b=1,c=0;
         System.out.println("Enter range of Fibonacci Series");
         n=Integer.parseInt(in.readLine());
         System.out.print(a+"\t"+b);
         while(n>=c)
         {
             c=a+b;
               if(n>=c)
               System.out.print("\t"+c);
             a=b;
             b=c;
         }

      }
}

 

7.  Program for implementation of 1D array 


class Tf

    public static void main(String args[])
     { 
        int a[] = {3,4,5,6,7};
        int i,j;
        i=0;j=0;
        while(i<a.length)
         {
             j=j+a[i];
             i=i+1;
         }
         System.out.println(j);
     }
}