7 Replies - 18762 Views - Last Post: 23 July 2007 - 10:01 PM Rate Topic: -----

#1 raedbenz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 13-December 06

Separate an integer into its digits

Posted 21 July 2007 - 10:40 AM

Hi...
what is the code/way of separating an integer into its digits. e.g. if i enter the integer 4148,
i want the output

4 1 4 8

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Separate an integer into its digits

#2 scarecrowfeild   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 14-July 07

Re: Separate an integer into its digits

Posted 21 July 2007 - 11:36 AM

hi!
why don't you use loop.i already write the code and run it.
p/s:dont forget the space.it is pretty simple program.
Was This Post Helpful? 0
  • +
  • -

#3 zyruz   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 13-August 05

Re: Separate an integer into its digits

Posted 21 July 2007 - 04:09 PM

you want to use % I bet.

129 % 10 wil be 9, remove 9 from 129, then divide 120 with 10.
then it is 12.
12% 10 = 2. etc etc. until the number is 0.
probarly want to store the new int's in a array.

zy
Was This Post Helpful? 1

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6655
  • View blog
  • Posts: 30,975
  • Joined: 10-May 07

Re: Separate an integer into its digits

Posted 21 July 2007 - 04:11 PM

I would just use sprintf & dump it into an array.
Was This Post Helpful? 0
  • +
  • -

#5 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Separate an integer into its digits

Posted 21 July 2007 - 11:32 PM

View Postno2pencil, on 21 Jul, 2007 - 04:11 PM, said:

I would just use sprintf & dump it into an array.


Using % operator and loop is great way to solve this problem rather than dump it into an array imo.
Was This Post Helpful? 0
  • +
  • -

#6 raedbenz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 13-December 06

Re: Separate an integer into its digits

Posted 22 July 2007 - 01:17 AM

Quote

hi!
why don't you use loop.i already write the code and run it.
p/s:dont forget the space.it is pretty simple program


could you please specify more or show an example.
Thanks
Was This Post Helpful? 0
  • +
  • -

#7 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Separate an integer into its digits

Posted 22 July 2007 - 02:14 AM

View Postraedbenz, on 22 Jul, 2007 - 01:17 AM, said:

Quote

hi!
why don't you use loop.i already write the code and run it.
p/s:dont forget the space.it is pretty simple program


could you please specify more or show an example.
Thanks


this is what i've done but without loop:
#include<conio.h>
#include<stdio.h>

void main()
{
int a,b,c;


scanf("%d",&a);


b=a%1000;
c=a/1000;
printf("%d",c);

c=b/100;
b=b%100;
printf(" %d",c);


c=b/10;
b=b%10;
printf(" %d",c);

c=b%10;
printf(" %d",c);

getch();
}

This post has been edited by aaa111: 22 July 2007 - 02:16 AM

Was This Post Helpful? 0
  • +
  • -

#8 imamkomc   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 09-May 07

Re: Separate an integer into its digits

Posted 23 July 2007 - 10:01 PM

View Postaaa111, on 22 Jul, 2007 - 02:14 AM, said:

View Postraedbenz, on 22 Jul, 2007 - 01:17 AM, said:

Quote

hi!
why don't you use loop.i already write the code and run it.
p/s:dont forget the space.it is pretty simple program


could you please specify more or show an example.
Thanks


this is what i've done but without loop:
#include<conio.h>
#include<stdio.h>

void main()
{
int a,b,c;


scanf("%d",&a);


b=a%1000;
c=a/1000;
printf("%d",c);

c=b/100;
b=b%100;
printf(" %d",c);


c=b/10;
b=b%10;
printf(" %d",c);

c=b%10;
printf(" %d",c);

getch();
}

If you need loop try this code.

void main()
{
int a,b,c,check,num[10],length,loop,remainder;

a=4148;
//scanf("%d",&a);

loop=0;
do{
  check=powl(10,loop);
  loop++;
}while(check<=a);
length=(loop-1);
for(int i=1;i<=length;i++)
   	{
	  num[i]=a/powl(10,(loop-2)-(i-1));
	  remainder=a-num[i]*powl(10,(loop-2)-(i-1));
	  a=remainder;
	  cout<<num[i]<<" ";
	}
cout<<"\n";
getch();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1