Write a program to split number into digits

// Write a program to split number into digits
// Ex: 1234 -> 1 2 3 4
#include<stdio.h>
int main(){
    int n;
    printf("Enter a positive integer:");
    scanf("%d",&n); // input from user
    while(n<0){
         printf("Please enter positive integer only:");
         scanf("%d",&n);      
    }
    printf("Digits of given number are: ");
    if(n==0){
         printf("0");    
    }
    while(n>0){
         printf("%d ",n%10); // printing each digit
         n = n/10;
    }
    getch();
}

No comments:

Post a Comment