Java loops | Set 1 | Practice | GeeksforGeeks

For a given N, prints the sum of even and odd integers of the first N natural numbers.
Example 1:
Input:
N = 1
Output:
0 1
Explanation:
Natural numbers less than 1 are only 1.
So the sum of even number = 0.
and the sum of odd number = 1.
Example 2:
Input:
N = 6
Output:
12 9  
Explanation:
Natural numbers less than 6 are 
1 2 3 4 5 6
So the sum of even number = 2 + 4 + 6 = 12.
and the sum of odd number = 1 + 3 + 5 = 9.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function getSum() which takes an integer N as input parameter and return the sum of even and odd natural number less than equal to N.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints: 
1<= N <=104
-1
payalchaudhary4423 months ago
 static ArrayList<Integer> getSum(int N){       // code here          int evenSum=0;      int oddSum=0;      for(int i=1; i<=N;i++)      {          if(i%2==0)          {             evenSum=evenSum+i;                                      }          else          {              oddSum= oddSum+i;          }      }          getSum.add(evenSum);         getSum.add(oddSum);          return getSum;             }     }
0
sonamnkumari3 months ago
class Solution{   static ArrayList<Integer> getSum(int N){       // code here       int evenSum=0;       int oddSum=0;       for(int i=2;i<=N;i+=2){           evenSum=evenSum+i;       }              for(int i=1;i<=N;i+=2){           oddSum=oddSum+i;       }              ArrayList<Integer> numbers=new ArrayList<>();              numbers.add(evenSum);       numbers.add(oddSum);       return numbers;   }}
0
gauravverma9918187053 months ago
import Java.util.Scanner;
class OddEven
{
public static void main(string[] args){
int n,sum=0;
System.out.print("Enter the Number:");
Scanner r=new Scanner(System.in);
n=r.nextInt();
if(n%2==0)
{
for(int i=0;i<=n;i=i+2)
{
sum=sum+i;
}
⁠System.out.print("Sum of Even number:"+ sum);
}
else
{
for(int i=1;i<=n;i=i+2)
{
sum=sum+i;
}
System.out.print("Sum of Odd number :"+sum);
}
}
}
0
98pcjgbgt5a9ltq4d2c99y6scapx4ytypoz5t3pw4 months ago
public class Main{
   public static void main(String args[])   {       int sum=0,i,sumodd=0;       for(i=0;i<=5;i++)       {           if(i%2==0)           {            sum=sum+i;           }           else           {                 sumodd=sumodd+i;           }       }            System.out.println(sum );                       System.out.println(sumodd);           }       }   
0
badgujarsachin835 months ago
 static ArrayList<Integer> getSum(int N){
        // code here
        ArrayList<Integer> ans=new ArrayList<Integer>(2);
        int even=0;
        int odd=0;
        for(int i=1;i<=N;i++){
            if(i%2==0){
                even+=i;
            }else{
                odd+=i;
            }
        }
        ans.add(even);
        ans.add(odd);
        return ans;
    }
-1
pankajkumarravi6 months ago
********************** Java Logic ****************
static ArrayList<Integer> getSum(int N){    // code here    ArrayList<Integer> resp=new ArrayList<Integer>(2);        int i=1;        int evenSum=0;        int oddSum=0;        while (i<=N){                        if (i%2 ==0)                            evenSum =evenSum+i;                        else                            oddSum = oddSum+i;            i++;        }        resp.add(evenSum);        resp.add(oddSum);        return resp;}
-1
notaverageuser6 months ago
O(1) time compexity solution.
class Solution{
    static ArrayList<Integer> getSum(int N) {
        ArrayList<Integer> list = new ArrayList<>();

        int sumEven = 0, sumOdd = 0;
        int n = N / 2;
        sumEven = (int) (n / 2.0 * (2 * 2 + (n - 1)*2));

        n = (N + 1) / 2;

        sumOdd = (int) (n / 2.0 * (2 * 1 + (n - 1)*2));
        
        list.add(sumEven);
        list.add(sumOdd);
        return list;
    }
}
-1
rawadaridi6 months ago
ArrayList<Integer> sum = new ArrayList<Integer>();       int even = 0;       int odd = 0;              for(int i = 1; i <= N; i++){           if(N <= 10000 && N >= 1){               if(i % 2 == 0){                   even += i;               }               else{                   odd += i;               }           }           else{               System.out.println("Enter a number between 1 and 10000.");           }       }       sum.add(even);       sum.add(odd);              return sum;
0
vikash katiyar7 months ago
vikash katiyar
ArrayList<integer> list = new ArrayList<integer>();        int count =0,sum=0;        for(int i=1;i<=N;i++)        {
            if(i%2==0)            {                count=count+i;             }             else            {                sum=sum+i;            }        }        list.add(count);        list.add(sum);return list;
-1
ashay9 months ago
ashay
ArrayList<integer> list = new ArrayList<integer>();        int even=0,odd=0;        for(int i=1; i<=N; i++)
            if(i%2==0){              even=even+i;
            }            for(int i=1; i<=N; i++)
            if(i%2!=0){              odd=odd+i;
            }
        list.add(even);        list.add(odd);        return list;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.