# Number Series

• (4 Pages)
• 1
• 2
• 3
• Last »

## 46 Replies - 3440 Views - Last Post: 17 August 2009 - 03:24 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=120159&amp;s=650bf6ea2c681d9988e08945fcf68c0e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 metalloid

Reputation: -3
• Posts: 114
• Joined: 11-September 08

# Number Series

Posted 14 August 2009 - 04:56 AM

Make a program using a loop structure and procedure that would display the series of numbers as shown below. Using a loop structure and a method.

1 4 9 16 25 36 49 64 81 100

here is my code:
```import javax.swing.JOptionPane;

public class exammethod1
{
public static void main (String [] args) {
String output=" ";
for(int x=1;x<=2;x=x+2){
for(int y=1;y<=100;y=y+3){
output+=" "+y;}
}
JOptionPane.showMessageDialog(null,output,"Result",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Number Series

### #2 AbuJaFaR

• D.I.C Regular

Reputation: 13
• Posts: 330
• Joined: 13-December 07

## Re: Number Series

Posted 14 August 2009 - 05:22 AM

Everytime the value you must add for the next number increases by two.

```
public class example{
public static void main(String args[]){
String output=" ";
int x=1;
for(int i=3;i<=21;i+=2){
output+=" "+x;
x+=i;
}
System.out.println(output);
}
}

