Skip to content

Commit fe94969

Browse files
Create 33.2 VarArgs notdefined input in arguments_sum of odd number.java
1 parent 5a17f94 commit fe94969

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Puneet and Virat are playing a game. Virat tells some numbers to Puneet. Puneet needs to find sum of all numbers which are odd and discard even numbers. Write a program in which implement a method public int sumOddNumbers(int... n) which will return the sum of all odd numbers.
3+
4+
Input Format
5+
6+
Some integer values representing numbers given by Virat.
7+
8+
Constraints
9+
10+
Number will lie between 20 and 400.
11+
12+
Output Format
13+
14+
Sum according to the value returned by the method or will print Invalid Input in case of number did not match the constraints.
15+
16+
Sample Input 0
17+
18+
21 25 26 28
19+
Sample Output 0
20+
21+
46
22+
Sample Input 1
23+
24+
51 52 53 54 56
25+
Sample Output 1
26+
27+
104
28+
*/import java.io.*;
29+
import java.util.*;
30+
31+
public class Solution {
32+
33+
public int sumOddNumbers(int... n)
34+
{
35+
int sum = 0;
36+
for(int i :n)
37+
{
38+
if(i%2!=0)
39+
sum += i;
40+
}
41+
return sum;
42+
}
43+
public static void main(String[] args) {
44+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
45+
Solution sum = new Solution();
46+
Scanner sc = new Scanner(System.in);
47+
String takeinput = sc.nextLine();
48+
if(takeinput.startsWith(" "))
49+
takeinput = takeinput.substring(1);
50+
String []finalstring = takeinput.split(" ");
51+
52+
int []numbers = new int[finalstring.length];
53+
int i=0;
54+
55+
for(String s : finalstring)
56+
{
57+
numbers[i++] = Integer.parseInt(s);
58+
}
59+
System.out.print(sum.sumOddNumbers(numbers));
60+
}
61+
}

0 commit comments

Comments
 (0)