ODD (100 Marks)
You have to create a function odd(). The function should print only the odd numbers and should be able to handle the variable number of arguments.
odd(int, int)
odd(int, int ,int)
odd(int, int ,int, int)
odd(int, int ,int, int, int)
Input Format
There are 5 lines of input with each having an integer.
Constraints
1<= N <=50
Output Format
For each function call print the odd number in the arguments only space separately. If there is no odd integer print an empty line.
Sample TestCase 1
Input
1
2
3
4
5
Output
1
1
1 3
1 3
1 3 5
- Solution:
import java.util.*;
public class CandidateCode {
public static void odd(int ...a){
int temp[] =temp=new int[a.length];
int c=0,k=0;
for(int i=0; i<a.length; i++){
if(!(a[i]%2==0)){
c++;
temp[k++]=a[i];
}
for(int j=0; j<c; j++){
if(j!=c-1)
System.out.print(temp[j]+ " ");
else
System.out.print(temp[j]);
// if(i!=a.length-1)
System.out.println();
}
}
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int p=sc.nextInt();
int q=sc.nextInt();
int r=sc.nextInt();
int s=sc.nextInt();
int t=sc.nextInt();
odd(p,q,r,s,t);
}
}
No comments:
Post a Comment