Series Program in Loop Program – 2

Series loop program are very useful for high programming. In this blog we will see hard programs. We already know simple blog in last blog.

In this blog, we will discuss inbuilt Math class and pow(i,2) method. Math is inbuilt class and this class has many methods like sqrt(), cos(), tan() etc. We will discuss on pow() method.

 

Series Loop Program: 1^2+2^2+3^2+…+n^2 = ….

class loop8

{

public static void main(String args[])

{

int i;

int n = Integer.parseInt(args[0]);

double sum=0;

for(i=1;i<=n;i++)

{

//sum = sum + i*i;

sum = sum + Math.pow(i,2); // inbuilt class and method

}

System.out.println(“1^2+2^2+…+”+n+”^2=”+sum);

}

}

Output:

Command line Value is: 5

 1^2+2^2+…+5^2 = 55

 

Series Loop Program: 1! + 2! + 3! + … + n! = …..

class loop9

{

public static void main(String args[])

{

int i;

int n = Integer.parseInt(args[0]);

int sum=0,fact,j;

for(i=1;i<=n;i++)

{

fact = 1;

for(j=1;j<=i;j++)    // Nested Loop

{

fact = fact * j;

}

sum = sum + fact;

}

System.out.println(“1!+2!+…+”+n+”!=”+sum);

}

}

Output:

Command line Value is: 5

1!+2!+…+”+5+”!= 153

 

List Of Tutorial on Series Loop Program.

NoTitle
11^2-2^2+3^2-…+-n^2 = ….
21^1+2^2+3^3+…+n^n = ….
3-1^1+2^2-3^3+…+-n^n = ….
41! – 2! + 3! – 4! + 5! – 6! + 7! = ……

 

If you have problem on this blog, you can directly comment on it. This tutorial will help you understand the concepts of java loop programs.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

177total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint