Largest Odd Number

 Largest Odd Number 

PROBLEM STATEMENT :

The program must accept a string S and print the largest odd number L present in S. If there is no odd number in S, the program must print 0 as the output. 

Boundary Condition(s): 

1 <= Length of S <= 100 

Input Format: 

The first line contains S. 

Output Format: 

The first line contains L or 0 as per the given condition. 

Example Input/Output 1: 
Input: () 
123456 
Output: 12345

Explanation: 

Here S = 123456, the odd integers present in the string 123456 are given below 


123 
12345 
23 

2345 
345 
45 

The largest odd integer in the string is 12345. So 12345 is printed as the output. 

Example Input/Output 2: 
Input: () 
4466 
Output: 0





                    


            1)    LEARN THRICE 

                                👇 

            2)    THINK TWICE

                                👇 

            3)    APPLY ONCE




SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { char s[101]; int ind=-1; scanf("%s",s); for(int i=0;i<strlen(s);i++) { if((s[i]-'0')%2==1) ind=i; } if(ind==-1) printf("0"); else for(int i=0;i<=ind;i++) printf("%c",s[i]); }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { string s ; cin >> s; int ind = s.size()-1; while(ind>=0 && (s[ind]-'0')%2==0) ind--; if(ind<0) cout << 0; else cout << s.substr(0,ind+1); }



JAVA

       

import java.util.*; import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner sc = new Scanner (System.in); String S; int flag=0; S=sc.next(); for(int i=S.length()-1;i>-1;i--){ if(Character.getNumericValue(S.charAt(i))%2!=0){ for(int j=0;j<=i;j++){ System.out.print(S.charAt(j)); } flag=1; break; } } if(flag==0){ System.out.print("0"); } } }



PYTHON

       

s=input().strip() l=len(s) lst=[] for i in range(l): for j in range(i,l): num=int(s[i:j+1]) if num%2!=0: lst.append(num) print(0 if lst==[] else max(lst))



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right