AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
3. Our Experts are looking for yours ?.

corejava-programming: Sum of N ODD and EVEN numbers

 Pranav is a genious student in electronics but his Java is not so good. One day he wanted to know the sum of all the ODD and EVEN numbers between the range of 1 to ´N´. That is why he is asking your help. Write a java program to find the sum of ODD and EVEN numbers. If ´N´ is invalid then print ´Invalid input´. Sample input:- 3 Sample Output:- Sum of EVEN Numbers:- 2 Sum of ODD Numbers:- 4 corejava x 353programming x 169 Posted On : 2016-10-08 16:35:16.0 Aarti Kumar 5900 4

 import java.util.Scanner; public class EvenOddSum { public static void main(String args[]) { Scanner s=new Scanner(System.in); int N=s.nextInt(); int Evencount=0,Oddcount=0; if(N==0||N<0) { System.out.println("Invalid Input!!::"); } else { for(int i=1;i<=N;i++) { if(i%2==0) { Evencount=Evencount+i; } else { Oddcount=Oddcount+i; } } System.out.print("Sum Of Even Numbers :-: "+Evencount); System.out.print(" Sum of Odd Numbers :-: "+Oddcount); } } } Posted On : 2016-10-09 12:42:05 Satisfied : 4 Yes  1 No 4

 import java.util.Scanner; public class SumOfOddAndEvenNumbers { private static int inputNumber = 0; private static Scanner scanner; private static long sumOfOddNumbers = 0L; private static long sumOfEvenNumbers = 0L; public static void main(String[] args) { scanner = new Scanner(System.in); System.out.println("Enter a number : "); inputNumber = scanner.nextInt(); findSumOfOddAndEvenNumbers(inputNumber); } private static void findSumOfOddAndEvenNumbers(int number) { if(number <= 0) System.out.println("Invalid input."); else { for(int i = 1; i <= number; i++) { if(i%2 == 0) sumOfEvenNumbers += i; else sumOfOddNumbers += i; } System.out.println("Sum of odd numbers from 1 to "+ number + " is : "+sumOfOddNumbers); System.out.println("Sum of even numbers from 1 to "+ number + " is : "+sumOfEvenNumbers); } } } Posted On : 2017-10-28 17:11:34 Satisfied : 1 Yes  0 No 3

 import java.util.Scanner; class EvenODD { public static void main(String [] args){ Scanner sc=new Scanner(System.in); int N=sc.nextInt(); long esum=0,osum=0; if(N<=0) { System.out.print("Invalid Input"); }else{ for(int i=1;i<=N;i++){ if(i%2==0){ esum+=i; }else{ osum+=i; } } System.out.println("Sum of EVEN Numbers: "+esum); System.out.println("Sum of ODD Numbers: "+osum); } } } Posted On : 2017-11-27 17:11:01 Satisfied : 1 Yes  1 No Divesh 101180Reply This Thread 0
corrected Ans:
import java.util.Scanner;

class EvenODD
{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
long esum=0,osum=0;
if(N<=0)
{
System.out.println("Invalid input");
}else{
for(int i=0;i<=N;i=i+2){
esum+=i;
}
for(int i=1;i<=N;i=i+2){
osum+=i;
}
System.out.println("Sum of EVEN Numbers:- "+esum);
System.out.println("Sum of ODD Numbers:- "+osum);
}

}
}
Divesh
101  18  0
Posted On :2017-11-30 16:30:10.0