Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Perfect number program in Java
Ans:
In this tutorial you will learn how to write a program in Java to check a given number is perfect or not.
Read This: What is Perfect Number? Write a program in C for Perfect Number.
How this Java program will behave?
Java program for perfect number is written below. This program will take a positive number as an input and after performing some operation it will print given number is perfect or not.
Suppose you want to check a number like is perfect or not then you have to give that number as an input to this below program at time of execution.
After the calculation it will give you appropriate output.
Below is a program to check a given number is perfect or not in Java
import java.util.*;
class Main{
public static void main(String ...args){
int remainder, sum=0, i, originalNum;
Scanner sc= new Scanner(System.in);
System.out.print("please enter a number: ");
originalNum = sc.nextInt();
for (i = 1; i <= originalNum/2; i++)
{
remainder = originalNum % i;
if (remainder == 0)
{
sum = sum + i;
}
}
if (sum == originalNum)
System.out.print("given no. is perfect number");
else
System.out.print("given no. is not a perfect number");
}
}
Output:
Also prepare these Java Programs:
- Java Program to print Fibonacci series using recursive method
- Palindrome program in Java for number using Recursive method
- Java program to check a given is an Armstrong
- Java program to add two numbers without using addition operator
- How to remove a character from a string in java
- Write a program in Java to count occurrence of a given character in a String.
- Java program to find largest and smallest number in an array
- Java program to print first duplicate number in an array of 1-100
- Java Program to remove duplicate elements from an array
- Program to count repeated elements in an array java
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program