```

### #3 FoodGood

Reputation: 5
• Posts: 97
• Joined: 08-July 09

## Re: Number Series

Posted 14 August 2009 - 05:37 AM

This is what I understand from the sentence "Using a loop structure and a method"

```public class Main
{
public Main()
{
}

public static void main(String[] args)
{
int i=1;
for (;i<11;i++)
System.out.print(Power(i) + " ");
}

public static int Power(int a)
{
return a*a;
}
}
```

### #4 NoobKnight

Reputation: 49
• Posts: 238
• Joined: 14-July 09

## Re: Number Series

Posted 14 August 2009 - 07:09 AM

Here's another one.

```public class Main {

public static void main(String[] args) {
String result="";

for(int i=1;i<11;i++)  {
result = result + powerUP(i) + " ";
}
System.out.println(result);
}
public static int powerUP(int p)  {
return p*p;
}
```

Output:
```1 4 9 16 25 36 49 64 81 100

```

### #5 metalloid

Reputation: -3
• Posts: 114
• Joined: 11-September 08

## Re: Number Series

Posted 14 August 2009 - 05:54 PM

Thanks for all the help. Now that I have seen the program running, I need to make a method class and a main class about the program above.

Here is my method classs:
```import javax.swing.JOptionPane;

public class exammethod1
{
public String series(int x){
String output=" ";
x=1;
for(int i=3;i<=21;i=i+2){
output+=" "+x;
x=x+i;

}	return output;}
public void Result(String seriesout){
JOptionPane.showMessageDialog(null,seriesout,"output",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

```

And here is my main class:
```public class exammain
{
public static void main(String args[])
{
exammethod1 x=new exammethod1();
String seriesout=" ";
x.Result(seriesout);
}
}

```

All of those compiled but there is no output. Need help on this please.

### #6 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Number Series

Posted 14 August 2009 - 06:01 PM

```exammethod1 x=new exammethod1();
String seriesout= x.series(21);
x.Result(seriesout);

```

### #7 metalloid

Reputation: -3
• Posts: 114
• Joined: 11-September 08

## Re: Number Series

Posted 14 August 2009 - 07:26 PM

Still there is no output. JCreator gave this prompt after I attempted to run the program.

--------------------Configuration: exammethod1 - JDK version 1.6.0_14 <Default> - <Default>--------------------
java.lang.NoSuchMethodError: main
Process completed.

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Number Series

Posted 14 August 2009 - 07:37 PM

metalloid, on 14 Aug, 2009 - 04:54 PM, said:

Thanks for all the help. Now that I have seen the program running, I need to make a method class and a main class about the program And here is my main class:
```public class exammain
{
public static void main(String args[])
{
exammethod1 x=new exammethod1();
String seriesout=" ";
x.Result(seriesout);
}
}

```

You mean your "ecammain: class ???

### #9 metalloid

Reputation: -3
• Posts: 114
• Joined: 11-September 08

## Re: Number Series

Posted 14 August 2009 - 07:40 PM

Thanks a lot my good friends I have made run some how after doing revisions on what you have suggested that I should do. Thanks again.

### #10 metalloid

Reputation: -3
• Posts: 114
• Joined: 11-September 08

## Re: Number Series

Posted 15 August 2009 - 02:04 AM

One more thing, how do I convert this into recursion?

### #11 metalloid

Reputation: -3
• Posts: 114
• Joined: 11-September 08

## Re: Number Series

Posted 15 August 2009 - 07:44 AM

Make a program using a loop structure and procedure that would display the series of numbers as shown below. Using a loop structure and a method.

1 4 9 16 25 36 49 64 81 100

Here is my code:
```import javax.swing.JOptionPane;
public class exam2{
public static void main(String args[]){
int n=100;
int a=series(n);
String output= " ";
}
static int series(int x){
if (x==1)
return 1;
else
return x+series(x+2);
}
JOptionPane.showMessageDialog(null,output,"output",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
```

I'm stuck till this part only, it won't even compile.

### #12 prajayshetty

Reputation: 31
• Posts: 920
• Joined: 27-April 07

## Re: Number Series

Posted 15 August 2009 - 08:01 AM

```import javax.swing.JOptionPane;
public class NewClass{
static String	output;

public static void main(String args[]){

series(10);

}
static void  series(int x){
output="";
for(int i=0;i<=x;i++){

output=output+" "+i*i;

}
System.out.println(output);

JOptionPane.showMessageDialog(null,output,"output",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

```

hey dude as per to ur problem u need to just square your no now see
here:
1 =1*1;
2=2*2=4;
right see the above code should work

This post has been edited by prajayshetty: 15 August 2009 - 08:03 AM

### #13 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: Number Series

Posted 15 August 2009 - 08:24 AM

metalloid, on 15 Aug, 2009 - 01:44 PM, said:

Make a program using a loop structure and procedure that would display the series of numbers as shown below. Using a loop structure and a method.

1 4 9 16 25 36 49 64 81 100

I'm stuck till this part only, it won't even compile.

I see you are trying a recursive version - try this
```import javax.swing.JOptionPane;
public class exam2{
static String output= " ";
public static void main(String args[]){
int n=1;
int a=series(n);
JOptionPane.showMessageDialog(null,output,"output",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}

static int series(int x){
output+=x*x + " ";
if (x*x==100)
return 1;
else
return x+series(x+1);
}
}
```

### #14 metalloid

Reputation: -3
• Posts: 114
• Joined: 11-September 08

## Re: Number Series

Posted 15 August 2009 - 09:18 AM

Does this mean that recursion is multiplying the number by itself?

### #15 metalloid

Reputation: -3
• Posts: 114
• Joined: 11-September 08

## Re: Number Series

Posted 15 August 2009 - 06:32 PM

Making that program to run was just the first thing to do. Now I need to make a method class and a main class with the same program above, help me on this.

Method class:
```import javax.swing.JOptionPane;

public class exammethod2{
static int series(int x){
output+=" "+x*x;
if (x*x==100)
return 1;
else
return x+series(x+1);
}
static String output=" ";
public void Result(String seriesout){
JOptionPane.showMessageDialog(null,seriesout,"Output",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
```

Here is my main class:
```public class exammain2{
public static void main(String args[]){
exammethod2 x=new exammethod2();
String seriesout=x.series(100);
x.Result(seriesout);
}
}
```

The problem is the main class wont compile.