Thread: alter the result in the end

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    alter the result in the end

    i use codeblocks for compiling.The following is the code that I have written.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        printf("enter the number\n\n");
        scanf("%d",&n);
        printf("\n\n%d\n",n);
        while(n>0)
             {
    
                 printf("%d\n",n/10);
                 n=n/10;
    
             }
    
        return 0;
    }
    if 1234 is entered then it produces the following result

    1234
    123
    12
    1
    0

    now i want to have only upto 1 and eliminate o.I am not able to do this can anyone help me.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep.... divide before display and remove the /10 from the display line.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    i have changed it like this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        printf("enter the number\n\n");
        scanf("%d",&n);
        printf("\n\n%d\n",n);
        while(n>0)
             {
                 n=n/10;/**changed here**/
                 printf("%d\n",n);/**changed here**/
    
    
    
             }
    
        return 0;
    }
    But it still shows the same result

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by narendrav View Post
    i have changed it like this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        printf("enter the number\n\n");
        scanf("%d",&n);
        printf("\n\n%d\n",n);
        while(n>0)
             {
                 n=n/10;/**changed here**/
                 printf("%d\n",n);/**changed here**/
    
    
    
             }
    
        return 0;
    }
    But it still shows the same result
    Ok... so now modify the exit condition of your loop...

    Two things to try...
    first ... less than what?
    second ... switch the loop around to a do..while() loop and see what happens.

    One of the really fun things about programming is *experimenting* and seeing the results. Seriously, it's the absolute best way to learn.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ditch the printf before the loop and use a do-while. Inside the loop, print the undivided number then divide.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops that iterate/alter code in a binary fashion?
    By 1:1 in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2011, 06:32 PM
  2. Replies: 5
    Last Post: 05-15-2009, 02:36 AM
  3. What would be the result...
    By sreeramu in forum C Programming
    Replies: 8
    Last Post: 03-25-2008, 04:43 AM
  4. Alter Ego
    By valar_king in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-10-2003, 06:28 PM