Read Long Number and Split it Into Odd and Even
Problem Description
Input is accepted by variable n. Odd and even numbers are extracted from this variable n using pointers n1 and n2. The variable n is divided by 10 and the remainder is stored in variable r. The quotient obtained after this division is considered as variable n. Then variable r is then divided by 2 and if it returns 0 as remainder ,then the variable r is considered as even number and stored in pointer n2 .
If 0 is not obtained, then the variable r is considered as odd number and stored in pointer n1.
This process continuous until the variable n is decremented to 0
CODING ARENA
#include <stdio.h>
int main()
{
long *n,a,*n1,*n2,*m1,*m2,r,a1=0,a2=0,a3=1,a4=1;
scanf("%ld",&a);
n=&a;
n1=&a1;
n2=&a2;
m1=&a3;
m2=&a4;
while(*n!=0)
{
r=*n%10;
*n=*n/10;
if(r%2==0)
{
r=*m2*r;
*n2=*n2+r;
*m2=*m2*10;
}
else
{
r=*m1*r;
*n1=*n1+r;
*m1=*m1*10;
}
}
printf("%ld %ld",*n1,*n2);
return 0;
}
Test Case 1
Input (stdin)123456
Expected Output
135 246
Test Case 2
Input (stdin)1234
Expected Output
13 24
No comments:
Post a Comment