Odd even program in c | c++ and java

If you are searching for odd even program in c , c++ and java then you are at the right article.

Even number is a number which is completely divisible by 2. Odd number is a number which is not completely divisible by 2. If you divide an odd number then the remainder will always 1.

Example:

Odd numbers : 1,3,5,7,9,11,13,15,17,19
Even numbers: 0,2,4,6,8,10,12,14,16,18,20


Pre-requisite Programming concepts

  • Input output operations in C, C++ and Java.
  • % opeartor ( it performs division operation and assigns the remainder )
  • Data-type in c, c++ and java.

Logic for checking a number is odd or even

  • Input the number.
  • Divide the number by 2 if remainder is equal to 0 then it is even number otherwise
    that number is odd.

Odd even program in c

#include<stdio.h>
int main(int argc, char const *argv[])
{
	//scan a number to check even or odd
	int number;
	printf("\nOdd or even program in c\n");
	scanf("%d",&number);
	/*If the number is competely divisible by 2.
	then that number is even number.
	otherwise that number is odd number.
	*/
	if( number % 2 == 0 )
	{
		printf("%d is even number\n",number);
	}
	else
	{
		printf("%d is odd number\n",number);
	}
	return 0;
}
Odd even program in c
Odd even program in c

Odd even program in c++

#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
	//scan a number to check even or odd
	int number;
	cout<<"Odd or even program in c++"<<endl;
	cin>>number;
	/*If the number is competely divisible by 2.
	then that number is even number.
	otherwise that number is odd number.
	*/
	if( number % 2 == 0 )
	{
		cout<<number<< " is even number\n";
	}
	else
	{
		cout<<number<< " is odd number\n";
	}
	return 0;
}
Odd even program in c++
Odd even program in c++

Odd even program in Java

Odd even prograimport java.util.Scanner;
public class odd_or_even {
	public static void main(String[] args) {
		/*scan the number*/
		int number;
		System.out.println( "Odd or even number in java" );
		System.out.println( "Enter a number" );
		Scanner stdin=new Scanner(System.in);
		number=stdin.nextInt();
		/*If the number is completely divisible by two then
		 * that number is even number.
		 * otherwise that number is odd number.
		 */
		if( number % 2 == 0 )
		{
			System.out.println( number+" is even number." );
		}
		else
		{
			System.out.println( number+" is odd number." );
		}
	}
}m in c 
Odd even program in java
Odd even program in java

Suggested further practice