1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| /* Write a program to generate Harmonic Series.
Example :
Input - 5
Output - 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately) */
class HarmonicSeries{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
double result = 0.0;
while(num > 0){
result = result + (double) 1 / num;
num--;
}
System.out.println("Output of Harmonic Series is "+result);
}
} |
/* Write a program to generate Harmonic Series.
Example :
Input - 5
Output - 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately) */
class HarmonicSeries{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
double result = 0.0;
while(num > 0){
result = result + (double) 1 / num;
num--;
}
System.out.println("Output of Harmonic Series is "+result);
}
}
import java.io.*;
class h2
{
public static void main(String args[])
{
int n=Integer.parseInt(args[0]);
double r=0.0;
while(n>=0)
{
r=r+(double) 1 / n;
n–;
}
System.out.println(“output of harmonic series is ” + r);
}
}
i tried that above program but i’l get a Array index out of bounds exception error.. i dont know how to rectify that error…kindly rectify it …pls
public class display
{
public static void main(String ar[])
{
int a=1,i;
double s=0.0;
for(i=1;i<=5;i++)
{
s=s+a/i;
}
System.out.println("series="+s);
}
}
calculator program logics
public class HermonicSeries {
public static void main(String args[])
{
int i;
double result=0.0;
for(i=1; i<=5; i++)
{
result=result + (double) 1/i;
}
System.out.println("1+1/2+1/3+1/4+1/5 = "+result);
}
}
why we are using double????? in result= result
= 1/i;
why its not like result = result + 1/i;
compile: javac h2.java
run: java h2 8
see here, we are using command line arguments. so in command prompt after compiling we have run the program with java space , as i given a example above. if you are not interested in command line arguments then use “Scanner” class from “util” package.
this may be a good solution to yours type of output.
————————————————————————————-
class HarmonicSeries
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
double result = 0.0;
System.out.println(“Output of Harmonic Series is:”);
for (int i=1;i 0)
{
result = result + (double) 1 / num;
num–;
}
System.out.println(” = ” +result);
}
}
try this…
its good in look and better output.
——————————————————-
class HarmonicSeries
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
double result = 0.0;
System.out.println(“Output of Harmonic Series is:”);
for (int i=1;i 0)
{
result = result + (double) 1 / num;
num–;
}
System.out.println(” = ” +result);
}
}
class HarmonicSeries
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
double result = 0.0;
System.out.println(“Output of Harmonic Series is:”);
for (int i=1;i 0)
{
result = result + (double) 1 / num;
num–;
}
System.out.println(” = ” +result);
}
}
since int/int gives int.
use this : result = result + 1.0/i; i.e double/int gives double, and result is a double variable.
Genuinely great content. I merely stumbled upon your blog site and
sought tto note that I’ve surely preferred browsing your weblog content.
Yet again shortly No matter the reason I’ll be subscribing in your
nourish and I am hoping you prepare once more
It will just produce the final result by multiplying…….how to generate whole series like 1+1/2+1/3+1/4+1/5………
Any Idea Please? 🙂
Thank You soooo much
can you give the sum of following series 1/2+2/3+4/5+5/6+…….n terms
import java.util.*;
class series{
public static void main(String args[]){
int i,n,s=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter The Range = “);
n=sc.nextInt();
for(i=1;i<=n;i++)
s=i/(i+1);
System.out.println("The Sum = "+s);
}
}
import java.util.*;
class series{
public static void main(String args[]){
int i,n;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter The Range = “);
n=sc.nextInt();
for(i=1;i<=n;i++)
s=1/i;
System.out.println("The Sum = "+s);
}
}
How to print this series :- 7.7+77.77+777.777+7777.7777+77777.77777+777777.777777+7777777.7777777 pls do reply soon!!