Thread: Probelm while recursing

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    1

    Probelm while recursing

    HELLO FORUM,
    please let me know the problem in the following code, when input is given as 12 am getting one two one as output whats the problem with the code. thanks in advance

    PS:PLEASE FIND THE CODE IN ATTACHMENTS
    Code:
    #include<stdio.h>
    
    void fun(int n)
    {
    int rem;
    while(n!=0)
    {
    rem=n%10;
    n=n/10;
    fun(n);
    rem==1?printf("one\n"):printf("two\n");
    }
    
    }
    
    void main()
    {
    int x;
    scanf("%d",&x);
    fun(x);
    }
    Attached Files Attached Files
    • File Type: c n2w.c (179 Bytes, 110 views)
    Last edited by Salem; 04-04-2017 at 04:26 AM. Reason: code inlined

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    There is no problem with the code (except for the whole void main issue which you should fix).

    > when input is given as 12 am getting one two one as output
    The problem would seem to be your expectation.
    What did you expect?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're mixing iteration and recursion. Your "while" should be an "if".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Probelm
    By peacealida in forum C Programming
    Replies: 7
    Last Post: 04-01-2008, 07:12 AM
  2. C and ASM probelm
    By peckitt99 in forum C Programming
    Replies: 8
    Last Post: 11-02-2006, 06:33 AM
  3. Database probelm
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 03-15-2006, 07:26 PM
  4. Recursing main
    By gcn_zelda in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2003, 10:54 AM

Tags for this Thread