0% found this document useful (0 votes)
8 views35 pages

Java Programs for Number Analysis

The document contains multiple Java programs that demonstrate various algorithms and functionalities, such as checking for pronic and perfect numbers, finding the greatest and smallest numbers, summing even and odd numbers, and identifying twisted and duck numbers. Each program includes an algorithm, code implementation, and a list of variables with their data types and purposes. The document serves as a comprehensive guide for learning basic Java programming concepts and problem-solving techniques.

Uploaded by

cseaimlbday
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views35 pages

Java Programs for Number Analysis

The document contains multiple Java programs that demonstrate various algorithms and functionalities, such as checking for pronic and perfect numbers, finding the greatest and smallest numbers, summing even and odd numbers, and identifying twisted and duck numbers. Each program includes an algorithm, code implementation, and a list of variables with their data types and purposes. The document serves as a comprehensive guide for learning basic Java programming concepts and problem-solving techniques.

Uploaded by

cseaimlbday
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Programs

1. Write a program to find whether the given number is a pronic number or not and display it.
(Pronic number is the number which is the product of two consecutive integers. Ex: 2*3=6, 1*2=2,
5*6=30)
Algorithm:
Step 1: Start of the algorithm for the main method().
Step 2: To input the number.
Step 3: To store the value of the entered number.
Step 4: Inside the loop:
 Syntax of the loop:
 Initialize the control variable i as 1.
 To check the condition (i.e. i<n).
 To increment the variable.
 Body of the loop:
 If the product of the two consecutive numbers are equal to the entered number.
 Then the flag will be 1.
Step 5: If the product is one it is a pronic number.
Step 6: Else it is not a pronic number.
Step 7: End.

import [Link];
public class Pronic_Number
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int n,flag=0;
[Link]("Enter a number:");
n=[Link]();
for(int i=0;i<n;i++)
{
if(i*(i+1)==n)
flag=1;
}
if(pro==1)
[Link]("It is a pronic number");
else
[Link]("It is not a pronic number");
}
}
Output:

List of variables:
Name of the variable Data type Purpose/Description
n int To take the input from the user.
flag int To check whether it is a pronic
number or not.
i int For loop iteration.
2. Write a program to accept a number and check whether the number is perfect or not. A number is
said to be perfect if the sum of the factors(including 1 and excluding the number itself) is same as the
original number.
Sample input:6
Sample output: Perfect number
Factors are 1+2+3=6.

import [Link];
public class Perfect
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int n,p=0;
[Link]("Enter a number:");
n=[Link]();
for(int i=1;i<n;i++)
{
if(n% i ==0)
p=p+i;
}
if(p==n)
[Link]("It is a perfect number");
else
[Link]("It is not a perfect number");
}
}
Output:

List of variables:
Name of the variable Data type Purpose/Description
n int To take the input from the user.
p int To check whether it is a perfect
number or not.
i int For loop iteration.
3. Write a program to accept a different numbers display the greatest and smallest numbers display the
greatest and the smallest numbers from a set of numbers entered by the user.
Program :
import [Link];
public class Set
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int n,g=0,s=100;
[Link]("Enter 10 different numbers");
for(int i=1;i<=10;i++)
{
n=[Link]();
if(n>g)
g=n;
if(n<s)
s=n;
}
[Link]("Largest number:"+g);
[Link]("Smallest number=:"+s);
}
}
Output:

List of variables:
Name of the variable Data type Purpose/Description
n int To take the input from the
user.
g int To check the greatest number..
s int To check the smallest number
i int For loop iteration.
4. Write a program to calculate the sum of all positive even numbers and the sum of all negative odd
numbers from a set of numbers. You can enter 0 to quit the program and thus it displays the result.

import [Link];
public class Neg_Odd_Pov_Even
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int i,sum=0,n,sum1=0,num;
[Link]("How many numbers do you want to enter:");
n=[Link]();
[Link]("Enter '0' to quit the program and display the result.");
[Link]("Enter the numbers");
for(i=1;i<=n;i++)
{
num=[Link]();
if(n==0)
break;
else
{
if(num%2==0&&num>0)
sum=sum+num;
else
sum1=sum1+ num;
}
}
[Link]("The sum of positive even numbers="+sum);
[Link]("The sum of negative odd numbers="+sum1);
}
}.
Output:

List of variables:
Name of the variable Data type Purpose/Description
n int To take many numbers to be
inputed.
i int For loop iteration
num int To take the input of the
numbers.
sum int To find the sum of all positive
numbers.
sum1 Int To find the sum of all the
negative numbers.
5. In the game of tossing a coin, you want to know the number of times you get ‘Head’ and ‘Tail’
respectively. You keep the record as ‘1’ (one) for getting ‘Head’ ‘0’ (zero) for ‘Tail’. Write a program
to perform the above task. Suppose you have tossed a coin for 20 times in this game.

