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
Input Format:
The first line contains N.
Output Format:
The first line contains either odd digits as per the condition separated by a space or -1.
Example Input/Output 1:
Input:
12345
Output:
5 3 1 3 1 3 1 1 1
Explanation:
In 12345, the odd digits 5,and are printed.
After removing the last digit the integer becomes 1234.
In 1234, the odd digits 3 and are printed.
After removing the last digit the integer is 123.
In 123, the odd digits 3 and are printed.
After removing the last digit the integer is 12.
In 12, the odd digit is printed.
After removing the last digit the integer becomes 1.
In 1, the odd digit is printed.
Example Input/Output 2:
Input:
2468
Output:
-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;
           }
            a=a/10;
        }
        n=n/10;
   }


}

Comments

Popular posts from this blog

Two Matrix Spiral Print

Alphabets Positions Reversed

Odd Factors - (Error Identification) skillrack program id - 7306