Thread: C program Help!

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    C program Help!

    how to put space in a one variable.......

    like this!....pls help!

    int num;

    num=%d

    Enter a number (<=3000): 2968

    You have entered:
    2
    9
    6
    8
    2 + 9 + 6 + 8 = 25
    25 is an ODD number

    and 2968 is whole num of %d!....how to do it spacing a thousand number in a vertical line!.....
    Last edited by [Student]; 12-11-2010 at 05:24 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use the base number (10 in our case), and the modulus operator, to help:
    Code:
    2968 % 10 (our base number) equals 8 
    2968 /  10 equals 
    296
    
    296 % 10 equals 6
    296 / 10 equals
    29
    See how that can "peel off" the right most digit of the number, step by step?
    Code:
    a while loop is helpful:
    int i=0, count=0
    int digits[5]={0}
    
    while(number > 0) {
      digits[i++]=number % 10
      number /= 10
      count++;
    }
    Now the digits are in the digits[] array. To see them right, print it out from i=count to 0, instead of the normal way. (that is, counting downward from digits[count] to digits[0], in a for loop.

  3. #3
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    You could also read the number in as a string of characters. Limit input to some arbitrary size and then store them somewhere convenient, like a slightly larger array of char. Then loop through and print each one. Doing calculations on char is like working with int, but you will have to subtract '0' to get the value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM