Found 9150 Articles for Object Oriented Programming

Java program to find sum of digits of a number using recursion

Manisha Chand
Updated on 18-Jun-2025 17:52:44

1K+ Views

In this article, we will understand how to find the sum of the digits of a number using recursion in Java. A recursive function is a function that calls itself multiple times until a particular condition or base condition is met. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. Problem Statement Write a program in Java to find the sum of the digits of a number using recursion. Below is a demonstration of the same. Input Enter the number : 12131415 ... Read More

Java program to pass lambda expression as a method argument

Alshifa Hasnain
Updated on 20-Feb-2025 18:26:51

596 Views

In this article, we will learn to pass a lambda expression as a method argument in Java. A lambda expression is a short block of code that takes in parameters and returns a value. Lambda Expression Lambda expressions allow passing functionality as a method parameter, reducing the need for anonymous classes. They are often used in functional interfaces, especially when working with collections and streams. Problem Statement Given an Arraylist the goal is to reverse the strings present in it. Below is a demonstration of the same − Input ("Apple", "Orange", "Grapes") Output elppA, egnarO, separG Reversing Strings in ... Read More

Java program to calculate the power using recursion

Manisha Chand
Updated on 18-Jun-2025 17:50:23

1K+ Views

To calculate the power of a number, Java provides different ways to do this using loops, built-in methods, and recursion. In this article, we will learn how to calculate the power of a number using recursion. In Java, Recursion is a process in which a function calls itself multiple times until it reaches a base condition, and the corresponding function is called a recursive function. Problem Statement Given a base number and a power value, we need to calculate the result of raising the base to the power using recursion. Below is a demonstration of the same. Input: Enter ... Read More

Java Program to Find Factorial of a Number Using Recursion

Manisha Chand
Updated on 18-Jun-2025 17:05:44

904 Views

In this article, we will understand how to find the factorial of a number using recursion in Java. The factorial of a positive number is the product of the number itself and all descending positive integers up to "n". The factorial of a negative number does not exist, and the factorial of 0 is 1. The factorial of a positive number is - factorial of n (n!) = 1 * 2 * 3 * 4 * ... * n Factorial Using a Recursive Function A recursive function is a function that calls itself multiple times until a particular condition is satisfied. ... Read More

Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

Shriansh Kumar
Updated on 19-Jun-2025 10:12:31

1K+ Views

The given task is to write a Java program to check if an integer can be expressed as the sum of two prime numbers. A number is said to be a prime if it has only two factors, which are 1 and itself, and cannot be divided by any other number. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number; all other prime numbers are odd numbers. Example Scenario Let's understand the problem with an example. The possible solution is 43 = 2 + 41. Here, 2 and ... Read More

Java Program to Make a Simple Calculator Using switch...case

AmitDiwan
Updated on 22-Feb-2022 09:22:12

4K+ Views

In this article, we will understand how to construct a simple calculator using switch-case. The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.Following are the arithmetic operations we are going to perform.AdditionSubtractionMultiplicationDivisionFloor DivisionModuloBelow is a demonstration of the same −InputSuppose our input is −The two inputs: 40.0 and 12.0 Operator:%OutputThe desired output would be −The result is 40.0 % 12.0 = 4.0AlgorithmStep 1 - START Step 2 - Declare three values namely my_input_1, my_input_2 and my_result and declare a character value namely operator. Step 3 - Read the required ... Read More

Java Program to Display Factors of a Number

Manisha Chand
Updated on 09-Jun-2025 12:39:33

7K+ Views

In this article, we will understand how to display the factors of a number. Factors are numbers that divide the original number without leaving a remainder. For example, 1, 2, 3, 4, 6, and 12 are factors of 12. If a and b are factors of a number, then a x b is also a factor of the number. If we multiply 3 and 5, we get 15. We say that 3 and 5 are factors of 15. The largest factor of any number is the number itself, and the smallest factor is 1. 1 ... Read More

Java program to display prime numbers between intervals using function

Alshifa Hasnain
Updated on 24-Dec-2024 17:46:58

575 Views

In this article, we will understand how to display prime numbers between intervals using a function in Java. We will be using two approaches: one with user input and the other with predefined input. Prime numbers The prime numbers are special numbers that have only two factors 1 and itself and cannot be divided by any other number. A number is a prime number if its only factors are 1 and itself. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is ... Read More

Java Program to Display Prime Numbers Between Two Intervals

Manisha Chand
Updated on 17-Jun-2025 19:21:10

2K+ Views

In this article, we will understand how to find prime numbers between two intervals. Prime numbers are special numbers that have only two factors, 1 and itself, and cannot be divided by any other number. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers. Problem Statement Write a program in Java to display all prime numbers between two given intervals. Below is a demonstration of the same. ... Read More

Java program to find the area of a parallelogram

AmitDiwan
Updated on 21-Oct-2024 11:06:11

593 Views

In this article, we will understand how to find the area of a parallelogram using Java. A parallelogram has two pairs of parallel equal opposite sides. It has a base and a height which is the perpendicular distance between the base and its opposite parallel side. The area of a parallelogram is calculated using the formula − base * height i.e. b x h Problem Statement Write a program in Java to find the area of a parallelogram. Below is a demonstration of the same − Input Base : 6 Height : 8 Output Area parallelogram is ... Read More

Advertisements