AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
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 333
programming x 150
Posted On : 2016-10-08 16:35:16.0
profile Aarti Kumar - anyforum.in Aarti Kumar
5900
up-rate
4
down-rate

Answers


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 : 2 Yes  0 No
profile sudheerkumar Javvadi - anyforum.in sudheerkumar Javvadi
0920
Reply This Thread
up-rate
4
down-rate

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
profile Amar - anyforum.in Amar
0510
Reply This Thread
up-rate
3
down-rate

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  0 No
profile Divesh - anyforum.in Divesh
8800
Reply This Thread
up-rate
0
down-rate
Comments
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);
}

}
}
profile Divesh - anyforum.in Divesh
88  0  0
Posted On :2017-11-30 16:30:10.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in