Topics Covered:
60-74 – B
50-59 – C
0-49 – D
Print an error message if a negative number is entered or if the number is not with in the range from 0 to 100.
Print 1 to 10 without using loop in java
package com.simplePrograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PrintNumbersWithoutUsingLoop {
public static void recursivefun(int n) {
try {
if (n <= 10) {
System.out.println(n);
recursivefun(n + 1);
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String args[]) throws NumberFormatException, IOException {
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader read = new BufferedReader(istream);
System.out.print("Enter a number ");
int num = 0;
num = Integer.parseInt(read.readLine());
recursivefun(num);
}
}
OUTPUT:
Enter an integer value for num1: 12
Enter an integer value for num2: 13
Before Swapping value of num1 : 12 num2 = 12
After swapping value of num1 : 13 num2= 12
Swapping Two Numbers using temporary or third variable
package com.simplePrograms;
import java.util.Scanner;
public class Swap2NumbersUsing3rdVariable {
public static void swapfucUsing3rdVariable(int num1, int num2) {
int temp=0;
try {
if (num1 != num2) {
System.out.println("Before Swapping value of num1 : " + num1 + " and num2 = " + num1);
temp = num1;
num1 = num2;
num2 = temp;
System.out.println("After swapping value of num1 : " + num1 + " and num2= " + num2);
} else
System.out.println("Entered numbers are found equal");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1, num2;
System.out.print("Enter an integer value for num1: ");
num1 = input.nextInt();
System.out.print("Enter an integer value for num2: ");
num2 = input.nextInt();
swapfucUsing3rdVariable(num1, num2);
}
}
OUTPUT:
Enter an integer value for num1: 12
Enter an integer value for num2: 13
Before Swapping value of num1 : 12 and num2 = 12
After swapping value of num1 : 13 and num2= 12
Program to sum of digits of a number in java
package com.simplePrograms;
import java.util.Scanner;
public class SumOfDigitsOfANumber {
public static void swapfucUsing3rdVariable(int number) {
try {
int sum = 0;
int input = number;
while (input != 0) {
int lastdigit = input % 10;
sum += lastdigit;
input /= 10;
}
System.out.printf("Sum of digits of number %d is %d", number, sum);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number to calculate sum of digits");
int number = sc.nextInt();
swapfucUsing3rdVariable(number);
}
}
OUTPUT:
Please enter a number to calculate sum of digits
123
Sum of digits of number 123 is 6
Program to reverse a number without using inbuilt function
package com.simplePrograms;
import java.util.Scanner;
public class ReverseANumber {
public static void reversefunc(int number) {
try {
int reversedNumber = 0;
int temp = 0;
while (number > 0) {
// use modulus operator to strip off the last digit
temp = number % 10;
// create the reversed number
reversedNumber = reversedNumber * 10 + temp;
number = number / 10;
}
System.out.println("Reversed Number is: " + reversedNumber);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number");
int number = sc.nextInt();
reversefunc(number);
}
}
OUTPUT:
Please enter a number
1234
Reversed Number is: 4321
Check Given number is Armstrong number or not
A number is called as ARMSTRONG number if,sum of cube of every digit present in the number is equal to the number itself then that number is called as armstrong number.
For example the 153 is a armstrong number because 1^3+5^3+3^3=153.
package com.simplePrograms;
import java.util.InputMismatchException;
import java.util.Scanner;
public class AmstrongNumber {
public static void verifyAmstrongNumber(int number) {
int expectedNumber = number;
int sum = 0;
try {
while (number > 0) {
int remainder = number % 10;
sum = (int) (sum + Math.pow(remainder, 3));
number = number / 10;
}
if (expectedNumber == sum)
System.out.println("Number : " + expectedNumber + " is Amstrong");
else
System.out.println(
"Entered number : " + expectedNumber + " is not Amstrong" + " calculated sum is : " + sum);
} catch (Exception e) {
e.printStackTrace();
throw new InputMismatchException("Enter a valid integer number");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = input.nextInt();
verifyAmstrongNumber(number);
}
}
OUTPUT:
Enter a number: 153
Number : 153 is Amstrong
Program to print Fibonacci series
In Fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of Fibonacci series are 0 and 1.
package com.simplePrograms;
import java.util.Scanner;
public class FibonacciSeriesProgram {
public static void getFibonacciSeries(int num) {
try {
int num1 = 0;
int num2 = 1;
System.out.print(num1 + num2);// 0 1
for (int i = 1; i <= num; i++) {
int num3 = num1 + num2;// num3=0+1=1
System.out.print(" " + num3);
num1 = num2;// num1 =0+1
num2 = num3;// num2=1
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number whose fibonacci series you want to find");
int num = sc.nextInt();
getFibonacciSeries(num);
}
}
OUTPUT:
Enter a number whose Fibonacci series you want to find
5
1 1 2 3 5 8
Find Out Whether The Given Number Is Binary Or Not
A binary number is a number which contains only 0 or 1. For example : 101101, 110010110, 10010011 are binary numbers.
package com.simplePrograms;
import java.util.Scanner;
public class BinaryNumberProgram {
static void isBinaryOrNot(int number) {
boolean isBinary = true;
int copyOfNumber = number;
try {
while (copyOfNumber != 0) {
int temp = copyOfNumber % 10; // Gives last digit of the number
if (temp > 1) {
isBinary = false;
break;
} else {
copyOfNumber = copyOfNumber / 10; // Removes last digit from the
// number
}
}
if (isBinary) {
System.out.println(number + " is a binary number");
} else {
System.out.println(number + " is not a binary number");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value : ");;
int num = input.nextInt();
isBinaryOrNot(num);
}
}
OUTPUT:
Enter an integer value : 101110
101110 is a binary number
Example of Break:
The break statement exits a for or while loop completely.
package com.simplePrograms;
import java.util.Scanner;
public class BreakSimpleProgram {
static void breakExample(int num) {
try {
while (num < 100) {
if (num == 10)
break;
System.out.println(num);
num++;
}
System.out.println("Terminating loop if number reaches 10");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value : ");
int num = input.nextInt();
breakExample(num);
}
}
OUTPUT:
Enter an integer value : 1
1
2
3
4
5
6
7
8
9
Terminating loop if number reaches 10
Program to Print Multiplication Table for any Number
package com.simplePrograms;
import java.util.Scanner;
public class MultiplicationTable {
static void multiplyfunc(int num) {
try {
System.out.println("Multiplication table of "+num+" is :-");
for (int i = 1 ; i <= 10 ; i++ )
System.out.println(num+"*"+i+" = "+(num*i));
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer to print it's multiplication table");
int num = input.nextInt();
multiplyfunc(num);
}
}
OUTPUT:
Enter an integer to print it's multiplication table
9
Multiplication table of 9 is :-
9*1 = 9
9*2 = 18
9*3 = 27
9*4 = 36
9*5 = 45
9*6 = 54
9*7 = 63
9*8 = 72
9*9 = 81
9*10 = 90
Program to check number is even or odd by using modulus "%" operator
package com.simplePrograms;
import java.util.Scanner;
public class EvenorOdd {
public static void main(String[] args) {
int number;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to check even or odd");
number = in.nextInt();
try {
if ((number % 2) == 0) {
System.out.println(+number + " is Even number");
} else {
System.out.println(+number + " is Odd Number");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
5
5 is Odd Number
Program to check number is even or odd without using modulus "%" operator
& is bitwise. && is logical.
& evaluates both sides of the operation.
&& evaluates the left side of the operation, if it's true, it continues and evaluates the right side.
package com.simplePrograms;
import java.util.Scanner;
public class EvenorOdd {
public static void main(String[] args) {
int number;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to check even or odd");
number = in.nextInt();
try {
if((number & 1)==0){
System.out.println(+number+" is Even number");
}else{
System.out.println(+number+" is Odd Number");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
OUTPUT:
Enter a number to check even or odd
9
9 is Odd Number
Program to check if number is divisible by a certain number?
package com.simplePrograms;
import java.util.Scanner;
public class DivisibleBy2and5 {
public static void main(String[] args) {
System.out.println("Enter an integer number:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
try {
if (x % 2 == 0) {
System.out.println("The integer number you entered is divisible by 2");
} else {
System.out.println("The integer number you entered is not divisible by 2");
if (x % 5 == 0) {
System.out.println("The integer number you entered is divisible by 5");
} else {
System.out.println("The interger number you entered is not divisible by 5");
}
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
OUTPUT:
Enter an integer number:
25
The integer number you entered is not divisible by 2
The integer number you entered is divisible by 5
Switch statement simple program
package com.simplePrograms;
import java.util.Scanner;
public class SwitchStatement {
public static void main(String args[]) {
System.out.println("Enter Grade of Student:");
Scanner input = new Scanner(System.in);
char grade = input.next().charAt(0);
try {
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
case 'C':
System.out.println("Well done");
break;
case 'D':
System.out.println("You passed");
case 'F':
System.out.println("Better try again");
break;
default:
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Largest of 3 numbers:
package com.simplePrograms;
import java.util.Scanner;
public class LargestOfThreeNumbers {
public static void main(String args[]) {
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
try {
if (x > y && x > z)
System.out.println("Largest number is :" + x);
else if (y > x && y > z)
System.out.println("Largest number is :" + y);
else if (z > x && z > y)
System.out.println("Largest number is :" + z);
else
System.out.println("Entered numbers are not distinct.");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
OUTPUT:
Enter three integers
15
16
17
Largest number is :17
Palindrome Number:
A palindromic number or palindrome number is a number that remains the same when its digits are reversed. Like 16461.
package com.simplePrograms;
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String args[]) {
System.out.print("Enter Number: ");
Scanner read = new Scanner(System.in);
int num = read.nextInt();
int n = num;
// reversing number
int rev = 0, rmd;
try {
while (num > 0) {
rmd = num % 10;
rev = rev * 10 + rmd;
num = num / 10;
}
if (rev == n)
System.out.println(n + " is a Palindrome Number!");
else
System.out.println(n + " is not a Palindrome Number!");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Prime Number
package com.simplePrograms;
import java.util.Scanner;
public class PrimeNumber {
public static void main(String args[]) {
int n, i, res;
boolean flag = true;
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter a No.");
n = scan.nextInt();
try {
for (i = 2; i <= n / 2; i++) {
res = n % i;
if (res == 0) {
flag = false;
break;
}
}
if (flag)
System.out.println(n + " is Prime Number");
else
System.out.println(n + " is not Prime Number");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Output:
Please Enter a No.
3
3 is Prime Number
Program for sum of harmonic series (1+1/2+1/3+....1/n)
package com.simplePrograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HarmonicSeries {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
float sum = 0, val = 0;
System.out.println("Enter n vlaue:");
int n = Integer.parseInt(br.readLine());
try {
for (int i = 1; i <= n; i++) {
val = (float) 1 / i; // type converting the value
sum = sum + val; // summing up the value
}
System.out.println("Sum=" + sum); // displaying the sum
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Write a program to input three marks of a student and determine the average. Print a suitable grade if the average is between;
75-100 – A
60-74 – B
50-59 – C
0-49 – D
Print an error message if a negative number is entered or if the number is not with in the range from 0 to 100.
package com.simplePrograms;
import java.util.Scanner;
public class AverageAndGrade {
public static void main(String[] args) {
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
int average = 0;
try {
if (x > 100 | y > 100 | z > 100 | x < 0 | y < 0 | z < 0) {
System.out.println(
"The entered number is not with in the range from 0 to 100. Please enter a valid number.");
} else {
average = (x + y + z) / 3;
System.out.println("Average : " + average);
}
if (average > 75) {
System.out.println("Grade : A");
} else if (average > 60) {
System.out.println("Grade : B");
} else if (average > 50) {
System.out.println("Grade : c");
} else if (average > 0) {
System.out.println("Grade : D");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Determine if year is Leap Year
If year contains more than 365 days.
That is if Feb month has 29 days then that particular year is a leap year
package com.simplePrograms;
import java.util.Scanner;
public class LeapYearProgram {
public static void funcToVerifyLeapYear(int year) {
try {
if (year % 400 == 0 || year % 4 == 0)
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
System.out.println("\nEnter the year : ");
Scanner scan = new Scanner(System.in);
int year = Integer.parseInt(scan.nextLine());
funcToVerifyLeapYear(year);
}
}
Output:
Enter the year :
1982
1982 is not a leap year
while loop and do-while loop difference program
The logic in while loop is simple it executes the block of statements when the Boolean expression returns true. It gets terminated when the Boolean expression returns false.
do-while loop is similar to while loop, however there is a single difference between these two. Unlike while loop, do-while guarantees at-least one execution of block of statements. This happens because the do-while loop evaluates the boolean expression at the end of the loop’s body. Therefore the set of statements gets executed at-least once before the check of boolean expression.
while Loop
package com.simplePrograms;
public class WhileLoop {
public static void main(String args[]){
int i=4;
while(i>1){
System.out.println(i);
i--;
}
}
}
- Print 1 to 10 without using loop in java
- Factorial program using loop in java
- How to swap two numbers without Temp or Third variable
- Swapping two Numbers using temporary or third variable
- Program to sum of digits of a number in java
- Program to reverse a number without using inbuilt function
- Check given number is Armstrong number or not
- Program to print Fibonacci series
- Find out whether the given number is binary or not
- Example of Break:
- Program to print multiplication table for any number
- Program to check number is even or odd by using modulus "%" operator
- Program to check number is even or odd without using modulus "%" operator
- Program to check if number is divisible by a certain number?
- Switch statement simple program
- Largest of 3 numbers:
- Palindrome number:
- Prime number
- Program for sum of harmonic series (1+1/2+1/3+....1/n)
- Write a program to input three marks of a student and determine the average. Print a suitable grade if the average is between;
60-74 – B
50-59 – C
0-49 – D
Print an error message if a negative number is entered or if the number is not with in the range from 0 to 100.
- Determine If Year Is Leap Year
- while loop and do-while loop difference program
Print 1 to 10 without using loop in java
package com.simplePrograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PrintNumbersWithoutUsingLoop {
public static void recursivefun(int n) {
try {
if (n <= 10) {
System.out.println(n);
recursivefun(n + 1);
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String args[]) throws NumberFormatException, IOException {
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader read = new BufferedReader(istream);
System.out.print("Enter a number ");
int num = 0;
num = Integer.parseInt(read.readLine());
recursivefun(num);
}
}
OUTPUT:
Enter a number 1
1
2
3
4
5
6
7
8
9
10
Factorial Program using loop in java
package com.simplePrograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FactorialProgram {
public static void getFactorialFunc(int num) {
try {
int i, fact = 1;
for (i = 1; i <= num; i++) {
fact = fact * i;
}
System.out.println("Factorial of " + num + " is: " + fact);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader read = new BufferedReader(istream);
System.out.print("Enter a number ");
int num = 0;
num = Integer.parseInt(read.readLine());
getFactorialFunc(num);
}
}
OUTPUT:
Enter a number 5
Factorial of 5 is: 120
How to Swap Two Numbers without Temp or Third variable
package com.simplePrograms;
import java.util.Scanner;
public class Swap2Numbers {
public static void swapfun(int num1, int num2) {
try {
if (num1 != num2) {
System.out.println("Before Swapping value of num1 : " + num1 + " and num2 = " + num1);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After swapping value of num1 : " + num1 + " and num2= " + num2);
} else {
System.out.println("Entered numbers are found equal");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1, num2;
System.out.print("Enter an integer value for num1: ");
num1 = input.nextInt();
System.out.print("Enter an integer value for num2: ");
num2 = input.nextInt();
swapfun(num1, num2);
}
}
import java.util.Scanner;
public class Swap2Numbers {
public static void swapfun(int num1, int num2) {
try {
if (num1 != num2) {
System.out.println("Before Swapping value of num1 : " + num1 + " and num2 = " + num1);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After swapping value of num1 : " + num1 + " and num2= " + num2);
} else {
System.out.println("Entered numbers are found equal");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1, num2;
System.out.print("Enter an integer value for num1: ");
num1 = input.nextInt();
System.out.print("Enter an integer value for num2: ");
num2 = input.nextInt();
swapfun(num1, num2);
}
}
OUTPUT:
Enter an integer value for num1: 12
Enter an integer value for num2: 13
Before Swapping value of num1 : 12 num2 = 12
After swapping value of num1 : 13 num2= 12
Swapping Two Numbers using temporary or third variable
package com.simplePrograms;
import java.util.Scanner;
public class Swap2NumbersUsing3rdVariable {
public static void swapfucUsing3rdVariable(int num1, int num2) {
int temp=0;
try {
if (num1 != num2) {
System.out.println("Before Swapping value of num1 : " + num1 + " and num2 = " + num1);
temp = num1;
num1 = num2;
num2 = temp;
System.out.println("After swapping value of num1 : " + num1 + " and num2= " + num2);
} else
System.out.println("Entered numbers are found equal");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1, num2;
System.out.print("Enter an integer value for num1: ");
num1 = input.nextInt();
System.out.print("Enter an integer value for num2: ");
num2 = input.nextInt();
swapfucUsing3rdVariable(num1, num2);
}
}
OUTPUT:
Enter an integer value for num1: 12
Enter an integer value for num2: 13
Before Swapping value of num1 : 12 and num2 = 12
After swapping value of num1 : 13 and num2= 12
Program to sum of digits of a number in java
package com.simplePrograms;
import java.util.Scanner;
public class SumOfDigitsOfANumber {
public static void swapfucUsing3rdVariable(int number) {
try {
int sum = 0;
int input = number;
while (input != 0) {
int lastdigit = input % 10;
sum += lastdigit;
input /= 10;
}
System.out.printf("Sum of digits of number %d is %d", number, sum);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number to calculate sum of digits");
int number = sc.nextInt();
swapfucUsing3rdVariable(number);
}
}
Please enter a number to calculate sum of digits
123
Sum of digits of number 123 is 6
Program to reverse a number without using inbuilt function
package com.simplePrograms;
import java.util.Scanner;
public class ReverseANumber {
public static void reversefunc(int number) {
try {
int reversedNumber = 0;
int temp = 0;
while (number > 0) {
// use modulus operator to strip off the last digit
temp = number % 10;
// create the reversed number
reversedNumber = reversedNumber * 10 + temp;
number = number / 10;
}
System.out.println("Reversed Number is: " + reversedNumber);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number");
int number = sc.nextInt();
reversefunc(number);
}
}
Please enter a number
1234
Reversed Number is: 4321
Check Given number is Armstrong number or not
A number is called as ARMSTRONG number if,sum of cube of every digit present in the number is equal to the number itself then that number is called as armstrong number.
For example the 153 is a armstrong number because 1^3+5^3+3^3=153.
package com.simplePrograms;
import java.util.InputMismatchException;
import java.util.Scanner;
public class AmstrongNumber {
public static void verifyAmstrongNumber(int number) {
int expectedNumber = number;
int sum = 0;
try {
while (number > 0) {
int remainder = number % 10;
sum = (int) (sum + Math.pow(remainder, 3));
number = number / 10;
}
if (expectedNumber == sum)
System.out.println("Number : " + expectedNumber + " is Amstrong");
else
System.out.println(
"Entered number : " + expectedNumber + " is not Amstrong" + " calculated sum is : " + sum);
} catch (Exception e) {
e.printStackTrace();
throw new InputMismatchException("Enter a valid integer number");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = input.nextInt();
verifyAmstrongNumber(number);
}
}
OUTPUT:
Enter a number: 153
Number : 153 is Amstrong
Program to print Fibonacci series
In Fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of Fibonacci series are 0 and 1.
package com.simplePrograms;
import java.util.Scanner;
public class FibonacciSeriesProgram {
public static void getFibonacciSeries(int num) {
try {
int num1 = 0;
int num2 = 1;
System.out.print(num1 + num2);// 0 1
for (int i = 1; i <= num; i++) {
int num3 = num1 + num2;// num3=0+1=1
System.out.print(" " + num3);
num1 = num2;// num1 =0+1
num2 = num3;// num2=1
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number whose fibonacci series you want to find");
int num = sc.nextInt();
getFibonacciSeries(num);
}
}
Enter a number whose Fibonacci series you want to find
5
1 1 2 3 5 8
Find Out Whether The Given Number Is Binary Or Not
A binary number is a number which contains only 0 or 1. For example : 101101, 110010110, 10010011 are binary numbers.
package com.simplePrograms;
import java.util.Scanner;
public class BinaryNumberProgram {
static void isBinaryOrNot(int number) {
boolean isBinary = true;
int copyOfNumber = number;
try {
while (copyOfNumber != 0) {
int temp = copyOfNumber % 10; // Gives last digit of the number
if (temp > 1) {
isBinary = false;
break;
} else {
copyOfNumber = copyOfNumber / 10; // Removes last digit from the
// number
}
}
if (isBinary) {
System.out.println(number + " is a binary number");
} else {
System.out.println(number + " is not a binary number");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value : ");;
int num = input.nextInt();
isBinaryOrNot(num);
}
}
OUTPUT:
Enter an integer value : 101110
101110 is a binary number
Example of Break:
The break statement exits a for or while loop completely.
package com.simplePrograms;
import java.util.Scanner;
public class BreakSimpleProgram {
static void breakExample(int num) {
try {
while (num < 100) {
if (num == 10)
break;
System.out.println(num);
num++;
}
System.out.println("Terminating loop if number reaches 10");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value : ");
int num = input.nextInt();
breakExample(num);
}
}
OUTPUT:
Enter an integer value : 1
1
2
3
4
5
6
7
8
9
Terminating loop if number reaches 10
Program to Print Multiplication Table for any Number
package com.simplePrograms;
import java.util.Scanner;
public class MultiplicationTable {
static void multiplyfunc(int num) {
try {
System.out.println("Multiplication table of "+num+" is :-");
for (int i = 1 ; i <= 10 ; i++ )
System.out.println(num+"*"+i+" = "+(num*i));
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer to print it's multiplication table");
int num = input.nextInt();
multiplyfunc(num);
}
}
Enter an integer to print it's multiplication table
9
Multiplication table of 9 is :-
9*1 = 9
9*2 = 18
9*3 = 27
9*4 = 36
9*5 = 45
9*6 = 54
9*7 = 63
9*8 = 72
9*9 = 81
9*10 = 90
Program to check number is even or odd by using modulus "%" operator
package com.simplePrograms;
import java.util.Scanner;
public class EvenorOdd {
public static void main(String[] args) {
int number;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to check even or odd");
number = in.nextInt();
try {
if ((number % 2) == 0) {
System.out.println(+number + " is Even number");
} else {
System.out.println(+number + " is Odd Number");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
OUTPUT:
Enter a number to check even or odd5
5 is Odd Number
Program to check number is even or odd without using modulus "%" operator
& is bitwise. && is logical.
& evaluates both sides of the operation.
&& evaluates the left side of the operation, if it's true, it continues and evaluates the right side.
package com.simplePrograms;
import java.util.Scanner;
public class EvenorOdd {
public static void main(String[] args) {
int number;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to check even or odd");
number = in.nextInt();
try {
if((number & 1)==0){
System.out.println(+number+" is Even number");
}else{
System.out.println(+number+" is Odd Number");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
OUTPUT:
Enter a number to check even or odd
9
9 is Odd Number
Program to check if number is divisible by a certain number?
package com.simplePrograms;
import java.util.Scanner;
public class DivisibleBy2and5 {
public static void main(String[] args) {
System.out.println("Enter an integer number:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
try {
if (x % 2 == 0) {
System.out.println("The integer number you entered is divisible by 2");
} else {
System.out.println("The integer number you entered is not divisible by 2");
if (x % 5 == 0) {
System.out.println("The integer number you entered is divisible by 5");
} else {
System.out.println("The interger number you entered is not divisible by 5");
}
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Enter an integer number:
25
The integer number you entered is not divisible by 2
The integer number you entered is divisible by 5
package com.simplePrograms;
import java.util.Scanner;
public class SwitchStatement {
public static void main(String args[]) {
System.out.println("Enter Grade of Student:");
Scanner input = new Scanner(System.in);
char grade = input.next().charAt(0);
try {
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
case 'C':
System.out.println("Well done");
break;
case 'D':
System.out.println("You passed");
case 'F':
System.out.println("Better try again");
break;
default:
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
OUTPUT:
Enter Grade of Student:
A
Excellent!
Your grade is A
package com.simplePrograms;
import java.util.Scanner;
public class LargestOfThreeNumbers {
public static void main(String args[]) {
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
try {
if (x > y && x > z)
System.out.println("Largest number is :" + x);
else if (y > x && y > z)
System.out.println("Largest number is :" + y);
else if (z > x && z > y)
System.out.println("Largest number is :" + z);
else
System.out.println("Entered numbers are not distinct.");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Enter three integers
15
16
17
Largest number is :17
Palindrome Number:
A palindromic number or palindrome number is a number that remains the same when its digits are reversed. Like 16461.
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String args[]) {
System.out.print("Enter Number: ");
Scanner read = new Scanner(System.in);
int num = read.nextInt();
int n = num;
// reversing number
int rev = 0, rmd;
try {
while (num > 0) {
rmd = num % 10;
rev = rev * 10 + rmd;
num = num / 10;
}
if (rev == n)
System.out.println(n + " is a Palindrome Number!");
else
System.out.println(n + " is not a Palindrome Number!");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
OUTPUT:
Enter Number: 12321
12321 is a Palindrome Number!
Prime Number
Logic: Prime Number are divisible by itself only.
Not divisible by any Number | Divisible by 2 ...no need to check further | Divisible by 3 ...no need to check further |
7%2=1 7%3=1 7%4=3 7%5=2 7%6=1 | 8%2=0 8%3= 8%4= 8%5= 8%6= 8%7= | 9%2=1 9%3=0 9%4 9%5 9%6 9%7 9%8 |
Numbers are not divisible by more than half of the number | ||
No need to check up to 6 check up to 3 only | No need to check upto 7 check up to 4 only | No need to check upto 8 check up to 4 only |
package com.simplePrograms;
import java.util.Scanner;
public class PrimeNumber {
public static void main(String args[]) {
int n, i, res;
boolean flag = true;
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter a No.");
n = scan.nextInt();
try {
for (i = 2; i <= n / 2; i++) {
res = n % i;
if (res == 0) {
flag = false;
break;
}
}
if (flag)
System.out.println(n + " is Prime Number");
else
System.out.println(n + " is not Prime Number");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Output:
Please Enter a No.
3
3 is Prime Number
Program for sum of harmonic series (1+1/2+1/3+....1/n)
package com.simplePrograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HarmonicSeries {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
float sum = 0, val = 0;
System.out.println("Enter n vlaue:");
int n = Integer.parseInt(br.readLine());
try {
for (int i = 1; i <= n; i++) {
val = (float) 1 / i; // type converting the value
sum = sum + val; // summing up the value
}
System.out.println("Sum=" + sum); // displaying the sum
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Output:
Enter n vlaue:
5
Sum=2.2833335
Write a program to input three marks of a student and determine the average. Print a suitable grade if the average is between;
75-100 – A
60-74 – B
50-59 – C
0-49 – D
Print an error message if a negative number is entered or if the number is not with in the range from 0 to 100.
package com.simplePrograms;
import java.util.Scanner;
public class AverageAndGrade {
public static void main(String[] args) {
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
int average = 0;
try {
if (x > 100 | y > 100 | z > 100 | x < 0 | y < 0 | z < 0) {
System.out.println(
"The entered number is not with in the range from 0 to 100. Please enter a valid number.");
} else {
average = (x + y + z) / 3;
System.out.println("Average : " + average);
}
if (average > 75) {
System.out.println("Grade : A");
} else if (average > 60) {
System.out.println("Grade : B");
} else if (average > 50) {
System.out.println("Grade : c");
} else if (average > 0) {
System.out.println("Grade : D");
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
Output:
Enter three integers
70
90
90
Average : 83
Grade : A
If year contains more than 365 days.
That is if Feb month has 29 days then that particular year is a leap year
package com.simplePrograms;
import java.util.Scanner;
public class LeapYearProgram {
public static void funcToVerifyLeapYear(int year) {
try {
if (year % 400 == 0 || year % 4 == 0)
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
public static void main(String[] args) {
System.out.println("\nEnter the year : ");
Scanner scan = new Scanner(System.in);
int year = Integer.parseInt(scan.nextLine());
funcToVerifyLeapYear(year);
}
}
Enter the year :
1982
1982 is not a leap year
while loop and do-while loop difference program
The logic in while loop is simple it executes the block of statements when the Boolean expression returns true. It gets terminated when the Boolean expression returns false.
do-while loop is similar to while loop, however there is a single difference between these two. Unlike while loop, do-while guarantees at-least one execution of block of statements. This happens because the do-while loop evaluates the boolean expression at the end of the loop’s body. Therefore the set of statements gets executed at-least once before the check of boolean expression.
while Loop
package com.simplePrograms;
public class WhileLoop {
public static void main(String args[]){
int i=4;
while(i>1){
System.out.println(i);
i--;
}
}
}
Output:
4
3
2
do-while loop
package com.simplePrograms;
public class DoWhileLoop {
public static void main(String args[]) {
int i = 3;
do {
System.out.println(i);
i--;
} while (i > 4);
}
}
Comments
Post a Comment