Split number into digits in c programming





Extract digits from integer in c language

#include<stdio.h>
int main(){
  int num,temp,factor=1;

  printf("Enter a number: ");
  scanf("%d",&num);

  temp=num;
  while(temp){
      temp=temp/10;
      factor = factor*10;
  }

  printf("Each digits of given number are: ");
  while(factor>1){
      factor = factor/10;
      printf("%d ",num/factor);
      num = num % factor;
  }

  return 0;
}

Sample output:
Enter a number: 123
Each digits of given number are: 1 2 3






20 comments:

ajay said...

sir in this example we are able to extract all integer values except 0, i mean if i choose the number 120 then the output is only 1 2 . it does not display zero.how to remove this error

Unknown said...

#include


int main()
{
int r,sum=0,a[100],i=0,num,j;
printf("enter the number");
scanf("%d",&num);
while(num>0)
{
i++;
r=num%10;
num=num/10;
a[i]=r;

}
printf("each digit of a given number is: ");
for( j=i;j>=1;j--)
printf("%d ",a[j]);

return 0;
}

Anand said...

This is another solution.

#include
main()
{
int num, temp, foo = 1;
scanf("%d",&num);
temp = num;
while(num > 9)
{
foo *= 10;
num /= 10;
}
while(foo)
{
printf("%d ",temp/foo);
temp %= foo;
foo /= 10;
}
}

Prini Raj said...

#include
#include
int main()
{
int num,r,a[10],i=0,j;
clrscr();
printf("Enter a number:\n");
scanf("%d",&num);

while(num)
{
r=num%10;
a[i]=r;
num=num/10;
i++;
}
printf("After spliting:");
for(j=i-1;j>=0;j--)
printf("%d ",a[j]);
getch();
return 0;
}

Unknown said...

#include
#include

int main()
{
int n,c=0,i,num,temp;
printf("Enter the value of intger:- ");
scanf("%d",&n);
num=n;
for(;n>0;n/=10)
{
c++;
}
int a[c];
for(i=0;i=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}

Unknown said...

Can anyone dry run this this program to explain the logic? Help me please.

Unknown said...

#include
int main ( int argc, char* argv[] )
{
printf ( "Enter a Number :\t" ) ;
int nNumber ;
scanf ( "%i", &nNumber ) ;

int nBuffer ;
while ( nNumber != 0 ) { nBuffer *= 10 ; nBuffer += nNumber % 10 ; nNumber /= 10 ; }
printf ( "DIGITS SPLITTED :\n" ) ;
while ( nBuffer != 0 ) { printf ( "%i ", nBuffer % 10 ) ; nBuffer /= 10 ; }
}

Unknown said...

sier pplease explain the logics of the codes too it wil be easy for us to understand

Unknown said...

Splitting into Teams

During the Physical Education hour, PD sir Mr. Sundar has decided to conduct some team games. He wants to split the students in the class into equal sized teams. In some cases, there may be some students who are left out from teams and he wanted to use the left out students to assist him in conducting the team games.
For instance, if there are 50 students in the class and if the class has to be divided into 7 equal sized teams, 7 students will be there in each team and 1 student will be left out.
PD sir asks your help to automate this team splitting task. Can you please help him out?

Input Format:
Input consists of 2 integers. The first integer corresponds to the number of students in the class and the second integer corresponds to the number of teams.

Output Format:
Refer sample input and output for formatting specifications.

Sample Input and Output:
[All text in bold corresponds to input and the rest corresponds to output]
Enter the number of students in the class
60
Enter the number of teams
8
The number of students in each team is 7 and the number of students left out is 4
PLZ HELP ME TO WRITE THZ PROGRAM

Unknown said...

#include "stdio.h"
#include "conio.h"
int main ( int argc, char* argv[] )
{
int nStudent, nTeam ;
printf ( "Enter the number of students in the class" ) ;
scanf ( "%d", &nStudent) ;
printf ( "Enter the number of teams" ) ;
scanf ( "%d", &nTeam) ;

printf("The number of students in each team is %d and the number of students left out is %d", (nStudent/nTeam) , (nStudent%nTeam));
getch();
}


(nStudent/nTeam) -> This will give you the number of student in each team. Example 60/8 will return 7.
(nStudent%nTeam) -> This will give you number of student left out. Example 60%8 = 4.

Rizwan Khan said...

can anyone tell me , how can i save the separated digit as another integer?

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

take one array and save the value.
For example. declare one integer. int i=0; //at the top
declare one array int a[10];
and a[i++] = num/factor; //to save the number.

Unknown said...

Thank you..I was looking for this logic for so many days.

Unknown said...

Try this frndzz....It may be helpfull to you gayzz...
#include
#include
void main()
{
int num,rev=0,d,g,rev2=0;

printf("Enter the number: ");
scanf("%d",&num);
while(num>0)
{
d=num%10;
rev=rev*10+d;
num=num/10;
}
printf("The digits are: ");
while(rev>0)
{
g=rev%10;

printf("%d ",g);

rev=rev/10;
}
getch();






}

GSR said...

Why not the following
/* Extract Digits from a given number */
#include

void extractDigits(int n)
{
if(n == 0)
return;
else
{
extractDigits(n / 10);
printf("%d ", n % 10);
}
}

int main(void)
{
int n = 0;
int idx = 0;

scanf("%d",&n);

extractDigits(n);
return 0;
}

Unknown said...

Someone please explain this programme with output

#
#
{
int x=1;
clrscr();
printf("%d%d%d\n",x,(x=x+2),(x<<2));
x<<2;
printf("%d%d%d\n",++x,x++,++x);
getch();
}

mastan said...

#include
void main()
{
int n,dig;
print("N:");
scan("%d",&n);
while(n>0)
{
dig=n%10;
n=n/10;
}
print("Value :%d",dig);
}

Unknown said...

#include
int main(){
int num,temp,factor=1;

printf("Enter a number: ");
scanf("%d",&num);

temp=num;
while(temp){
temp=temp/10;
factor = factor*10;
}

printf("Each digits of given number are: ");
while(factor>1){
factor = factor/10;
printf("%d ",num/factor);
num = num % factor;
}

return 0;
}