General

Chat

Quick help on a C programming problem

I need some halps with a practice problem for a C programming course I'm taking.
[url=http://imageshack.us/f/46/halps.png/]This is it.[/url]

I know it's just a simple problem for those who have any background in the language, but I'm definitely stuck here.
Any help would be appreciated!

March 11, 2013

3 Comments • Newest first

WetDuck

Thank you guys! Helped a bunch

edit: Haha that definitely cleared things up for me @Invictinite. Thanks again!

Reply March 11, 2013 - edited
Invictinite

You just % 10 store the digit, and then divide the number by 10 which will remove that digit because of truncating. Then you just keep repeating this until your original number = 0 and you have all your digits.

I can pseudo code it for you since I can't quite remember my C

input -> int number

int digit;
int i;
int check = 0;
w/e an array is[7];

for(i = 7; i >= 0; i--)
{ digit = number % 10;
array[i]<store digit;
number = number / 10;
}

for(i = 0; i < 8; i++)
{ if(check == 0 && array[i] != 0) //This will ignore all the 0s before the actual number if i did it correctly
{ check = 1;
} if(check == 1)
{ printf("%i",array[i]);
} }

Reply March 11, 2013 - edited
Chema

You can either distribute the int into an array then print it within a for clause
OR export it into a string, split the string then print it

Reply March 11, 2013 - edited