public class Toss


{
public static void main(String args[])
{
int i,t=0,h=0;
double b;
for(i=1;i<=20;i++)
{
b=[Link]();
if(b==1)
h=h+1;
else
t=t+1;
}
[Link]("Number of times HEAD recieved="+h);
[Link]("Number of times TAIL recieved="+t);
}
}
Output:

List of variables:
Name of the variable Data type Purpose/Description
t int To keep the record for getting
tail.
h int To keep the record for getting
head.
b int To toss the coin randomly.
i int For loop iteration

6. Write a program to input a number. Display the product of the successors of even digits of the
number entered by user.
Sample input: 2745.
Sample output: 15.
[Hint: The even digits are :2 and 4.
The product of even digits is: 3 * 5=15]

import [Link];
public class Successors
{
public static void mina(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number:");
int num=[Link]();
int pro=1;
while(num>0)
{
int digit=num%10;
num=num/10;
if(digit%2==0)
{
++digit;
pro=pro*digit;
}
}
[Link]("The product of the successors of even digit="+pro);
}
}
Output:

List of variables:
Name of the variable Data type Purpose/Description
num int To take the input from the user
digit int To calculate the digit of the
number.
pro int To find the product of the
digits.
7. A prime number is said to be ‘Twisted prime’, if the new number obtained after reversing the digits
is also a prime number. Write a program to accept a number and check whether the number is ‘Twisted
Prime’ or not.
Sample input: 167.
Sample output: 761.
167 is a ‘Twisted Prime’

import [Link];
public class Twisted_Prime
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number:");
int num=[Link]();
int num1=num;
int rem,rev=0;
while(num>0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
[Link]("Reversed number:"+rev);
if(rev%2==0)
[Link](num1+" is not a twisted prime number");
else
[Link](num1+" is a twisted prime number");
}
}

Output:

List of variables:
Name of the variable Data type Purpose/Description
num int To take the input from the user
num1 int To store the value of num.
rem int To calculate the remainder.
rev int To reverse the number and to
check whether it is twisted
prime or not.
8. A number is said to be duck if the digit zero (0) is present in it. Write a program to accept a number
and check whether the number is Duck or not.
The program displays the message accordingly. (The number must not begin with zero.)
Sample input: 5063.
Sample output: It is a duck number.
Sample input: 7435.
Sample output: It is not a duck number.

import [Link];
public class Duck
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number which does not begin with zero:");
int num=[Link]();
int a=0;
while(num>0)
{
int digit=num%10;
num=num/10;
if(digit==0)
a=1;
}
if(a==1)
[Link]("It is a duck number");
else
[Link]("It is not a duck number");
}
}
Output:

List of variables:
Name of the variable Data type Purpose/Description
num int To take the input from the user
a int To check whether it is duck
number or not.
digit int To find the digit of the number.
9. A happy number is defined as:
Take a positive number and replace the number by the sum of the squares of its digits. Repeat the
process until the number equals 1(one). If the number ends with 1 then it is called a happy number.
Write a program to input a number and check whether it is a ‘Happy Number’ or not. The program
displays a message accordingly.
Sample input: 31.
Solution: 31-> 32 + 12 = 10 => 12 + 0 = 1.
Sample output: A Happy Number.

import [Link];
public class Happy_Number
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int num,sum=0,d;
[Link]("Enter a number:");
num=[Link]();
do
{
num=sum;
sum=0;
do
{
d=num%10;
sum=sum+d*d;
num=num/10;
}
while(num>0);
}
while(sum>9);
if(sum==1)
[Link]("It is a Happy Number.");
else
[Link]("It is not a Happy Number.");
}
}
Output:

List of variables:
Name of the variable Data type Purpose/Description
num int To take the input from the user
d int To calculate the digits of the
number.
sum int To store the value of the num,
to calculate the sum of the
digits and to check whether it is
a happy number or not.
10. Write a program to find the smallest digit of an integer that is input.
Sample inpuit: 6524.
Sample output: Smallest digit is 2.

import [Link];
public class Smallest_Digit
{
public static void mian(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number:");
int i=[Link]();
int d=0,s=i;
while(ii0)
{
d=i%10;
i=i/10;
if(d<s)
s=d;
}
[Link]("The smallest digit is "+s);
}
}
Output:

List of variables:
Name of the variable Data type Purpose/Description
i int To take the input from the user.
d int To get the single digit of the
number.
s int To store the value of i.

