"Lab Recursive Method"
package s9226307;import javax.swing.JOptionPane;
public class Recursion
{
public static void main(String[] arg)
{
String PString=JOptionPane.showInputDialog("Enter a number n=");
int i=Integer.parseInt(PString);
System.out.println( Recursion.comput(i));
System.exit(0);
}
public static void sum(int i)
{
if(i == 1)
System.out.print(1);
else{
sum(i - 1);
System.out.print(" + "+ i * i);
}
}
public static int comput(int i)
{
if(i == 1)
return 1;
else
return (comput(i - 1) + i * i);
}
}
0 Comments:
Post a Comment
<< Home