Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 69

99 JAVA SOURCE

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:

to input a number and print its even factors


class compute
{
public void working(int number)

{
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:

to define a function fact() to compute the factorial of any entered


number.
class compute
{
public void fact( int a)
{
int s=1;
for(int i=1;i<=a;i++)
s=s*i;
System.out.println("The factorial of "+a+" is "+s);
}
}

PROGRAM #4:

to define a function found()


// to print the Highest Common Factor of any two numbers.
class compute
{
public void found(int a, int b) // declaration of the function
{
int p,h=0;
p=a*b;
for (int i=1;i<=p;i++)
{
if (a%i==0 && b%i==0)
h=i;
}
System.out.println("The HCF of the given two numbers is "+h);
}
}

PROGRAM #5:

to define and utilize a function perfect(), to print all perfect numbers


from 1 to 100. ( A perfect number is a number which is equal to the
sum of its factors eg. 6=1+2+3, hence 6 is a perfect number).
class compute
{
public void perfect()
{
int sum=0;
for (int i=1;i<=100;i++)
{
for(int j=1;j<=i-1;j++)
{
if (i%j==0)
sum = sum + j;
}
if (sum==i)
System.out.println(i+" is a perfect number");
sum = 0;
}
System.out.println();
}
}

PROGRAM #6:

to compute prime factors of any entered number with the help of a


function pfact().
class compute
{
void pfact(int n) // Definition
{
int t=-1;
int anum[]=new int[10];
for (int i=1;i<=n-1;i++)
{
if (n%i==0)
{
t=t+1;
anum[t]=i;
}
}
for (int count = 0, k=0;k<=t;k++)
{
System.out.println(anum[k]+" is a factor of "+n);

for (int m=1;m<=anum[k];m++)


{
if (anum[k]%m==0) count = count + 1;
}
if (count==2) System.out.println(anum[k]+" is also prime");
count = 0;
}
}
}

PROGRAM #7:

input a number and print its binary equivalent.


class compute
{
public void work(int n)
{
int a[]=new int [10];
int c=-1,p;
while(n>0)
{
c=c+1;
p=n%2;
if (p==0)
{ a[c]=0;}
else
{a[c]=1;}
n=n/2;
}
for(int i=c;i>=0;i--)
{
System.out.print(a[i]);
}
}
}

PROGRAM #8 :

Sorting string using Bubble Sorting Technique


class sorting
{
public void working()
{
String name[]= {
"Shruti","Shashank","Yogita","Dheeraj","Ratnesh","Sharad","Vanya"};

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

System.out.println("For u'r kind information march has only 31


days.\n");
break;
case 4: if(date<=30)
{day=(7+date)%7;}
else
System.out.println("\nFor u'r kind information april has only 30
days.\n");
break;
case 5: if(date<=31)
{day=(9+date)%7;}
else
System.out.println("\nFor u'r kind information may has only 31
days.\n");
break;
case 6: if(date<=30)
{day=(12+date)%7;}
else
System.out.println("\nFor u'r kind information june has only 30
days.\n");
break;
case 7: if(date<=31)
{day=(7+date)%7;}
else
System.out.println("\nFor u'r kind information july has only 31 days.\n");
break;
case 8: if(date<=31)
{day=(10+date)%7;}
else
System.out.println("\nFor u'r kind information august has only 31
days.\n");
break;
case 9: if(date<=30)
{day=(6+date)%7;}
else
System.out.println("\nFor u'r kind information september has only 30
days.\n");
break;
case 10: if(date<=31)
{day=(8+date)%7;}

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 :

to display the Pascals Triangle

// 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 :

Develop a program to input any number


// and print them in descending order without using the sorting
technique.
class numsort
{
public void work(int number)
{
int c[]=new int[6];
int digit, sum=0,i=0;
do
{
digit = number%10;
i++;
c[i]=digit;
number = number/10;
}
while (number!=0);
// display in sorted manner
System.out.println("The sorted digit format is ");
for(int m=9;m>=0;m--)
{
for(int j=1;j<=i;j++)
{
if (c[j]==m) System.out.print(c[j]);
}
}
}

PROGRAM #12 :

Code a program to compute the prime factors of any number


// using a function
class primefactors
{
void fact(int n) // Definition
{
int cc=0;
for (int i=1;i<=n-1;i++)
{
if (n%i == 0)
{
// checking prime
for(int k=1;k<=i;k++)
{
if (i%k==0) cc++;
}
if (cc==2) System.out.println(i+" is a prime factor of "+n);
cc=0;
}
}
}
}

PROGRAM #13 :

Program to display all prime fibonacci series between 1 to 10000


class primefibonacci
{
public void work()
{
System.out.println("All prime fibonacci series in the range of 1 to 10000
are");
int r;
int a=1,b=2,c=0;
r=isprime(a);
if (r==1) System.out.print((a)+" ");
r=isprime(b);
if (r==1) System.out.print((b)+" ");
while (c<=10000)
{
c=a+b;

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 :

Program to input any string and display the number of vowels.


public class vowels
{
private int x,i;
/**
* Constructor for objects of class vowels
*/
public vowels()
{
// initialise instance variables
x = 0;
}
public void vowel(String a)
{

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 :

Program which replaces each character in s by the character which is at a


distance of move ahead, if move is positive. If move is 0 the original
string is returned unchanged. If move is negative, then each character is
replaced by a character at a distance move behind it. For example:
When s=ABCXYX, encodeDecode(2) returns CDEZAB and
When s=ABCDE, encodeDecode(-3) returns XYZAB
[ICSE
Sample Paper-2005]
class decode
{
public void compute(String name,int n)
{
int j,i,l,c=0,y,n1;
l=name.length();
System.out.println("original string is "+name);
for(i=0;i<l;i++)
{
char c1=name.charAt(i);
try
{
c=(int)c1 ;
}
catch(NumberFormatException e)
{}
if(n>0)
{
if((c+n)<=90)
System.out.print((char)(c+n));

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 to input a string and print each word in reverse.

public class reverse


{
// instance variables - replace the example below with your own
private int i,a1,l,p,j;
/**
* Constructor for objects of class reverse
*/
public reverse()
{
p=0; // initialise instance variables
}

public void sampleMethod(String str)


{
l=str.length();
System.out.print(str);
for(i=0;i<l;i++)
{
char a=str.charAt(i);
/* System.out.print(a);*/
a1=(int) a;
if(a1==32 || i==l-1)
{
for(j=i;j>=p;j--)
{
char b=str.charAt(j);
System.out.print(b);
}
System.out.print(" ");
p=i;
}
}
}
}

PROGRAM #17 :

Program to print the frequency of all the characters within a string.


public class frequency
{
// instance variables - replace the example below with your own
private int i,a1,l,p,j,freq;
/**
* Constructor for objects of class reverse
*/
public frequency()
{
p=0;
freq=0;// initialise instance variables
}
public void sampleMethod(String str)

{
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 to display the output as:


// 55555
// 54444
// 54333
// 54322
// 54321
class summation
{
public void display()
{
int a=5,c=5;
for (int i=5;i>=1;i--)
{
for (int k=a;k>=i;k--)
{
System.out.print(k);
}
for (int j=1;j<=c-1;j++)
{
System.out.print(c);
}
System.out.println();
c=c-1;
}
}

PROGRAM #19 :

Program to Sort an array using Bubble Sort Method


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();
// Bubble Sort
for(int k=0;k<n;k++)
{
for(int kk=0;kk<n-1;kk++)
{
if(M[kk]>M[kk+1])
{
flag = M[kk];
M[kk]=M[kk+1];
M[kk+1]=flag;
}
}
}
System.out.println("The sorted array is ");
for(i=0;i<n;i++)
{
System.out.println(M[i]+ " ");
}
}
}

PROGRAM #20 :

Program to sort numbers using Selection Sort Method


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();
// Selection Sort
for(int k=0;k<n-1;k++)
{
for(int kk=k+1;kk<n;kk++)
{
if(M[k]>M[kk])
{
flag = M[k];
M[k]=M[kk];
M[kk]=flag;
}
}
}
System.out.println("The sorted array is ");
for(i=0;i<n;i++)
{
System.out.println(M[i]+ " ");
}
}
}

PROGRAM #21 :

Program to sort any array of numbers using Insertion Sort Method

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 :

Program to perform Binary Search in an array of numbers:


class fillarray {
protected int M[] = new int[10];
public void fill(int s)// argument s to be the element to be searched
{

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 :

Program to perform Linear Search in a given array of numbers:


class linear{
protected int M[] = new int[10];
public void fill(int s)// argument s to be the element to be searched
{
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]);
}
for(i=0;i<n;i++)
{
if (M[i]==s)
System.out.println(M[i]+" occurs at position "+(i+1));
}
}
}

PROGRAM #24 :

Displaying the sum of row and columns of a double dimension array.

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 :

Program to enter a number and sort the digits in ascending


// order. Display the new digit so obtained.
class work
{
public void compute(int n)
{
int a[] = new int [10];
int js,b,d,s=0;
while(n>0)
{
d=n%10;
a[s]=d;
s=s+1;
n=n/10;
}
for(int i=0;i<s-1;i++)
{ for(int j=i+1;j<s;j++)

{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 :

Store N elements in an array WORK and compute the total number of


// times M occurs in WORK. Where N and M are the inputs alongwith the
// elements of the array.
Program to print the frequency of M in
an
// array of N elements.
class freqarray
{
private int a[]=new int[10];
private int nnn,mmm;
public void cal()
{
nnn=10;
int c=0,i;
for(i=0;i<nnn;i++)
if (a[i]==mmm) c=c+1;
System.out.println("The frequency of "+mmm+" in the array is "+c);
}
public void work1(int nn,int mm,int num[])
{
for(int i=0;i<nn;i++)
{a[i]=num[i];
}
nnn=nn;
mmm=mm;
}
}

PROGRAM #27 :

Program to input any number and check if it is a prime. Also implement


the logic of display of all twin primes within a given limit.

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 to compute the middle digit of any entered number. If the


number does not have a middle digit, it has to return the average of the
middle two digits.
class number
{
int nthno(int m,int n)
{
int a[] = new int [100];

int b[] = new int [100];


int x,mm,i=0;
mm=m;
while(mm!=0)
{
i=i+1;
x=mm%10;
a[i]=x;
mm=mm/10;
}
for(int j=0;j<i;j++)
{
b[j+1]=a[i-j];
}
x=b[n];
return(x);
}
void inputnumber(int m)
{
int z,s,c=0;
z=m;
while(z!=0)
{
z=z/10;
c=c+1;
}
if((c%2)==0)
{
int s1,s2;
s1=nthno(m,c/2);
s2=nthno(m,(c+2)/2);
s=(s1+s2)/2;
}
else
{
s=nthno(m,(c+1)/2);
}
System.out.println("Middle term is="+s);
}
}

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

or more digits. Obtain another positive number by reversing the digits of


the starting number. Add the two numbers together. Repeat the whole
procedure with the sum as the starting number till you get a sum which is
a palindrome number.
// Program to calculate the palindrome of a given number in maximum 15
terms. Assume that the starting number is not a palindrome. Sample
Input: 87. Output: 4884 Steps: 4
class number
{
int reverse(int x)
{
int s,t=0;
while(x!=0)
{
s=x%10;
t=(t*10)+s;
x=x/10;
}
return(t);
}
int palin(int x)
{
int s,t=0,r;
r=x;
while(r!=0)
{
s=r%10;
t=(t*10)+s;
r=r/10;
}
if(t==x)
{
return(1);
}
else
{
return(0);
}
}
void palcal(int n)
{
int r,c=1,p,n1;
n1=n;
int flag=1;

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 to print the sum of factorial of the digits of an entered number.


class work
{
void working(int a)
{
long fact =1,i,j,sum=0;
int num=a;
int digit;
while(num>0)
{
digit = num%10;
for(i=1;i<=digit;i++)
{
fact = fact * i;
}
sum = sum + fact;
num = num/10;
fact = 1;
}
System.out.println(The sum of factorials of the digits of+a+ is +sum);
}
}

PROGRAM #31 :

Example class to demostrate the shorthand notation


public class shorthand
{
public void work()
{
int a=10;
float b=20;
a+=10;
b-=10;
System.out.println(a is +a);
System.out.println(b is +b);
a*=2;
b/=2;
System.out.println(Now a is +a);
System.out.println(Now b is +b);
a%=10;
b%=10;
System.out.println(Now a is +(++a));
System.out.println(Now b is +(++b));
}
}

PROGRAM #32 :

Program to display the output as:


// 55555
// 54444
// 54333
// 54322
// 54321
class summation
{
public void display()
{
int a=5,c=5;
for (int i=5;i>=1;i--)
{
for (int k=a;k>=i;k--)
{
System.out.print(k);
}
for (int j=1;j<=c-1;j++)

{
System.out.print(c);
}
System.out.println();
c=c-1;
}
}
}

PROGRAM #33 :

Program to sort a list of numbers generated randomly.


public class SortNumbers {
public static void sort(double[] nums) {
for(int i = 0; i < nums.length; i++) {
int min = i; // holds the index of the smallest element
// find the smallest one between i and the end of the array
for(int j = i; j < nums.length; j++) {
if (nums[j] < nums[min]) min = j;
}
// Swaping being done using the third variable tmp.
double tmp;
tmp = nums[i];
nums[i] = nums[min];
nums[min] = tmp;
}
}
public void work(){
double[] nums = new double[10];
// Create an array to hold
numbers
for(int i = 0; i < nums.length; i++) // Generate random numbers
nums[i] = Math.random() * 100;
sort(nums);
// Sort them
for(int i = 0; i < nums.length; i++) // Print them out
System.out.println(nums[i]);
}
}

PROGRAM #34 :

Program to frame a simple calculator with functions like add, sub, mult,
divide.
public class Calculator
{

// instance variables - replace the example below with your own


private double result;
/**
* Constructor for objects of class Calculator
*/
public Calculator()
{
// initialise instance variables
result = 0;
}
public double add( double value)
{
// put your code here
return result+=value;
}
public double subtract( double value)
{
// put your code here
return result-=value;
}
public double multiplyBy( double value)
{
// put your code here
return result*=value;
}
public double divideBy( double value)
{
// put your code here
if(value==0.0)
return result;//can not divide by 0, returning last result
else
return result/=value;
}
public double clear()
{
// put your code here
return result=0;
}
}

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 to print the reverse of any entered string.


public class reverse
{
private int x,i,l;
public reverse()
{
x = 0;
}
public void array1(String a)
{
l=a.length();
System.out.print("the reverse string is ");
for(i=l-1;i>=0;i--)
System.out.print(a.charAt(i));
}
}

PROGRAM #37 :

Program to arrange an entered string in Alphabetical order.


public class alphabet

{
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 :

Develop a program to input a list of elements in a double dimensional


array of 3x3 and print the maximum and the minimum elements
alongwith their location within the array.
class maxminarray
{
protected int number[][] = new int[3][3];
private int sm,lg,p1,p2,q1,q2;
public void inputvalues(int num[][])
{
for(int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
number[i][j]=num[i][j];
}
}
}

public void comp()


{
int max=number[0][0];
int min=number[0][0];
for(int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{if(max<number[i][j])
{
p1=i;
p2=j;
lg=number[i][j];
}
else
{
q1=i;q2=j;sm=number[i][j];
}
}
}
System.out.println("largest"+lg+"position in row"+p1+"column "+p2);
System.out.println("smallest "+sm+"position in row"+q1+"column
"+q2);
}
}

PROGRAM #39 :

Program to Sort a given Array in Descending Order


class ArraySort
{
public void work()
{
int number [] = {15,8,12,32,52};
int n = number.length;
System.out.print("The elements of array are:");
for (int i = 0; i<n; i++)
{
System.out.println(number[i]);
}
for (int i = 0; i<n; i++)
{
for (int j =i+1; j<n; j++)
{
if (number [i] <number [j])
{

int temp = number[i];


number[i] = number[j];
number [j] = temp;
}
}
}
System.out.print("Sorted Elements of the Array are: ");
for (int i = 0; i<n; i++)
{
System.out.println(number[i]);
}
}
}

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

// THIS LINE STORES THE INTEGER PART OF


// INTO A SEPERATE VARIABLE

remainder = input - whole;


DECIMAL

// REMAINDER IS THE VALUE AFTER THE


// POINT

if (remainder >= 0.5)

// IF REMAINDER IS GREATER THAN 0.5

answer = whole + 1;
else

// ADD 1 TO THE VALUE

// OTHERWISE
answer = whole;

// KEEP THE INTEGER PART AS IT IS

System.out.println("Rounded Off Number Is : " +answer); //


DISPLAY THE ANSWER

// END OF FUNCTION

PROGRAM #41 :

Program to display a Triangle of numbers called the Pascal's Triangle,

when provided with the number of rows.


class pascal
{
public void working(int n)
{
int pal[][]= new int[50][50];
int i,j,k;
for (i=0;i<n;i++)
{
pal[i][0]=1;
pal[i][i]=1;
for (j=1;j<i;j++)
{
pal[i][j]=pal[i-1][j-1]+pal[i-1][j];
}
for (j=1;j<=(n-(i+1));j++)
{
System.out.print(" ");
}
for (k=0;k<=i;k++)
{
System.out.print(pal[i][k]);
}
System.out.println();
}
}
}

PROGRAM #42 :

This program creates a magic square box of order n x n.


// The speciality of a magic square box is that the sum of elements of
//any row or any column // is always equal.
// Enter the row of matrix and the starting index of matrix number
generation

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 to check if any entered number is a Magic Number


class magic
{
public void working(int num)
{
int num1,i,j,k,flag=0,n,sum=0,c=0;
num1=num;
while(flag==0)
{
while(num!=0)
{
n=num%10;
sum=sum+n;
num=num/10;
c=c+1;
}
if (c==1)
{
if (sum==1) System.out.println(num1+" is a magic number");
else
System.out.println(num1+" is not a magic number");
flag=1;}
num=sum;
sum=0;
c=0;
}
}
}

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 to print a triangle as follows:


//1
//12
//123
//1234
//12345
class display
{
public void working()
{
int a,b,i,j;
for(i=1;i<=5;i=i+1)
{
for(j=1;j<=i;j=j+1)
{
System.out.print(" "+j);
}
System.out.println();
}
}
}

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 to input a number and print its binary equivalent.


class binconversion
{
public void calculate(int n)
{
int a[]=new int[10],c=-1,p;
while(n>0)
{
c=c+1;
p=n%2;
if (p==0)
{ a[c]=0;}
else
{a[c]=1;}
n=n/2;
}
for(int i=c;i>=0;i--)
{System.out.print(a[i]);}
}
}

PROGRAM #48 :

Program to enter a number and sort the digits in ascending


// order. Display the new digit so obtained.
class sortdigit
{

public void work(int n)


{
int js,b;
int a[]=new int[10],d,s=0;
while(n>0)
{
d=n%10;
a[s]=d;
s=s+1;
n=n/10;
}
for(int i=0;i<s-1;i++)
{ for(int j=i+1;j<s;j++)
{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 #49 :

to extract and display the maximum prime digit from a number


class maxprimedigit
{
public void compute(int c)
{
int b;
int n,big=0,d,count=0,i;
do{
n=c%10;
for(i=1;i<=n;i++)
{
if(n%i==0)
count=count+1;}
if(count==2)
{ if(big<n)
big=n;
count=0;
}

count=0;
c=c/10;
}
while(c!=0);
System.out.println(big+"is the maximum prime digit number");
}
}

PROGRAM #50 :

Program to display the following output:


// 11
// 12 22
// 13 23 33
class out
{
public void working()
{
int a,b,i,j;
for(i=1;i<=3;i=i+1)
{
for(j=1;j<=i;j=j+1)
{
System.out.print(" "+j+i);
}
System.out.println();
}
}
}

PROGRAM #51 :

Program to print the characters of the string present at odd positions.


public class odd
{
private int x,i,l;
public odd()
{
x = 0;
}

public void array1(String a)


{
l=a.length();
System.out.print("the characters of the string "+a+" at odd
positions are:");
for(i=0;i<l;i++)
{
if (i%2!=0)
System.out.print(a.charAt(i));}
}
}

PROGRAM #52 :

Program to print the characters of the string present at even positions.


public class even
{
private int x,i,l;
public even()
{
x = 0;
}
public void array1(String a)
{
l=a.length();
System.out.print("the characters of the string "+a+" at even
positions are:");
for(i=0;i<l;i++)
{
if (i%2==0)
System.out.print(a.charAt(i));}
}
}

PROGRAM #53 :

Program to input a number and print its sum and average of digits
class sd
{
public void input(int number)

{int sum=0, avg, i,j;


int digit,n,nn=0;
n=number;
while(n>0)
{
digit=n%10;
sum =sum+digit;
nn=nn+1;
n=n/10;
}
System.out.println("The number entered is "+number);
System.out.println("The sum of digits is "+sum);
System.out.println("The average of digits is "+sum/nn);
}
}

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 to display the sum of the following series


// S = 1+3+5+7....N where N is the input.
public class prog
{
public void work(int n)
{
int s=0;
for (int i=1;i<=n;i=i+2)
{
s = s+i;
}
System.out.println("The sum of the series is "+s);
}
}

PROGRAM # 57 :

Program to display the following series


// -1+2-3+4....N where N is the input.
public class prog
{
public void work(int n)

{
for (int i=1;i<=n;i++)
{
if (i%2==0)
System.out.print("+"+i);
else
System.out.print("-"+i);
}
}
}

PROGRAM #58 :

Program to print the sum of the even digits of an entered number


public class prog

{
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 :

Program to print the number of the prime digits of an entered number


public class prog

{
public void work(int n)
{
int digit, sum=0;
while(n>0)
{
digit = n%10;
// checking digit even

if (digit==1 || digit==3 || digit == 5 || digit==7)


sum = sum + 1;
n=n/10;
}
System.out.println("The number of the prime digits of the
number is "+sum);
}
}

PROGRAM #60 :

Program to print the maximum digit of an entered number.


public class prog
{
public void work(int n)
{
int digit,max=0;
while(n>0)
{
digit = n%10;
{
if (digit>max )
max=digit;}
n=n/10;
}
System.out.println("The maximum digit is "+max);
}
}

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 to display the following series


// 1+3+5+7....N where N is the input.
public class prog
{
public void work(int n)
{
int s=0;
for (int i=1;i<=n;i=i+2)
{
System.out.print(i);
}
}
}

PROGRAM #63 :

Program to display the sum of the following series


// S = 1-2+3-4.....N where N is the input.
public class prog
{
public void work(int n)
{
int s=0;
for (int i=1;i<=n;i++)
{
if (i%2==0)
s = s+i;
else
s = s-i;
}
System.out.println("The sum of the series is "+s);
}
}

PROGRAM # 64 :

Program to display the following series


// 1 22 333 4444 55555... N
public class prog
{
public void work(int n)
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.print(" ");
}
}
}

PROGRAM #65 :

Program to print the sum of the prime digits of an entered number


public class prog
{
public void work(int n)
{
int digit, sum=0;
while(n>0)
{
digit = n%10;
// checking digit even
if (digit==1 || digit==3 || digit == 5 || digit==7)
sum = sum + digit;
n=n/10;
}
System.out.println("The sum of the prime digits of the number is
"+sum);
}
}

PROGRAM #66 :

Program to print the sum factors of each digit of an entered number


public class prog
{
public void work(int n)
{

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 :

Program to print the minimum digit of an entered number.


public class prog

{
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 :

Program to print whether the sum of digits of an entered number is odd


or even.
public class prog
{
public void work(int n)
{
int digit,sum=0;

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 :

Program to print the factors of each digit of an entered number


public class prog

{
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 :

Program to print the sum of the odd digits of an entered number


public class prog

{
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 :

Program to display the following series


// 1 12 123 1234 12345
public class prog

{
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 to display the following series


// +1-2+3-4....N where N is the input.
public class prog
{
public void work(int n)
{
for (int i=1;i<=n;i++)
{
if (i%2!=0)
System.out.print("+"+i);
else
System.out.print("-"+i);
}
}

PROGRAM #73 :

Program to display the sum of the following series


// S = -1+2-3+4.....N where N is the input.
public class prog
{
public void work(int n)
{
int s=0;
for (int i=1;i<=n;i++)
{
if (i%2==0)
s = s-i;
else
s = s+i;
}
System.out.println("The sum of the series is "+s);
}
}

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 :

// length of each word of a sentence


//
print longest word
class string4
{
private int i,l,c;
char z;
public string4()
{
// initialise instance variables
c=0;
}
public void work(String s)
{s=s+" ";
l=s.length();
for(i=0;i<l;i++)
{
z=s.charAt(i);
c++;
System.out.print(z);
if(z==' ')

{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

System.out.println("The string is not a palindrome");


}
}
PROGRAM # 96 :
public class ABC
{//piglatin- shift all the characters to end of the string till vowel does not
come then add 'a'
private int x,y,c,l;String a;
char b;
public ABC()
{
c=0;
x=0;y=0;
}
public void sampleMethod(String a)
{
x=a.length();
for(y=0;y<x;y++)
{
b=a.charAt(y);
if(b=='a' || b=='e' || b=='i' || b=='o' || b== 'u')
{
break;
}
else
{
c++;
a=a+b;
//a[y]=' ';
}
}
a=a+'a';
System.out.print("piglatin of the word is ");
l=x+c+1;
for(y=c;y<l;y++)
{
b=a.charAt(y);
System.out.print(b);
}
}
}

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();
}
}
}
}

You might also like