What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 38891 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > How to print 1 to 100 number without using forloop in C# ...
Shubham

How to print 1 to 100 number without using forloop in C#

 Code Snippet posted by: Shubham | Posted on: 3/3/2012 | Category: C# Codes | Views: 1462 | Status: [Member] | Points: 40 | Alert Moderator   


using System;
class recurs
{
public void recursivePrint(int i)

{
if (i <= 100)
{
Console.WriteLine(i + "");
i++;
recursivePrint(i);

}
}
}
class neww
{
public static void Main()
{
recurs re = new recurs();
re.recursivePrint(1);
Console.ReadLine();
}

}

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar
Found interesting? Add this to:


 Responses

T.Saravanan
Posted by: T.Saravanan | Posted on: 3/4/2012 | Level: Silver | Status: [Member] [MVP] | Points: 10 | Alert Moderator 

Nice try. If i use '1000', the print method call '1000' times. In this time method parameter initialize more than time. Please compare with loop concept which is take more time.

Kindly post your code inside the code tag.

Thanks,
T.Saravanan

Puneet20884
Posted by: Puneet20884 | Posted on: 3/5/2012 | Level: Bronze | Status: [Member] | Points: 10 | Alert Moderator 

A simple way to illustrate the recursion and the very nice explanation by the moderator too why not efficient.

A suggestion, if in the worst case this approach is required to be used, the class "recurs" should be taken as singleton one, so that at least the same object may not get created everytime.

Kindly correct me if not appropriate.

Best Regards,
Puneet Sharma - Infosys
Pune, India

Shubham
Posted by: Shubham | Posted on: 3/6/2012 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

i m agree wid your response but this code is example of recursion method simply you have to know how the recursion works thts why i m only gives example to print 1-100 digit.....
if u r print 1000 time thats is worst case for this program........
ithankssssss......

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 5:26:37 PM