Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my purpose to write this code ist that to have 3 line as output with 3 recursive method.
first line 1,2,3,4,5,5,6,..,n,
second line 1,4,9,...,n*n,
third line 2,4,6,8,10,...,n,

but I do not know where is incorrect? and how to separate every line?

What I have tried:

Java
public class Recursiv {

	

	private static int prt1234(long n) {
		if (n == 1) {

			return 1;
		} else {

			return prt1234(n-1);

		}
	}

	

	private static int prtSqr1234(long n) {

		if (n == 0) {

			return 0;
		} else {

			return (int) (prtSqr1234(n - 1) - 1 + (2 * n));

		}
	}

	private static int prt2468(long n) {

		if (n % 2 == 0) {
			if (n <= 2) {

				return 2;
			} else {

				return prt2468(n - 2);

			}
		} else {
			n = n - 1;
			if (n <= 2) {

				return 2;
			} else {

				return prt2468(n - 2);

			}

		}

	}
	public static void main(String[] args) {
		Recursiv f = new Recursiv();
		for(long i = 1; i < Long.parseLong(args[0]); i++) {
            System.out.print( f.prt1234(i)+ ",");
        
        }
		
	        for(long j = 1; j < Long.parseLong(args[0]); j++) {
	            System.out.printf( prtSqr1234(j)+ ",");
	        
	        }
	        for(long k = 1; k < Long.parseLong(args[0]); k++) {
	            System.out.print( f.prt2468(k)+ ",");
	        
	        }
		
		
	}
}



Second solution
public class Recursive {

	private static int prtSqr1234(long m) {

		return (int) (m == 0 ? 0 : prtSqr1234(m - 1) + m + m - 1);

	}

	public static void main(String[] args) {

		for (long j = 1; j <= Long.parseLong(args[0]); j++) {
			System.out.print(prtSqr1234(j) + ",");
		}

	}
}
Posted
Updated 26-Nov-18 22:17pm
v4
Comments
Afzaal Ahmad Zeeshan 26-Nov-18 2:02am    
And the problem with this code is? You would have to use 3 function calls to perform this, one in each line.
Mohibur Rashid 27-Nov-18 2:22am    
why have you added an for loop? I can see you have not learn anything from my example. all you are trying to do is get answer from other people. It's not going to happen.
Member 13817762 27-Nov-18 3:20am    
I am sorry , that I am not in age 65 as clever as you.
Mohibur Rashid 27-Nov-18 3:22am    
Abusive or rude behavior will not be accepted.

1 solution

Quote:
And correct my recursive method?

Correcting your code is your job, the debugger is the tool that will help you to.
You are free to create code as complicated as you want, but do not expect us to try to follow you this way.
Why do you need recursive methods ?

you want:
prt1234(1)=1
prt1234(2)=2
prt1234(3)=3
prt1234(4)=4
prt1234(5)=5
The problem is that nobody will have the idea to look at recursive method to answer this, because there is a much simpler solution.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

a href="https://en.wikipedia.org/wiki/Debugger">Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900