Wednesday, May 8, 2019

DIGIT SEPARATION

Problem Description

 Tom has a homework.His class teacher gave homework in Extract digits from integer in c language.your task is to help tom in extracting it by giving integer number as an input.the output should contain the extracted digits separated by a space. 

Test Case 1 
Input (stdin) 
356 

Expected Output
 3 5 6 

Test Case 2 
Input (stdin) 
4787

 Expected Output 
4 7 8 7

Solution

#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;
}

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...