ITEngineer8995
November 15, 2020
Java Program To Find The Factorial Of A Number Using Recursion
In this Java Program, we calculate the factorial of the number using recursion.
Factorial of n is the product of all the positive descending integers. It is denoted by n!
For example : 4! = 4 * 3 * 2 * 1 = 24
Logic : fact = fact * i;
Step 1 : Create main method
Step 2 : Create variable factorial and store factorial of 4 in it.
Step 3 : Print the number
Step 4 : Create a static function which returns 1
Step 5 : Create recursion function which calls itself
Program :
public class FactorialDemo2 {
public static void main(String[] args){
int factorial = fact(4);
System.out.println("Factorial of 4 is:"+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion : Function calling itself
output = fact(n-1)*n;
return output;
}
}
Output :
Factorial of 4 is : 24
For execution of the same do visit my YouTube Channel :
https://youtu.be/xGJ2VBxZuRc
ITEngineer8995
November 09, 2020
Java Program To Find The Factorial Of A Number Using Recursion Entered By The User
In this Java Program, we calculate the factorial of the number using recursion entered by the user.
Factorial of n is the product of all the positive descending integers. It is denoted by n!
For example : 4! = 4 * 3 * 2 * 1 = 24
Logic : fact = fact * i;
Step 1 : Import Scanner Class
Step 2 : Create main method
Step 3 : Create scanner object for capturing the user input
Step 4 : Ask the user to enter the number
Step 5 : Store the entered value in the variable num
Step 6 : Call the user defined function fact
Step 7 : Print the number
Step 8 : Create a static function which returns 1
Step 9 : Create recursion function which calls itself
Program :
import java.util.Scanner;
public class FactorialDemo {
public static void main(String[] args)
{
//Scanner object for capturing the user input
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
//Store the entered value in variable
int num =sc.nextInt();
//Call the user defined function fact
int factorial = fact(num);
System.out.println("Factorial of entered number is:"+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion: Function calling itself
output = fact(n-1)*n;
return output;
}
}
Output :
Enter the number:
5
Factorial of entered number is:120
For execution of the same do visit my YouTube Channel :
ITEngineer8995
October 25, 2020
Java Program To Reverse a String Entered by the User
In this Java Program, we reverse a string entered by the user
Logic : String reversed = reverseString(str);
Step 1 : Import Scanner Class
Step 2 : Create main method
Step 3 : Create variable str
Step 4 : Ask user to enter the username
Step 5 : Create the object of the Scanner Class
Step 6 : Store the user entered username in the variable str
Step 7 : Close the scanner class to avoid memory lea
Step 8 : Apply the logic
Step 9 : Print the reversed string
Step 10 : Create reversedString method
Step 11 : Apply if condition which says if str is empty then reverse string
Step 12 : Call the function recursively
Program :
import java.util.Scanner;
public class JavaExample2 {
public static void main(String[] args)
{
String str;
System.out.println("Enter your username:");
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
sc.close();
String reversed = reverseString(str);
System.out.println("The reversed string is:"+reversed);
}
public static String reverseString(String str)
{
if(str.isEmpty())
return str;
//calling function recursively
return reverseString(str.substring(1))+str.charAt(0);
}
}
Output :
Enter your username:
how are you doing?
The reversed string is:?gniod uoy era woh
For execution of the same do visit my Youtube Channel :
ITEngineer8995
October 14, 2020
ZARA ZARA
CHORDS : A Major , D major, F Major
STRUMMING PATTERN :
Visit My Youtube Channel For the Cover :
ITEngineer8995
October 04, 2020
Java Program To Reverse a String using Recursion
In this Java Program, we reverse a string using recursion.
The process in which the function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function
Logic :
String reversed = reverseString(str);
Step 1 : Create obj str of String class and store the output string in it.
Step 2 : Create another object as reversed and apply the logic of the reverse string in it.
Step 3 : Output the reversed string using the System.out.println method.
Step 4 : Create reverseString method
Step 5 : Apply if statement, if str.isEmpty then return str.
Step 6 : Call function recursively.
Program :
public class JavaExample {
public static void main(String []args)
{
String str = "Welcome To my Channel";
String reversed = reverseString(str);
System.out.println("The reversed string is : "+reversed);
}
public static String reverseString(String str)
{
if(str.isEmpty())
return str;
//Calling function recursively
return reverseString(str.substring(1)) + str.charAt(0);
}
}
Output :
The reversed string is : lennahC ym oT emocleW
For execution of the same do visit my YouTube Channel :
ITEngineer8995
September 29, 2020
Java Program To Reverse a Number using Recursion
In this Java Program, we reverse a number using Recursion.
The process in which the function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function
Step 1 : Import Scanner Class
Step 2 : Create a method for reverse
Step 3 : Apply if condition in which the number should be smaller than 10
Step 4 : Print the number
Step 5 : Apply else condition in which number should mod by 10 and print the result
Step 6 : Apply reverse method condition in which number should be divided by 10
Step 7 : Create main method
Step 8 : Initialize num to 0
Step 9 : As the user to enter the number and press enter
Step 10 : Create Scanner object
Step 11 : Store the user entered number into num
Step 12 : Print Reverse of the input number is.
Step 13 : Call the reverseMethod method
Program :
import java.util.Scanner;
public class RecursionReverseDemo
{
//A method for reverse
public static void reverseMethod(int number)
{
if(number < 10)
{
System.out.println(number);
return;
}
else
{
System.out.print(number % 10);
//Method is calling itself : recursion
reverseMethod(number / 10);
}
}
public static void main(String args[])
{
int num = 0;
System.out.println("Input your number and press enter:");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
System.out.println("Reverse of the input number is:");
reverseMethod(num);
System.out.println();
}
}
Output :
Input your number and press enter:
5678901
Reverse of the input number is:
1098765
For execution of the same do visit my YouTube Channel :
ITEngineer8995
September 20, 2020
Java Program To Reverse a Number using For Loop
In this Java Program, we reverse a number using For loop.
Logic :
for (;num != 0;)
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
Step 1 : Import the Scanner Class
Step 2 : Create the main method
Step 3 : Initialize num and reversenum equals to zero
Step 4 : Ask the user to enter the number by using System.out.println() method
Step 5 : Create Scanner object.
Step 6 : Store the captured input in num
Step 7 : Apply for loop logic to reverse the number
Step 8 : In for loop, num should be not equal to zero
Step 9 : Multiply reversenum with 10 and store its result in reversenum.
Step 10 : Add num%10 to reversenum and store the result into reversenum
Step 11 : Divide num by 10 and store the result in num.
Step 12 : Print the reversenum
Program :
import java.util.Scanner;
public class ForLoopReverseDemo
{
public static void main(String args[])
{
int num=0;
int reversenum = 0;
System.out.println("Input your number and press enter:");
//This statement will capture the user input
Scanner sc = new Scanner(System.in);
//Captured input would be stored in the number num
num = sc.nextInt();
//For loop: No initialization part as num is already initialized.
//No increment/decrement part as logic
//num = num/10 already decrements the value of num
for (;num != 0;)
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of specified number is:"+reversenum);
}
}
Output :
ITEngineer8995
September 13, 2020
Java Program To Reverse a Number using While Loop
In this Java Program, we reverse a number using while loop.
Logic :
while (num != 0)
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
Step 1 : Import the Scanner Class
Step 2 : Create the main method
Step 3 : Initialize num and reversenum equals to zero
Step 4 : Ask the user to enter the number by using System.out.println() method
Step 5 : Create Scanner object.
Step 6 : Store the captured input in num
Step 7 : Apply while loop logic to reverse the number
Step 8 : In while loop, num should be not equal to zero
Step 9 : Multiply reversenum with 10 and store its result in reversenum.
Step 10 : Add num%10 to reversenum and store the result into reversenum
Step 11 : Divide num by 10 and store the result in num.
Step 12 : Print the reversenum
Program :
import java.util.Scanner;
public class ReverseNumberWhile
{
public static void main(String args[])
{
int num =0;
int reversenum = 0;
System.out.println("Input your number and press enter:");
//This statement will capture the user input
Scanner sc = new Scanner(System.in);
//Captured input would be stored in number num
num = sc.nextInt();
//While loop: Logic to find out the reverse number
while (num != 0)
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of input number is : "+reversenum);
}
}
ITEngineer8995
September 06, 2020
ITEngineer8995
September 02, 2020
ITEngineer8995
August 25, 2020
ITEngineer8995
August 13, 2020

ITEngineer8995
August 05, 2020

ITEngineer8995
July 28, 2020
ITEngineer8995
July 26, 2020

ITEngineer8995
July 24, 2020












