99 Java Source Codes
99 Java Source Codes
CODES
by
Dr. Dheeraj Mehrotra
(Author & National Awardee)
PROGRAM #1: to compute and print all prime numbers between 100 and
500
class test1
{
void prime()
{
int i,j,s=0,d=0;
for(i=100;i<500;i++)
{
s=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
s=s+1;
}
if(s==2)
{
System.out.println(i+" ");
d=d+1;
}
}
System.out.println();
System.out.println("the no. of prime no. are = "+d);
}
}
PROGRAM #2:
{
int i,j;
for(i=1;i<number;i++)
{
if (number%i==0)
{
if(i%2==0)
System.out.println(i+" is an even factor of "+number);
}
}
}
}
PROGRAM #3:
PROGRAM #4:
PROGRAM #5:
PROGRAM #6:
PROGRAM #7:
PROGRAM #8 :
for(int j=0;j<name.length;j++)
{
for(int i=j+1;i<name.length;i++)
{
if(name[i].compareTo(name[j])<0)
{
String temp = name[j];
name[j]=name[i];
name[i]=temp;
}
}
System.out.println(name[j]);
}
}
}
PROGRAM # 9 :
Description: Its a simple program which calculates the day of the week
for any given date & month of the year 2002.
class dateone
{
public void compute(int opt,int date,int day)
{
char b;
switch(opt)
{
case 1: if(date<=31)
{day=(8+date)%7;}
else
System.out.println("\nFor u'r kind information january has only 31
days.\n");
break;
case 2: if(date<=28)
{day=(11+date)%7;}
else
System.out.println("\nFor u'r kind information february has only 28
days.\n");
break;
case 3: if(date<=31)
{day=(11+date)%7;}
else
else
System.out.println("\nFor u'r kind information october has only 31
days.\n");
break;
case 11: if(date<=30)
{day=(11+date)%7;}
else
System.out.println("\nFor u'r kind information november has only 30
days.\n");
break;
case 12: if(date<=31)
{day=(6+date)%7;}
else
System.out.println("\nFor u'r kind information december has only 31
days.\n");
break;
}
if(day==0)
{System.out.println("\nIts SUNDAY.\n");}
if(day==1)
{System.out.println("\nIts MONDAY.\n");}
if(day==2)
{System.out.println("\nIts TUESDAY.\n");}
if(day==3)
{System.out.println("\nIts WEDNESDAY.\n");}
if(day==4)
{System.out.println("\nIts THURSDAY.\n");}
if(day==5)
{System.out.println("\nIts FRIDAY.\n");}
if(day==6)
{System.out.println("\nIts SATURDAY.\n");}
}
}
PROGRAM #10 :
// pascal triangle
class pascal
{
public void pascalw(int n) {
int [ ] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++) {
for (int j=0; j<=i; ++j)
System.out.print(pas[j]+" ");
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}
}
}
PROGRAM #11 :
PROGRAM #12 :
PROGRAM #13 :
r=isprime(c);
if ((r==1) && (c<=10000))
System.out.print(c+" ");
a=b;
b=c;
}
}
int isprime(int x)
{
int i;
if (x==1) return 0;
else
{
int p=1;
for(i=2;i<x;i++)
{
if (x%i==0)
p=2;
}
return (p);
}
}
}
PROGRAM #14 :
for(i=0;i<a.length();i++)
{
char b=a.charAt(i);
if(b=='a' || b=='e' || b=='i' ||b=='o' || b=='u')
x++;
}
System.out.println(x);
}
}
PROGRAM # 15 :
else
{
c=c+n;c=c%10;
c=65+(c-1);
System.out.print((char)(c));
}
}
else if(n<0)
{
n1=Math.abs(n);
if((c-n1) >=65)
System.out.print((char) (c-n1));
else
{
if(c>65)
c=c-65;
else
c=n1;
System.out.print((char)(90-(c-1)));
}
}
else if (n==0)
{
System.out.println("no change "+name);
break;
}
}
}
}
PROGRAM # 16 :
PROGRAM #17 :
{
int ii;
l=str.length();
System.out.print(str);
for(i=0;i<l;i++)
{
char a=str.charAt(i);
for(ii=0;ii<l;ii++)
{
char b = str.charAt(ii);
if (a==b)
freq=freq+1;
}
System.out.println(a+" occurs "+freq+" times");
freq=0;
}
}
}
PROGRAM # 18 :
PROGRAM #19 :
PROGRAM #20 :
PROGRAM #21 :
class linear{
protected int M[] = new int[10];
public void input(int num[])// Entry of array elements
{
for(int i=0;i<10;i++)
M[i]=num[i];
}
public void sort()
{
int x=0,i=0,j=0,n=10,flag=0;
// Display of array
System.out.println("The Array is ");
for(i=0;i<n;i++)
{
System.out.print(M[i]);
}
System.out.println();
for(int k=1;k<n;k++)
{
flag = M[k];
j=k-1;
while(flag<M[j] )
{
M[j+1] = M[j];
j=j-1;
if (j<0) break;
}
M[j+1]=flag;
}
System.out.println("The sorted array is ");
for(i=0;i<n;i++)
{
System.out.println(M[i]+ " ");
}
}
}
PROGRAM #22 :
int x=0,i=0,n=10,middle=0,pos=0,first=0;
for(i=0;i<n;i++)
{
M[i]=i+1;
}
// Display of array
for(i=0;i<n;i++)
{
System.out.println(M[i]);
}
int last = n-1;
pos=-1;
while((pos==-1)&&(first<=last))
{
// Application of binary search
middle=(first+last)/2;
if (M[middle]==s)
{
pos=middle;
}
if(M[middle]<s)
first=middle+1;
else
last=middle-1;
}
if (pos==-1)
System.out.println("Sorry not present in the list");
else
System.out.println("The element lies in the array at position"+(pos+1));
}
}
PROGRAM #23 :
{
System.out.println(M[i]);
}
for(i=0;i<n;i++)
{
if (M[i]==s)
System.out.println(M[i]+" occurs at position "+(i+1));
}
}
}
PROGRAM #24 :
class arraycheck
{
int a[][]=new int[3][3];
int l[][]=new int[3][3];
arraycheck()
{
int c=1;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{a[i][j]=c;
c++;}
}
}
void cal()
{
int srow=0,scol=0;
// Display of the array
System.out.println("The original array is ");
System.out.println();
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
// Computing sum of rows
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
srow=srow+a[i][j];
}
System.out.println("The sum of row "+(i+1)+ " is "+srow);
srow=0;
}
int k=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scol=scol+a[j][i];
k=j+1;
}
System.out.println("The sum of column "+k+ " is "+scol);
scol=0;
}
}
}
PROGRAM #25 :
{if (a[i]>a[j])
{js=a[i];
a[i]=a[j];
a[j]=js;}
}
}
for(int g=0;g<s;g++)
{System.out.print(a[g]);
}
}
}
PROGRAM #26 :
PROGRAM #27 :
Solution:
public class Primes
{
public boolean isPrime(int n)
{
// Returns true if n is a prime false otherwise
// assume n is >1
// initialise instance variables
int x=0;
int count = 0;
for(x=2;x<n;x++)
{
if (n%x==0)
count++;
}
if (count ==0)
return true;
else
return false;
}
public void twinPrime(int limit)
{
int num=0,i=0;
for(i=2;i<limit-2;i++)
{
num=i+2;
if(isPrime(i)&& isPrime(num))
System.out.println(i+ +num+ are twin primes);
}
}
}
PROGRAM #28 :
PROGRAM # 29 :
A palindrome number is one that reads same from left to right or right to
left. There is an ingenious method to get a palindrome from any positive
integer. The procedure is as follows: Start with any positive number of 2
for(int i=0;i<15;i++)
{
r=reverse(n1);
p=palin(r+n1);
if(p==1)
{
System.out.println("Initial Number:"+n);
System.out.println("Number is:"+(r+n1) + "
which is palindrome in step "+c);
flag=0;
break;
}
else
{
c=c+1;
n1=n1+r;
}
}
if (flag==1)
System.out.println("The Program is Terminated because it has exceded
15 steps and Palindrome was not achieved!");
}
}
PROGRAM #30 :
PROGRAM #31 :
PROGRAM #32 :
{
System.out.print(c);
}
System.out.println();
c=c-1;
}
}
}
PROGRAM #33 :
PROGRAM #34 :
Program to frame a simple calculator with functions like add, sub, mult,
divide.
public class Calculator
{
PROGRAM #35 :
Program to input name and marks in five subjects and compute avg. and
select the subjects available.
public class student
{
private int x;
public void result(String n,int a,int b,int c,int d,int e )
{
x = (a+b+c+d+e)/5;
if(x>=90)
System.out.println("science with computers");
else if(x>=80 && x<=89)
System.out.println("science without computers");
else if(x>=70 && x<=79)
System.out.println("commerce with maths");
else if(x>=60 && x<=69)
System.out.println("commerce without maths");
System.out.println(n);
}
}
PROGRAM #36 :
PROGRAM #37 :
{
private int x,l,i,j;
char b;
public alphabet()
{
// initialise instance variables
x = 0;
}
public void arrange(String a)
{
l=a.length();
for(i=65;i<=90;i++)
{
for(j=0;j<l;j++)
{
b=a.charAt(j);
x=b;
if(x==i || x==i+32)
System.out.print(b);
}
}
}
}
PROGRAM #38 :
PROGRAM #39 :
PROGRAM #40 :
This function allows the user to enter a number and than rounds it off,
// correct to the nearest integer.
class roundoff
{
public void working(double input)
{
double remainder=0; // INITIALIZATION AND DECLARATION OF
int whole=0,answer=0;
// VARIABLES
whole = (int)input;
THE VALUE
answer = whole + 1;
else
// OTHERWISE
answer = whole;
// END OF FUNCTION
PROGRAM #41 :
PROGRAM #42 :
class magicsquare
{
int arr[][]=new int[10][10];
public void working(int n, int x)
{
int a,b,i;
int j,k;
for (i=0; i<n; i++)
{
for (k=0; k<n; k++)
{
arr[i][k]=-1;
}
}
arr[0][n/2]=x;
magic(0,n/2, x, n);
for (i=0; i<n; i++)
{
for (k=0; k<n; k++)
{
System.out.print(arr[i][k]+" ");
}
System.out.println();
}
}
void magic( int a, int b, int x, int n)
{
int c;
c=1;
int i;
for (x=x+1,i=2; i<=n*n; i++,c++,x++)
{
if(c<n)
{
a=a-1;
b=b-1;
if (a==-1)
a=n-1;
if (b==-1)
b=n-1;
}
else
{
a=a+1;
c=0;
}
arr[a][b]=x;
}
}
}
PROGRAM # 43 :
PROGRAM #44 :
Program to input any number and display the sum of its digits
class sumdigit
{
public void sum(int n)
{
int sum=0,q=1,r;
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
System.out.println("sum of digits is"+sum);
}
}
PROGRAM #45 :
PROGRAM #46 :
Program to enter any number (max. 3 digits) and check the nature of its
digits.
class checkdigit
{
public void compute(int a)
{
if(a<10)
{
System.out.println(a+"is single digit no.");
}
if(a>10 && a<100)
{
System.out.println(a+"is double digit no.");
}
if(a>100)
{
System.out.println(a+"is triple digit no.");
}
}
}
PROGRAM #47 :
PROGRAM #48 :
PROGRAM #49 :
count=0;
c=c/10;
}
while(c!=0);
System.out.println(big+"is the maximum prime digit number");
}
}
PROGRAM #50 :
PROGRAM #51 :
PROGRAM #52 :
PROGRAM #53 :
Program to input a number and print its sum and average of digits
class sd
{
public void input(int number)
PROGRAM #54 :
Program to compute the sum of factorial of all the digits of any entered
number.
public class digitfact{
/** Compute and return x!, the factorial of x */
public int factorial(int x) {
int fact = 1;
int num = x, digit, sumfact=0;
// extracting digits of x
while(num>0)
{
digit = num%10;
for(int i = 1; i <= digit; i++) // loop
fact *= i;
// shorthand for: fact = fact * i;
sumfact = sumfact+fact;
System.out.println("The factorial of digit extracted is"+fact);
num=num/10;
fact=1;
}
return sumfact;
}
}
PROGRAM #55 :
Program to input any number and print the factorial of its prime digits.
public class prog
{
public void work(int n)
{
int digit,c=0;
int fact=1;
while(n>0)
{
digit = n%10;
// checking if digit is prime
for(int j=1;j<=digit;j++)
{if (digit%j==0) c=c+1;}
if (c==2)
{
// computing factorial of digit
for (int i=1;i<=digit;i++)
fact=fact*i;
System.out.println("The factorial of digit"+ digit+ " is "+fact);
fact=1;
}
c=0;
n=n/10;
}
}
}
PROGRAM #56 :
PROGRAM # 57 :
{
for (int i=1;i<=n;i++)
{
if (i%2==0)
System.out.print("+"+i);
else
System.out.print("-"+i);
}
}
}
PROGRAM #58 :
{
public void work(int n)
{
int digit, sum=0;
while(n>0)
{
digit = n%10;
// checking digit even
if (digit%2==0)
sum = sum + digit;
n=n/10;
}
System.out.println("The sum of the even digits of the number is
"+sum);
}
}
PROGRAM #59 :
{
public void work(int n)
{
int digit, sum=0;
while(n>0)
{
digit = n%10;
// checking digit even
PROGRAM #60 :
PROGRAM #61 :
Program to input any number and print the factorial of its digits.
public class prog
{
public void work(int n)
{
int digit;
int fact=1;
while(n>0)
{
digit = n%10;
// computing factorial of digit
for (int i=1;i<=digit;i++)
fact=fact*i;
System.out.println("The factorial of digit"+ digit+ " is "+fact);
fact=1;
n=n/10;
}
}
}
PROGRAM #62 :
PROGRAM #63 :
PROGRAM # 64 :
PROGRAM #65 :
PROGRAM #66 :
int digit,sum=0;
while(n>0)
{
digit = n%10;
// checking digit even
for(int i=1;i<digit;i++)
{
if (digit%i==0 )
sum = sum+i;}
System.out.println(sum+ " is the sum of factor of digit "+digit);
sum=0;
n=n/10;
}
}
}
PROGRAM #67 :
{
public void work(int n)
{
int digit,min=9;
while(n>0)
{
digit = n%10;
{
if (digit<min )
min =digit;}
n=n/10;
}
System.out.println("The minimum digit is "+min);
}
}
PROGRAM #68 :
while(n>0)
{
digit = n%10;
sum=sum+digit;
n=n/10;
}
if (sum%2==0)
System.out.println("The sum of digits is even");
else
System.out.println("The sum of digits is odd");
}
}
PROGRAM #69 :
{
public void work(int n)
{
int digit;
while(n>0)
{
digit = n%10;
// checking digit even
for(int i=1;i<digit;i++)
{
if (digit%i==0 )
System.out.println(i+ " is the factor of digit "+digit);
}
n=n/10;
}
}
}
PROGRAM #70 :
{
public void work(int n)
{
int digit, sum=0;
while(n>0)
{
digit = n%10;
// checking digit even
if (digit%2!=0)
sum = sum + digit;
n=n/10;
}
System.out.println("The sum of the odd digits of the number is
"+sum);
}
}
PROGRAM #71 :
{
public void work(int n)
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.print(" ");
}
}
}
PROGRAM #72 :
PROGRAM #73 :
PROGRAM #74 :
Program to compute the sum of factors of all the digits of any entered
number.
public class digitsumfact{
public void factor(int x) {
int f= 0;
int num = x, digit, sumfact=0;
// extracting digits of x
while(num>0)
{
digit = num%10;
for(int i = 1; i < digit; i++) // loop
{f=i+1; if(digit%i==0) sumfact = sumfact+i;
}
System.out.println("The sum of factors of "+f+" is "+sumfact);
sumfact=0;
num=num/10;
}
}
}
PROGRAM #75 :
/**
* program for printing number of words in a given string.
*
*
*
*/
public class string2
{
// instance variables - replace the example below with your own
private int x,l,i,b,c;
/**
* Constructor for objects of class string2
*/
public void string2()
{
// initialise instance variables
l = b=c=0;
}
public void convert_case(String a)
{
l=a.length();
for(i=0;i<l;i++)
{
char z=a.charAt(i);
if(z==' ')
b++;
}
b=b+1;
System.out.println("The total number of words in the string is "
+b);
}
}
PROGRAM #76 :
/**
* program for printing the reverse of a given string.
*
* * */
public class string2
{
private int x,l,i,b,c;
/**
* Constructor for objects of class string2
*/
public void string2()
{
// initialise instance variables
l = b=c=0;
}
public void convert_case(String a)
{
l=a.length();
for(i=l-1;i>=0l;i--)
{
System.out.print(a.charAt(i));
}
}
}
PROGRAM # 77 :
{c--;
System.out.print(" :"+c+" ");
c=0;
System.out.println();
}
}
}
}
PROGRAM #78 :
//
print the smallest word of an entered string
class string4
{
private int i,l,c,big,m,n;
char z;
String x;
public string4()
{
// initialise instance variables
c=0;
big=999;
}
public void work(String s)
{s=s+" ";
l=s.length();
m=0;
for(i=0;i<l;i++)
{
z=s.charAt(i);
c++;
if(z==' ')
{
n=i;
c--;
//System.out.print("
if(c<big)
{
big=c;
x=s.substring(m,n);
:"+c+" ");
m=n;
}
c=0;
}
}
System.out.println("The smallest word is "+x);
}
}
PROGRAM #79 :
/**
* program for printing number of spaces in a given string.
*
*
*
*/
public class string2
{
// instance variables - replace the example below with your own
private int x,l,i,b,c;
/**
* Constructor for objects of class string2
*/
public void string2()
{
// initialise instance variables
l = b=c=0;
}
public void convert_case(String a)
{
l=a.length();
for(i=0;i<l;i++)
{
char z=a.charAt(i);
if(z==' ')
b++;
}
System.out.println("The total number of spaces in the string is "
+b);
}
}
PROGRAM #80 :
/**
* convert first letter of each word to capital
*
*
*
*/
public class string3
{
private int i,l,b;
char z;
public string3()
{
// initialise instance variables
}
public void convert(String a)
{
l=a.length();
z=a.charAt(0);
if(z>='a' && z<='z')
{String x= new String(" ");
x=x+z;
System.out.print(x.toUpperCase());
}
for(i=1;i<l;i++)
{
z=a.charAt(i);
if(z==' ')
{ z=a.charAt(i+1);
if(z>='a' && z<='z')
{
String x= new String(" ");
x=x+z;
System.out.print(x.toUpperCase());i++;
}
}
else
System.out.print(z);
}
}
}
PROGRAM # 81 :
// length of each word of a sentence
//
print longest word
class string4
{
private int i,l,c,big,m,n;
char z;
String x;
public string4()
{
// initialise instance variables
c=0;
big=0;
}
public void work(String s)
{s=s+" ";
l=s.length();
m=0;
for(i=0;i<l;i++)
{
z=s.charAt(i);
c++;
if(z==' ')
{
n=i;
c--;
//System.out.print("
if(c>big)
{
big=c;
x=s.substring(m,n);
m=n;
}
c=0;
:"+c+" ");
}
}
System.out.println("longest word is "+x);
}
}
PROGRAM # 82 :
// pascal triangle
class pascal
{
public void pascalw(int n) {
int [ ] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++) {
for (int j=0; j<=i; ++j)
System.out.print(pas[j]+" ");
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}
}
}
PROGRAM # 83 :
/**
* program for counting number of upper csae and lower case.
*
*
*
*/
public class string2
{
// instance variables - replace the example below with your own
private int x,l,i,b,c;
/**
* Constructor for objects of class string2
*/
public void string2()
{
// initialise instance variables
l = b=c=0;
}
public void convert_case(String a)
{
l=a.length();
for(i=0;i<l;i++)
{
char z=a.charAt(i);
if(z>='A' && z<='Z')
b++;
}
System.out.println("upper caase characters are" +b);
}
}
PROGRAM # 84 :
// Program to print the frequency of most occurring digit of an entered
number.
public class prog
{
public void work(int n)
{
int numb[]=new int [9];
int digit,s=0;
int mostoccurringdigit=0;
int frequency=0;
int number=0;
System.out.println("The entered number is "+n);
while(n>0)
{
digit = n%10;
s=s+digit;
n=n/10;
numb[number]=digit;
number=number+1;
}
int c=0;
for(int k=0;k<=9;k++)
{
for(int i=0;i<number;i++)
{
{
if (numb[i]==k)
c++;
}
if (c>frequency)
{mostoccurringdigit=numb[i];
frequency=c;}
}
c=0;
}
System.out.println("The most occurring digit is
"+mostoccurringdigit);
System.out.println("It's frequency is "+frequency);
}
}
PROGRAM # 85 :
// Program to print the frequency of least occurring digit of an entered
number.
public class prog
{
public void work(int n)
{
int numb[]=new int [9];
int digit,s=0;
int leastoccurringdigit=0;
int frequency=9;
int number=0;
System.out.println("The entered number is "+n);
while(n>0)
{
digit = n%10;
s=s+digit;
n=n/10;
numb[number]=digit;
number=number+1;
}
int c=0;
for(int k=0;k<=9;k++)
{
for(int i=0;i<number;i++)
{
{
if (numb[i]==k)
c++;
}
if (c<frequency)
{leastoccurringdigit=numb[i];
frequency=c+1;}
}
c=0;
}
System.out.println("The least occurring digit is
"+leastoccurringdigit);
System.out.println("It's frequency is "+frequency);
}
}
PROGRAM # 86 :
// Program to print the frequency of digits of an entered number.
public class prog
{
public void work(int n)
{
int numb[]=new int [9];
int digit,s=0;
int number=0;
System.out.println("The entered number is "+n);
while(n>0)
{
digit = n%10;
s=s+digit;
n=n/10;
numb[number]=digit;
number=number+1;
}
int c=0;
for(int k=0;k<=9;k++)
{
for(int i=0;i<number;i++)
{
{
if (numb[i]==k)
c++;
}
}
System.out.println("The digit"+k+" occurs "+c+" times ");
c=0;
}
}
}
PROGRAM # 87 :
// Program to print the average of digits of an entered number.
public class prog
{
public void work(int n)
{
int digit,s=0;
int number=0;
System.out.println("The entered number is "+n);
while(n>0)
{
digit = n%10;
s=s+digit;
n=n/10;
number=number+1;
}
System.out.println("The sum of digits is "+s);
System.out.println("The number of digits is "+number);
double avg = s/number;
System.out.println("The average of all the digits of the number
is"+avg);
}
}
PROGRAM # 88 :
// Program to print the sum of cubes of each digit of an entered number.
public class prog
{
public void work(double n)
{
double digit,s=0;
System.out.println("The entered number is "+n);
while(n>0)
{
digit = n%10;
s=s+Math.pow(digit,3);
n=n/10;
}
System.out.println("The sum of cubes of all the digits of the
number is"+s);
}
}
PROGRAM # 89 :
// Program to print the sum of square roots of each digit of an entered
number.
public class prog
{
public void work(double n)
{
double digit,s=0;
System.out.println("The entered number is "+n);
while(n>0)
{
digit = n%10;
s=s+Math.sqrt(digit);
n=n/10;
}
System.out.println("The sum of square roots all the digits of the
number is"+s);
}
}
PROGRAM # 90 :
// Program to print the digits of an entered number in ascending order.
public class prog
{
public void work(int n)
{
int digit,num=n;
System.out.println("The entered number is "+n);
System.out.println("The sorted digits in ascending order is ");
for(int i=0;i<=9;i++)
{
while(n>0)
{
digit = n%10;
if (digit==i )
System.out.print(digit);
n=n/10;
}
n=num;
}
}
}
PROGRAM # 91 :
// Program to print the sum of factors of each digit of an entered number.
public class prog
{
public void work(int n)
{
int digit,num=n;
System.out.println("The entered number is "+n);
System.out.println("The factors of each digit are: ");
while(n>0)
{
digit = n%10;
System.out.println("The factors of digit "+digit +" are ");
for(int i=1;i<digit;i++)
{
if (digit%i==0)
System.out.print(i);
}
System.out.println();
n=n/10;
}
}
}
PROGRAM # 92 :
// Program to print the digits of an entered number in descending order.
public class prog
{
public void work(int n)
{
int digit,num=n;
System.out.println("The entered number is "+n);
System.out.println("The sorted digits in descending order is ");
for(int i=9;i>=0;i--)
{
while(n>0)
{
digit = n%10;
if (digit==i )
System.out.print(digit);
n=n/10;
}
n=num;
}
}
}
PROGRAM # 93 :
// Program to print the sum of squares of each digit of an entered
number.
public class prog
{
public void work(double n)
{
double digit,s=0;
System.out.println("The entered number is "+n);
while(n>0)
{
digit = n%10;
s=s+(digit*digit);
n=n/10;
}
System.out.println("The sum of squares of all the digits of the
number is"+s);
}
}
PROGRAM # 94 :
// Program to display the following series:
// 0 3 8 15 24 ....N terms, where N is the input.
class series
{
public void work(int n)
{
double i,j;
for(i=1;i<=n;i++)
{
System.out.print(Math.pow(i,2)-1);
}
System.out.print(" ");
}
}
PROGRAM # 95 :
// Program to check if the entered string is a prime palindrome.
class palind
{
public void computing(String s)
{
int i,j,k=0;
String ss="";
char s1;
k=s.length();
// checking if the string is a prime length.
int nc=k;
int cc=0;
for(i=1;i<=nc;i++)
{
if (nc%i==0)
cc=cc+1;
}
if (cc==2) System.out.println("The string is a prime");
else
System.out.println("The string is not a prime");
for(i=k-1;i>=0;i--)
{
s1=s.charAt(i);
ss=ss+s1;
}
System.out.println("The string is "+s);
System.out.println("The string in reverse is "+ss);
if(s.equals(ss))
System.out.println("The string is a palindrome");
else
PROGRAM # 97 :
// Program to print the sum of the following series:
// S = 1*2 + 2*3 + 3*4 + 4*5.... n terms. Where n is the input.
class sumseries
{
public void sum(int n)
{
int s=0, i=1,j=2;
for(i=1;i<=n;i++)
{
s= s+i*j;
j=j+1;
}
System.out.println("The sum of the series is "+s);
}
}
PROGRAM # 98 :
// Program to convert a given decimal number into binary.
class decbin
{
public void convert(int n)
{
int num[]=new int[20];
int s=0, c=0,i=1,j=2,d;
while(n>0)
{
d=n%2;
num[c]=d;
n=n/2;
c=c+1;
}
for( i=0;i<c;i++)
System.out.print(num[i]);
}
}
PROGRAM # 99 :
// This program will display all perfect numbers for numbers between 1
and 1000
// The program should display the perfect number alongwith the factors.
// eg. 6 = 1 + 2 + 3
class perfect
{
public void perfect() //Function that finds all the perfect numbers
{
int factor_sum=0; //The sum of the factors
//Main loop to check if it is a perfect number
for (long loop = 2; loop <=1000; loop++)
{
factor_sum = 0;
//If it is a perfect number, it adds the sums to
factor_sum
for (long factor = 1; factor < loop; factor++)
{
if (loop % factor == 0)
factor_sum += factor;
}
if(factor_sum==loop)
{
System.out.print(factor_sum+ "= 1");
//loop2 displays all the factors
for (long loop2=2; loop2 < loop; loop2++)
{
if(loop%loop2==0)
System.out.print( "+"+ loop2);
}
System.out.println();
}
}
}
}