×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
JAVA Byte Code Java Examples : Patterns Part 1
Java Examples : Number Series - Java
33050    Arnab De    06/07/2017

All The program of the series addition or multiplication is based on some mathematical logic. Programmers should found and understand the mathematics behind it. All the series have some starting value, some logic acts behind the increment of the start value and an end term value. Here we declare some variable sv, inc, n for the start value, increment, term end variable respectively.

softetechnologies

1+2+3+4+5+6+7+ …..+ n

import java.util.*;
public class sum1
{
public static void main(String [] args)
{
int sv=1, inc=1, I, n, s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the term No");
n=sc.nextInt();
for(i=0; i<10; i++)
{
s+=sv;
sv+=inc;
}
System.out.println(s);
}
}

softetechnologies

1+3+5+7+9+….+n

import java.util.*;
public class sum2
{
public static void main(String [] args)
{
int sv=1, inc=2, n, i, s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the term No");
n=sc.nextInt();
for(i=0; i<n; i++)
{
s+=sv;
sv+=inc;
}
System.out.println(s);
}
}

1-3+5-7+9-….

import java.util.*;
public class s1
{
public static void main(String [] args)
{
int sv=1, i,n,inc=2, s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the term No");
n=sc.nextInt();
for(i=0; i<n; i++)
{
if(i%2==0)
{
s+=sv;
}
else
{
s-=sv;
}
sv+=inc;
}
System.out.println(s);
}
}

2+4+6+8+10+….+n

import java.util.*;
public class sum3
{
public static void main(String [] args)
{
int sv=2, inc=2, i, n, s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the term No");
n=sc.nextInt();
for(i=0; i<10; i++)
{
s+=sv;
sv+=inc;
}
System.out.println(s);
}
}

softetechnologies

1+4+9+16+…+n2

import java.util.*;
public class sum4
{
public static void main(String [] args)
{
int sv=1, inc=2, n, i, s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the term No");
n=sc.nextInt();

for(i=0; i<10; i++)
{
s+=sv*sv;
sv+=inc;
}
System.out.println(s);
}
}

1+2+4+7+11+16+…..+ n

import java.util.*;
public class sum5
{
public static void main(String [] args)
{
int sv=1, inc=1, n, i, s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the term No");
n=sc.nextInt();
for(i=0; i<4; i++)
{
s+=sv;
sv+=inc;
inc+=1;
}
System.out.println(s);
}
}

softetechnologies

Design a class to overload a function series() as follows: [ICSE 2013]


(i) double series(double n) with one double argument and
returns the sum of the series.
sum = 1/1 + 1/2 + 1/3 + ….. 1/n
(ii) double series(double a, double n) with two double arguments
and returns the sum of the series.
sum = 1/a^2 + 4/a^5 + 7/a^8 + 10/a^11 ….. to n terms

public class Series
{
double series(double n)
{
double s=0;
for (int i=1;i<=n;i++)
{
s+= (double) 1/i;
}
return s;
}
double series(double a, double n)
{
double s=0;
int sv=1,inc=3;;
for (int i=1;i<=n;i++)
{
s+= (double) sv/Math.pow(a,(sv+1));
sv+=inc;
}
return s;
}
}

JAVA Byte Code Java Examples : Patterns Part 1
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
26/10/2021: 1+3+5+7+9+….+n There is a mistake in this problem please improve that code of for loop there should be i < n
By: Mayur Wakhare [mauyviy@gmail.com]
Thank You very much
Albert Einstein
It is the supreme art of the teacher to awaken joy in creative expression and knowledge.
Albert Einstein
2896
70.83
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     51489
25/06/2018     43416
01/01/2018     42548
28/06/2017     40298
02/08/2017     39093
01/08/2017     33336
06/07/2017     33050
15/05/2017     32451
14/07/2017     28635
11/09/2018     28387
softetechnologies