Number Series

Need help with this

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

46 Replies - 2078 Views - Last Post: 17 August 2009 - 03:24 AM Rate Topic: -----

#1 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • 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);
}
}


please help me get the right output.

Is This A Good Question/Topic? 0
  • +

Replies To: Number Series

#2 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • 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);
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 FoodGood  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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" :blink:

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;
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • 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 


Was This Post Helpful? 0
  • +
  • -

#5 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • 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);


Was This Post Helpful? 1
  • +
  • -

#7 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • 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
Exception in thread "main"
Process completed.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Number Series

Posted 14 August 2009 - 07:37 PM

View Postmetalloid, 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 ???
Was This Post Helpful? 0
  • +
  • -

#9 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#11 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#13 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Number Series

Posted 15 August 2009 - 08:24 AM

View Postmetalloid, 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);
		}
}

Was This Post Helpful? 1
  • +
  • -

#14 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • 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?
Sorry if I ask about this I am still a student, lesson is kind of new to me.
Was This Post Helpful? 0
  • +
  • -

#15 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

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