Odd Digits Reverse Pattern
The program must accept an integer N as the input. The program must print the odd digits from the last digit and remove the last digit of N then again print the odd digits from the last digit and remove the last digit and so on. If there is no odd digit in the original N then print -1 as the output.
Boundary Condition(s):
1 <= N <= 10^7
1 <= N <= 10^7
Input Format:
The first line contains N.
The first line contains N.
Output Format:
The first line contains either odd digits as per the condition separated by a space or -1.
The first line contains either odd digits as per the condition separated by a space or -1.
Example Input/Output 1:
Input:
12345
Input:
12345
Output:
5 3 1 3 1 3 1 1 1
5 3 1 3 1 3 1 1 1
Explanation:
In 12345, the odd digits 5,3 and 1 are printed.
After removing the last digit the integer becomes 1234.
In 1234, the odd digits 3 and 1 are printed.
After removing the last digit the integer is 123.
In 123, the odd digits 3 and 1 are printed.
After removing the last digit the integer is 12.
In 12, the odd digit 1 is printed.
After removing the last digit the integer becomes 1.
In 1, the odd digit 1 is printed.
In 12345, the odd digits 5,3 and 1 are printed.
After removing the last digit the integer becomes 1234.
In 1234, the odd digits 3 and 1 are printed.
After removing the last digit the integer is 123.
In 123, the odd digits 3 and 1 are printed.
After removing the last digit the integer is 12.
In 12, the odd digit 1 is printed.
After removing the last digit the integer becomes 1.
In 1, the odd digit 1 is printed.
Example Input/Output 2:
Input:
2468
Input:
2468
Output:
-1
-1
SOLUTION:-
#include<stdio.h>
#include <stdlib.h>
int main()
{
int n,flag=0;
scanf("%d",&n);
while(n!=0)
{
int a=n;
while(a!=0)
{
int x=a%10;
if(x%2!=0)
{
{
printf("%d ",x);
flag=1;
}
flag=1;
}
a=a/10;
}
n=n/10;
}
}
Comments
Post a Comment