Write a Program in Java to input a number and check whether it is a Pronic Number or Heteromecic Number or not.
Pronic Number : A pronic number, oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n (n + 1).
The first few pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 … etc.
import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
int flag = 0;
for(int i=1; i<=n; i++)
{
if(i*(i+1) == n)
{
flag = 1;
break;
}
}
if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}
}
Write a Program in Java to input a number and check whether it is a Niven Number or not..
Niven Number : In recreational mathematics, a Niven number, is an integer (in base 10) that is divisible by the sum of its digits.
Let’s understand the concept of Harshad Number through the following example:
•The number 18 is a Niven Number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)
•The number 1729 is a Niven Number in base 10, because the sum of the digits 1 ,7, 2 and 9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by 19 (1729 = 19 * 91)
•The number 19 is not a Niven Number in base 10, because the sum of the digits 1 and 9 is 10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19 % 10 = 9)
The first few Niven Number in base 10 are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190, 192, 195, 198, 200 etc.
import java.util.*;
class Niven Number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
int c = n, d, sum = 0;
//finding sum of digits
while(c>0)
{
d = c%10;
sum = sum + d;
c = c/10;
}
if(n%sum == 0)
System.out.println(n+" is a Harshad Number.");
else
System.out.println(n+" is not a Harshad Number.");
}
}
Write a program to input a word from the user and remove the duplicate characters present in it.
Example:
INPUT – abcabcabc
OUTPUT – abc
INPUT – javaforschool
OUTPUT – javforschl
INPUT – Mississippi
OUTPUT – Misp
import java.io.*;
class RemoveDupChar
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any word : ");
String s = br.readLine();
int l = s.length();
char ch;
String ans="";
for(int i=0; i<l; i++)
{
ch = s.charAt(i);
if(ch!=' ')
ans = ans + ch;
s = s.replace(ch,' '); //Replacing all occurrence of the current character by a space
}
System.out.println("Word after removing duplicate characters : " + ans);
}
}
Write a program to input a word from the user and remove the consecutive repeated characters by replacing the sequence of repeated characters by its single occurrence.
Example:
INPUT – Jaaavvvvvvvvaaaaaaaaaaa
OUTPUT – Java
INPUT – Heeeiiiissggoiinggg
OUTPUT – Heisgoing
import java.io.*;
class RemoveRepChar
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any word: "); // Inputting the word
String s = br.readLine();
s = s + " "; // Adding a space at the end of the word
int l=s.length(); // Finding the length of the word
String ans=""; // Variable to store the final result
char ch1,ch2;
for(int i=0; i<l-1; i++)
{
ch1=s.charAt(i); // Extracting the first character
ch2=s.charAt(i+1); // Extracting the next character
// Adding the first extracted character to the result if the current and the next characters are different
if(ch1!=ch2)
{
ans = ans + ch1;
}
}
System.out.println("Word after removing repeated characters = "+ans); // Printing the result
}
}
Pronic Number : A pronic number, oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n (n + 1).
The first few pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 … etc.
import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
int flag = 0;
for(int i=1; i<=n; i++)
{
if(i*(i+1) == n)
{
flag = 1;
break;
}
}
if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}
}
Write a Program in Java to input a number and check whether it is a Niven Number or not..
Niven Number : In recreational mathematics, a Niven number, is an integer (in base 10) that is divisible by the sum of its digits.
Let’s understand the concept of Harshad Number through the following example:
•The number 18 is a Niven Number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)
•The number 1729 is a Niven Number in base 10, because the sum of the digits 1 ,7, 2 and 9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by 19 (1729 = 19 * 91)
•The number 19 is not a Niven Number in base 10, because the sum of the digits 1 and 9 is 10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19 % 10 = 9)
The first few Niven Number in base 10 are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190, 192, 195, 198, 200 etc.
import java.util.*;
class Niven Number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
int c = n, d, sum = 0;
//finding sum of digits
while(c>0)
{
d = c%10;
sum = sum + d;
c = c/10;
}
if(n%sum == 0)
System.out.println(n+" is a Harshad Number.");
else
System.out.println(n+" is not a Harshad Number.");
}
}
Write a program to input a word from the user and remove the duplicate characters present in it.
Example:
INPUT – abcabcabc
OUTPUT – abc
INPUT – javaforschool
OUTPUT – javforschl
INPUT – Mississippi
OUTPUT – Misp
import java.io.*;
class RemoveDupChar
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any word : ");
String s = br.readLine();
int l = s.length();
char ch;
String ans="";
for(int i=0; i<l; i++)
{
ch = s.charAt(i);
if(ch!=' ')
ans = ans + ch;
s = s.replace(ch,' '); //Replacing all occurrence of the current character by a space
}
System.out.println("Word after removing duplicate characters : " + ans);
}
}
Write a program to input a word from the user and remove the consecutive repeated characters by replacing the sequence of repeated characters by its single occurrence.
Example:
INPUT – Jaaavvvvvvvvaaaaaaaaaaa
OUTPUT – Java
INPUT – Heeeiiiissggoiinggg
OUTPUT – Heisgoing
import java.io.*;
class RemoveRepChar
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any word: "); // Inputting the word
String s = br.readLine();
s = s + " "; // Adding a space at the end of the word
int l=s.length(); // Finding the length of the word
String ans=""; // Variable to store the final result
char ch1,ch2;
for(int i=0; i<l-1; i++)
{
ch1=s.charAt(i); // Extracting the first character
ch2=s.charAt(i+1); // Extracting the next character
// Adding the first extracted character to the result if the current and the next characters are different
if(ch1!=ch2)
{
ans = ans + ch1;
}
}
System.out.println("Word after removing repeated characters = "+ans); // Printing the result
}
}