Java program to swap two numbers
Below is the Java program to swap two numbers using temporary or third variable.
1. Take the input from the user(x, y)
2.Copy the value of x to third variable (z)
z = x;
3.Copy the value of y to x
x = y;
4.Copy the value of z (where x’s value is copied) to y
y = z;
Program Continue reading
Linear search program in java
Linear search is used to find whether a given element is available in an array or not. It is also called as sequential search.
Compare the given element with each element of the array until the element is found or list ends.
Example Continue reading
Perfect Number Program In Java
A perfect number is a positive integer that is equal to the sum of its proper divisors.
Example – 6,28,496 ,8128 etc
The factors of 6 are 1, 2, 3 and 6.
1 + 2 + 3 = 6
The factors of 28 are 1, 2, 4, 7, 14 and 28.
1 + 2 + 4 + 7 + 14 = 28
Algorithm Continue reading
Multiplication table program in java
In mathematics, a multiplication table is a mathematical table used to define a multiplication operation for an algebraic system.
1. Take the number from the user (number)
2. Iterate from 1 to 10 using for loop (i)
3. Multiply the number with i in the for loop (i*10)
4. Print the result Continue reading
Leap year program in java
A normal year has 365 days.
A leap year has 366 days (the extra day is the 29th of February)
example : 2012, 2016, 2020, 2024 …
1. Take the year from the user (year)
2. If the year is divisible by 400 it is a leap year
3. If the year is divisible by 4 and not divisible by 100 it is a leap year
4. If step 2 or step 3 conditions are not satisfied,It is a not leap year Continue reading
Pascal triangle program in java
Pascal’s triangle is a triangular array of the binomial coefficients.It is named after Blaise Pascal, a famous French Mathematician and Philosopher)
As show in figure,the triangle will start with 1 at the top and placing the numbers below it in the traiangular format.Except the edges(1’s), each number is the sum of two numbers above it.
1.Take ‘number of rows’ from the user(n)
2.Iterate from 0 to n(i)
3.Add spaces for every iteration from n-1 spaces to 0 spaces(j)
4.Iterate from 0 to i(k)
5. Print the values using (number * (i – k) / (k + 1)) Continue reading
Automorphic number java program
In mathematics an automorphic number is a number whose square ends in the same digits as the number itself.
52 = 25, 62 = 36 ,252=625 , 762 = 5776
The sequence of automorphic numbers begins 1, 5, 6, 25, 76, 376, 625, 9376…
- Take the input from the user (number)
- Calculate number of digits in the number(digits)
- Calculate square of the number(numberSquare)
- Calculate the reminder of number divided by the 10 to power of digits(lastDigits)
- If number is equal to last digits of the number,It is automorphic
- Else it is not automorphic
Palindrome number program in java
A palindromic number is a number that remains the same when its digits are reversed.Like 13231 for example is palindrome number whereas 2344 is not.
Step 1: Take the number from user and store entered number in number and tempNumber
Step 2: Repeat steps 3-5 until number != 0
Step 3: Divide number by 10 and determine remainder
reminder = number % 10;
Relational Operators are used for the purpose of comparing.They compare two quantities to determine if they are equal or greater than other.There are six types of relational operators
- = = equal to operator
- != not equal to operator
- > greater than operator
- < less than operator
- >= great than or equal operator
- <= less than or equal to operator
Fibonacci series program in java
Fibonacci series or Fibonacci sequence are the numbers in the following sequence
The first two numbers in the Fibonacci series are 0 and 1.The numbers followed by 0,1 are the sum of previous two numbers.
Fibonacci series program in java