

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find politeness of a number in java
The numbers which can be expressed as the sum of positive consecutive integers are known as polite numbers.
Ex: 5 = 2+3
The number of ways a number can be expressed as the sum of positive integers will be the Politeness of that number.
Ex: 9 = 4+5 || 2+3+4
Algorithm
- Get the prime factors of a number.
- Get the powers of prime factors greater than 2.
- Add 1 to all of them.
- Multiply them, subtract 1 from the result.
Program
import java.util.Scanner; public class PolitenessOfANumber { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int num = sc.nextInt(); int count = 0, result = 1; for(int i = 2; i< num; i++) { while(num%i == 0) { System.out.println(i+" "); num = num/i; if(i>2) { count ++; } result = result*(count+1); } if(num >2) { System.out.println(num); } System.out.println("Politeness of the given number is : "+(result-1)); } } }
Output
Enter a number 216 2 2 2 3 3 3 Politeness of the given number is : 3
- Related Questions & Answers
- Find politeness of a number in C++
- Find all divisors of a natural number in java
- Java Program to Find Factorial of a number
- Java program to find a cube of a given number
- Java Program to find minimum sum of factors of a number
- Java Program to Find sum of even factors of a number
- Java Program to find largest prime factor of a number
- Java Program to Find Factorial of a Number Using Recursion
- Find the largest number in a Java array.
- Find the smallest number in a Java array.
- Java Program to find Product of unique prime factors of a number
- Java Program to Find Sum of Digits of a Number using Recursion
- Java program to find the square root of a given number
- Java program to find the cube root of a given number
- How to find the number of days in a month of a particular year in Java?
Advertisements