In this tutorial, we will discuss The Java program to calculate sum of odd and even numbers
In this article, we are going to learn the how to calculate the sum of odd and even numbers in the Java program
When any integer value which ends in 0,2,4,6,8 is divided by two it is called as an even number
Example for even numbers – 34,-64,78,788
When any integer value which ends in 0,1,3,5,7,9 is not divided by two it is called as an odd number
Example for odd numbers – 33,-69,75,785
We can use a modular operator to find odd or even number in the given range.
if n%2==0, n is an even number
if n%2==1, n is an odd number
Program 1
This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from 1 to entered digits using a for loop.
import java.util.Scanner; class OddEvenSum{ public static void main (String args[]){ int i,num; //declare variable i,num int oddSum=0,evenSum=0; //declare and initialize variables oddSum,evenSum Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for num: \n"); num=scan.nextInt();//reads num1 from user for(i=1; i<=num; i++){ if(i%2==0) evenSum=evenSum+i; else oddSum=oddSum+i; } System.out.println("Sum of all odd numbers are: "+oddSum); System.out.println("Sum of all even numbers are: "+evenSum); } }
When the above code is executed, it produces the following results
Enter the number for num: 10 Sum of all odd numbers are: 25 Sum of all even numbers are: 30
Program 2
This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from 1 to entered digits using a while loop.
import java.util.Scanner; class OddEvenSumWhile{ public static void main (String args[]){ int i,num; //declare variable i,num int oddSum=0,evenSum=0; //declare and initialize variables oddSum,evenSum Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for num: \n"); num=scan.nextInt();//reads num1 from user i=1; while(i<=num){ if(i%2==0) evenSum=evenSum+i; else oddSum=oddSum+i; i++ } System.out.println("Sum of all odd numbers are: "+oddSum); System.out.println("Sum of all even numbers are: "+evenSum); } }
When the above code is executed, it produces the following results
Enter the number for num: 100 Sum of all odd numbers are: 2500 Sum of all even numbers are: 2550
Program 3
This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from 1 to entered digits using a do-while loop.
import java.util.Scanner; class OddEvenSumDoWhile{ public static void main (String args[]){ int i,num; //declare variable i,num int oddSum=0,evenSum=0; //declare and initialize variables oddSum,evenSum Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for num: \n"); num=scan.nextInt();//reads num1 from user i=1; do{ if(i%2==0) evenSum=evenSum+i; else oddSum=oddSum+i; i++; }while(i<=num); System.out.println("Sum of all odd numbers are: "+oddSum); System.out.println("Sum of all even numbers are: "+evenSum); } }
When the above code is executed, it produces the following results
Enter the number for num: 50 Sum of all odd numbers are: 625 Sum of all even numbers are: 650
Suggested for you
Similar post
C program to find a number is even or odd using the function
C program to separate Odd and Even numbers from an array
C program to Electricity bill calculation using the function
C program to display all even and odd numbers from 1 to n
C program display odd and even numbers without if statements
C program to calculate the sum of odd and even numbers
C program to find whether a number is even or odd
C++ program to find a number is even or odd using the function
C++ program to separate Odd and Even numbers from an array
C++ program to display all even and odd numbers from 1 to n
C++ program calculate Average of odd and even numbers in an array
C++ program to calculate the sum of odd and even numbers
C++ program to find whether a number is even or odd
Java program to find a number is even or odd using the method
Java program to separate Odd and Even numbers from an array
Java program to display all even and odd numbers from 1 to n
Java program display odd and even numbers without if statements
Java program to calculate the sum of odd and even numbers
Java program to find whether a number is even or odd
Python program check whether a number is odd or even
Python program to check a number is even or odd using the function
Python program to display even and odd numbers without if
Python program to display even and odd number in the given range
Separate odd and even numbers in a list to different two list
Python Program to find odd and even numbers from a list
Java program to calculate sum of odd and even numbers
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…
Python: Average of Odd & Even Numbers from User Input In this post, we will…
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…