0% found this document useful (0 votes)
151 views4 pages

Write A Program To Check If A Number Is Even or Odd

The documents contain programs to check if a number is even or odd, prime, palindrome, or Armstrong number. They also contain programs to bubble sort an integer array, print the Fibonacci series up to a given number using iterative and recursive methods. The programs take user input, perform various mathematical calculations and condition checks, and output the results.

Uploaded by

Sonali Sidana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
151 views4 pages

Write A Program To Check If A Number Is Even or Odd

The documents contain programs to check if a number is even or odd, prime, palindrome, or Armstrong number. They also contain programs to bubble sort an integer array, print the Fibonacci series up to a given number using iterative and recursive methods. The programs take user input, perform various mathematical calculations and condition checks, and output the results.

Uploaded by

Sonali Sidana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

Write a program to check if a number is even or odd

import java.util.Scanner;

class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();

if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
}
}

Write a program to find if a number is prime

import java.util.*;

class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;

Scanner in = new Scanner(System.in);


System.out.println("Enter the number of prime numbers you want");
n = in.nextInt();

if (n >= 1)
{
System.out.println("First "+n+" prime numbers are :-");
System.out.println(2);
}

for ( int count = 2 ; count <=n ; )


{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}

Write a program to check if a number is palindrome or not

import java.util.*;

class Palindrome
{
public static void main(String args[])
{
String original, reverse="";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome");


original = in.nextLine();

int length = original.length();

for ( int i = length - 1 ; i >= 0 ; i-- )


reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}

Write a program to bubble sort an integer array


import java.util.Scanner;

class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}

Write a program to find if a number is Armstrong or not


import java.util.*;

class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, r;

Scanner in = new Scanner(System.in);


System.out.println("Enter a number to check if it is an armstrong number");
n = in.nextInt();

temp = n;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}

if ( n == sum )
System.out.println("Entered number is an armstrong number.");
else
System.out.println("Entered number is not an armstrong number.");
}
}

Write a program to print Fibonacci series up to a given number using both


iterative and recursive methods

class Fibonacci
{
public static void main(String args[])
{
int a=0,b=1, c=0;

int i= Integer.parseInt(args[0]);
System.out.println("*******Fibonacci Series*******");
System.out.println(0);
while(i>1)
{
c = a + b;
a = b;
b = c;
System.out.print(" "+c);
i--;
}
}
}

You might also like