. Write a program to input a string and convert the 1 st letter of the word into capital letter and display
the new string.
Algorithm:
Step1:Start of algorithm for main () method.
Declaring local variable string s;
Display: “enter the string =”
Step 2:Get the input from the user of string type
s=[Link] ();
Now, find the length of string s using [Link]() method and that value in variable l of int type.
Trim the string using .trim(0; method and store that value in the variable s1 of string type.
Step 3:Display: “new string is”
Repeatedly mutate the string s1 using for loop. Declare the variable i and use the
incrementstatement to gradually increase the value of the i by 1.
Now, check for the space character and convert the next occurring character into upper case
using
[[Link]([Link](0))]
method and display that character
If, the character is not a space then
display the character occurring in the string
gradually.
Step 4:End of the algorithm.

import [Link];
public class FirstUpperCase
{
public static void main (String args[])
{
[Link]("Enter the String=");
Scanner p=new Scanner([Link]);
String s=[Link]();
int l=[Link]();
String s1=[Link]();
[Link]("New string is");
for(int i=0;i<l;i++)
{
if(i==0)
{
[Link]([Link]([Link](0)));
continue;
}
else if([Link](i-1)==' ')
[Link]([Link]([Link](i)));
else
[Link]([Link](i));
}
}
}

Output:
List of variables:
Name of the variable Data Types Description
i int Looping purpose.
s String To store the inputted word.
s1 String To store trimmed string .
l int To store last index value and
length of string of s and ss
respectively.

12. Write a program to input a string in uppercase and the convert ‘A’ into ‘AN’ make sure that
character should not be a part of the word. Display the new string.
Algorithm:
Step 1: Start of algorithm for main () method.
Declaring local variable String n;
Display: “Enter the String in uppercase=”
Step 2:Get the input from the user of String type
n=[Link] ();
Step 3:Declare local variable String s; int p=0, x;
n=n+” ”;
Step 4: Repeatedly modify the String s through concatenating word by usingsubstring method and
make sure that character ‘A’ is replace by ‘AN’ if it a word and not in between the word.
Step 5:Display the new string formed i.e., String s;
[Link] ("The Converted sentence is : "+s);
Step 6:End of the algorithm.

import [Link];
public class StringPatOwn
{
public static void main (String args[])
{
[Link]("Enter the String in uppercase=");
Scanner p1=new Scanner([Link]);
String n=[Link]();
String s="";
n=n+" ";
int p=0,x;
while(p<[Link]())
{
x=[Link](' ',p);
String word=[Link](p,x);
if([Link]("A")==0)
s = s+" "+"AN";
else
s = s+" "+word;
p = x+1;
}
[Link] ("The Converted sentence is : "+s);
}
}

Output:
List of variables:

Name of the variable Data Type Description


x int To store the index value of a
particular character.
n, word String To store the inputted word and
to store the substring of the n
respectively.
s String To store the string , which is
updating in the loop.
p int To store increment value of x.

13. Write a program to accept any three letter word and print all the probable threeLetter combination.
No letter combination should not repeat in the output.
import [Link];
public class Pthreeletters
{
public static void main (String args[])
{
[Link]("Enter a three letters:-");
Scanner p=new Scanner([Link]);
String str=[Link]();
[Link]("The required combinations of the words:-");
for(int i=0;i<[Link]();i++)
{
for(int j=0;j<[Link]();j++)
{
for(int k=0;k<[Link]();k++)
{
if(i!=j&&j!=k&&k!=i)
[Link]([Link](i)+""+[Link](j)+""+[Link](k)+" ");
}
}
}
}
}

Output:

Name of the variable Data Types Description

i, j, k int Loop purpose and also to check


the combinations occurring.
str String To store the inputted word.

List of variables:

14. Write a program in java to enter a string and to print the String in alphabeticalOrder of its letter.
import [Link];
public class AlphabeticalOrder
{
public static void main (String args[])
{
[Link]("Enter the String=");
Scanner p=new Scanner([Link]);
String s=[Link]();
int l=[Link]();
String s1=[Link]();
for(int i=97;i<=122;i++)
{
for(int k=0;k<l;k++)
{
if([Link](k)==(char)(i))
[Link]([Link](k));
}
}
}
}
Output:

List of variables:
Name of the variable Data Types Description
i, k int Loop purpose.
s String To store the inputted word.
l int To store the length of the string.
s1 String To store the lower cased
String of s.

15. Write a program in java to accept a word in lowercase and display the new word after removing the
entire repeated letter.
import [Link];
public class RepeatedRemove
{
public static void main (String args[])
{
[Link]("Enter a word:-");
Scanner p=new Scanner([Link]);
String str=[Link]();
String s="";
char ch1,ch2;
int t;
for(int i=0;i<[Link]();i++)
{
ch1=[Link](i);
t=0;
for(int j=0;j<[Link]();j++)
{
ch2=[Link](j);
if(ch1==ch2)
t=1;
}
if(t==0)
s+=ch1;
}
[Link]("The new String after removing duplicate letters ="+s);
}
}
Output:

List of variables:
Name of the variable Data Types Description
i, j int Loop purpose.
str String To store the inputted word.
ch1,ch2 char To store the character value of
string according to the index
value of the string.
s String To store concatenating
character in String .
t int To store occurrence.

[Link] a program to generate 10 character between A and J randomly.

import [Link];
public class
{
public static void main (String args[])
{
[Link]("Showing the alphabet randomly A and J");
int a1;
for(int i=1;i<=10;i++)
{
double a=[Link]();
int aa=(int)(a*10);
if((int)a==0)
{
char s=(char)(97+aa);
char p=[Link](s);
[Link](p+" ");
}
else
{
a1=(int)(aa%10);
char s1=(char)(97+a1);
char p1=[Link](s1);
[Link](p1+" ");
}
}
}
}
Output:

List of variables:
Name of the variable Data Types Description
i int Loop purpose.
s,p,s1,p1 char To store the character occurring
randomly from A to J.
a1,aa int To generate next 10 alphabet.
a double To store the random value.

17. Write a program to input the full name and display the initials along with surname.

import [Link];
public class AbbrevatedWordConvertor
{
public static void main (String args[])
{
[Link]("Enter the String=");
Scanner p=new Scanner([Link]);
String s=[Link]();
String s1=[Link]();
int w=[Link](" ");
int l=[Link]();
String ss=[Link](0,w);String s2="",k, m;
int l1=[Link]();
for(int i=0;i<l1;i++)
{
if([Link](i)==' ')
{
s2+=([Link](i+1)+".");
}
}
s2=([Link](0)+"."+s2);
m=[Link](w,l);
k=[Link](m);
[Link]("Abbrevated name with surname ="+k);
}
}
Output:

List of variables:
Name of the variable Data Types Description
i, j int Loop purpose.
s String To store the inputted word.
ss,s2,m String To store the substring of string
according to the index value of
the string.
s1 String To store trimmed string .
w, l,l1 int To store last index value and
length of string of s and ss
respectively.
k String To store final concated
character in String.

[Link] a program ,which will get text String and count all occurrence of a particular word.
import [Link];
public class ToCheckTheOccurOfWord
{
public static void main (String args[])
{
[Link]("Enter the String:-");
Scanner p=new Scanner([Link]);
String str=[Link]();
[Link]("Enter the word to be counted:-");
Scanner p1=new Scanner([Link]);
String wrd=[Link]();
String s=[Link](wrd,"$");
char a;
int count=1,ab=1;
for(int i=0;i<[Link]();i++)
{
char b=[Link](i);
if(b=='$')
ab++;
}
for(int j=0;j<[Link]();j++)
{
a=[Link](j);
if(a=='$')
count++;
}
int t=count-ab;
[Link]("The occurence of the word '"+wrd+"' :"+(count-1));
}
}
Output:

List of variables:
Name of the variable Data Types Description
i, j int Loop purpose.
str, wrd String To store the inputted String.
s String To store the replaced string.
count, ab int To store the occurrence of a
particular task or word.
t int To store the occurrence of the
inputted word.

19. Write a program to input a string and to check the maximum occurring character
and to display it.

import [Link];
public class StringMaxchar
{
public static void main (String args[])
{
[Link]("Enter the String=");
Scanner p=new Scanner([Link]);
String s=[Link]();
int m=1,max=0,i,j,t=1;
char c,mm=' ';
int l=[Link]();
for(i=0;i<l;i++)
{
c=[Link](i);
m=t;
for(j=0;j<l;j++)
{
if([Link](j)==c)
{
m++;
}
}
if(max<m)
{
mm=[Link](i);
max=m;
}
}
[Link]("Maximum character occuring is "+mm);
}
}
Output:

List of variables:

Name of the variable Data Type Description


i, j int Loop purpose.
s String To store the inputted word.
m, max, t int To store trimmed string .
l int To store last index value and
length of string of s and ss
respectively.
mm char To store character as per the
index value of i.

20. Write a program to generate the following string pattern.


BLUEJ
LUEJB
UEJBL
EJBLU
JBLUE
import [Link];
public class Patternconfuse
{
public static void main (String args[])
{
[Link]("Enter the String=");
Scanner p=new Scanner([Link]);
String s=[Link]();
int l=[Link]();
String s1,s2,t;
for(int i=0;i<l;i++)
{
s1=[Link](i,(l));
s2=[Link](0,i);
t=[Link](s2);
[Link](t);
[Link]();
}

}
}
Output:

List of variables:
Name of the variable Data Types Description
i int Loop purpose.
s String To store the inputted word.
s1,s2 String To store the substituted string.
t String To store the concatenated
String.

You might also like