Wednesday, 3 August 2011

PROGRAMS 3


PROGRAM-13
TO CHECK WHETHER THE NUMBER IS SPECIAL NUMBER OR NOT

ALGORITHM
Step-1 Start

Step-2 Declaration of method for finding the factorial value of any no.

Step-3 Declaration of the main program.

Step-4 Take value from the user.

Step-5 Initialization of variables.

Step-6 Extract each digit from the entered value till its value becomes 0 a send each   digit into the method one by one and add its returning value in a variable in main
program.

Step-7 If the sum is equal to the original entered value then the result will be true else false.

Step-8 Exit









PROGRAM
 class special
{
public static long fact(int x)
{
long     i,f=1;
for (i=1;i<=x;i++)
{
f=f*i;
    }
  
 return f;
}
public static void main(int a)
{
special obj = new special();
long r=0,n=a,s=0;
while(a>0)
{
int k=a%10;
r=obj.fact(k);
s=s+r;
a=a/10;
    }
    if (s==n)
        System.out.println("The entered number is special");
        else
        System.out.println("The entered number is not special");
    }
}




VARIABLE DESCRIPTION-

VARIABLE USED
DATATYPE
DESCRIPTION

a
Integer type
For taking the number from the user.
r
Long type
For storing the returning value.
n
Long type
For storing the original number.
s
Long type
For storing the sum of the digits.
k
Integer type
For extracting the last digit of the entered number.

INPUT/OUTPUT-

INPUT
OUTPUT

145

The entered number is special

12345


The entered number is not special


********************************************************************************************





PROGRAM-14
Program to print the twin primes.

ALGORITHM-

Step1- Start
Step2- Store the limit in variable ‘a’.
Step3- Set for loop i.e. for (k=1; k<=’a’;k+)
Step4- Inside a loop find out the total number of factors of the limit i.e. ‘a’    by using the condition if (a%k==0) if true then goto Step-5.
Step5- Increase the counter by 1i.e. c++.
Step6- Check whether (c==2) if true then goto Step7 if false then got
               Step8
Step7- Return true.
Step8- Return false.
Step9- Make another main method
Step10-Accept a lower limit ‘lo’ and an upper limit’u’. upto which the twin numbers are to be printed .
Step11- Set a loop i.e. for (b=lo; b<u;b++)
Step12- Inside this loop calls the pime functioni.e. if (prime(b)&&prime(b+2))
Step13- If the upper condition is true then print b& b+2.
Step14- End.
                                     




 PROGRAM
import java.io.*;
class Twin_Prime
{
public static boolean prime(int a)
{
int k,c=0;
for(k=1;k<=a;k++)
{
if(a%k==0)
c++;
}
if(c==2)
return true;
else
return false;
public static void main()throws IOException
{
 int lo,up;
InputStreamReader i=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i);
System.out.println("Enter lower limit");
lo=Integer.parseInt(br.readLine());
System.out.println("Enter upper limit");
up=Integer.parseInt(br.readLine());
System.out.println("Twin Prime between "+lo+" to "+up+" are:-");
int b=0;
for(b=lo;b<=(up-2);b++)
{
if(prime(b)&&prime(b+2))
{
System.out.println(b+" "+(b+2));
}
}
System.out.println();
System.out.println();
}
}
INPUT/OUTPUT-



VARIABLE DESCRIPTION

Variables
Types Of Variables
Function
k,b
Integer
For Loop Purpose
c
Integer
Counter variable
a
Integer
Reference variable for limits
lo
Integer
Lower limit
up
Integer
Upper limit


********************************************************************************************








                                                                                




PROGRAM-15
TO PRINT THE FOLLOWING PATTERN
A
A B
A B H
A B H I
A B H I S
A B H I S H
A B H I S H E
A B H I S H E K
ALGORITHM

Step-1 Start
Step-2 Take the String from the user.
Step-3 Initialization of variables.
Step-4 Take the length of the string.
Step-5 Run the loop for printing the no. of lines.
Step-6 Run the loop for printing the numbers of spaces.
Step-6 Print the spaces.
Step-8 Run the loop for printing the characters of the entered string on the left   hand side.
Step-9 Print the characters
Step-10 Run the loop for printing the characters of the entered string on the right   hand side.
Step-11 Print the characters.
Step-12 Change the line.
Step-13 Exit






PROGRAM
class pattern8
{
public static void main(String name)
{
int i,j,len=name.length();
int space=len;
for (i=0;i<len;i++);
{
for (j=1;j<=space;j++)
{
System.out.print (" ");
    }
    for (j=1;j<=i;j++)
    {
      System.out.print(name.charAt(j));
  }
    for (j=i-1;j>=0;j--)
{
        System.out.print(name.charAt(j));
    }
    space--;
  
 System.out.println();
    }
    }
}







VARIABLE DESCRIPTION-

VARIABLE USED
DATATYPE
DESCRIPTION

name
String type
For storing the entered string.
i
Integer type
For running the loop to print the no. of lines.
j
Integer type
For running the loop to print the no. of spaces.
len
Integer type
For storing the length of the entered string.
space
Integer type
For storing the value of variable ‘len’ and used as a counter as well as a loop variable













INPUT/OUTPUT-

INPUT
OUTPUT

KETAN
K
KE
KET
KETA
KETAN



SAURABH
S
SA
SAU
SAUR
SAURA
SAURAB
SAURABH


********************************************************************************************



                                                                                                                         






PROGRAM- 16
To enter the first day of a month and the number of days
in that month & print the list of Sundays in that month.

ALGORITHM
Step 1:Start
Step 2:Declare fd,nd,n[][]=new int[6][7].
Step 3:Make a void function input().
Step 4:Input no. of days & first day.
Step 5:Make a function calc()
Step 6:Generate a loop from 0 to 6 in a.
Step 7:Generate a loop from 0 to 7 in b.
Step 8:If a==0 and b==fd-1, c++.
Step 9:If c>0 and c<=nd, n[a][b]=c++.
Step 10:Make the main() function.
Step 11:Call input() and calc().
Step 12:Print the dates that are at 7th column & is greater than zero.
Step 13:End

PROGRAM
import java.util.*;
class CalcSun
{
Scanner ob=new Scanner(System.in);
int fd,nd,n[][]=new int[6][7];
void input()throws Exception
 {
System.out.println("Enter no.of days in the month:");
nd=ob.nextInt();
System.out.println("Enter I day's no.if 1 is for monday:");
fd=ob.nextInt();
}
void calc()
{
int a,b,c=0;
for(a=0;a<6;a++)
{
for(b=0;b<7;b++)
{
if(a==0&&b==fd-1)
c++;
if(c>0&&c<=nd)
n[a][b]=c++;
}
}
}
void main()throws Exception
{
input();
calc();
System.out.println("List of sundays is:");
for(int h=0;h<6;h++)
if(n[h][6]!=0)
System.out.println(n[h][6]);
}
}



   


VARIABLE DESCRIPTION
Variable
Type
Use
a
Integer
Loop variable
b
Integer
Loop variable
c
Integer
Counter
n[][]
Integer
Array to store calendar
nd
Integer
Store no of days
fd
Integer
Store first day
h
Integer
Loop variable





















                           


                                          
PROGRAM-17
To enter the number of rows & then print the matrix of that
order in the following form-
                                       1 2      3      4
                                     12  13    14    5
                                    11   16    15    6
                                   10    9      8      7

ALGORITHM
Step 1:Start
Step 2:Enter tha no of rows/column 'n' that is odd.
Step 3:Create a 2-D array a[][] of rows & columns n.
Step 4:Assign the central element as 0.
Step 5:Assign ar[0][0]=1.
Step 6:Declare c=0,r=0,d=1,v=n.
Step 7:Make a loop to end when central element is not 0(Step 8 to 19).
Step 8:Make a loop until c<v.
Step 9:In loop,a[r][c]=++d & c++.
Step 10:After loop,c-- & r++.
Step 11:Make a loop until r<v.
Step 12:In loop,a[r][c]=++d & r++.
Step 13:After loop,c-- & r--.
Step 14:Make a loop until c>=n-v.
Step 15:In loop,a[r][c]=++d & c--.
Step 16:After loop,c++,v-- & r--.
Step 17:Make a loop until r>=n-v.
Step 18:In loop,a[r][c]=++d & r--.
Step 19:After loop,c++ & r++.
Step 20:Print the array.
Step 21:End.


PROGRAM
class Spiral
{
public static void main()
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER");
n=Integer.parseInt(br.readLine());
int a[][]=new int[n][n],c=0,r=0,d=1,v=n;
a[r][c]=1;
a[n/2][n/2]=0;
for(c=1;a[n/2][n/2]==0;c++,r++)
{
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT IS");
while(c<v)
{
a[r][c]=++d;
c++;
}
c--;r++;
while(r<v)
{
a[r][c]=++d;
r++;
}
c--;r--;
while(c>=n-v)
{
a[r][c]=++d;
c--;
}
v--;c++;r--;
while(r>=n-v)
{
a[r][c]=++d;
r--;
}
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
System.out.print(a[r][c]+"\t");
System.out.println();
}
}
}

               


VARIABLE DESCRIPTION
Variable
Type
Use
a[][]
Integer
Array to store numbers
r
Integer
To store row no.
c
Integer
To store column no.
d
Integer
To store current number
n
Integer
store array size
v
Integer
Counter



   





INPUT & OUTPUT

                              








PROGRAM-18
To print each digit of an entered number in words, and that too in reverse order. You have to use recursion in your program

ALGORITHM
Step 1:Start.
Step 2:Declare int n.
Step 3:Make default constructor to initialize n=0.
Step 4:Make a void function inpnum() to enter value of n.
Step 5:Make a void function extDigit() to input a number a.
Step 6:If a<=9,call NumToWords(a).
Step 7:Else call NumToWords(a%10) & call extDigit(a/10)
Step 8:Make function NumToWords() to input a number.
Step 9:Using switch case,check the input number for 0 to 9.
Step 10:Correspondingly print its number name
Step 11:Make main() function & call inpNum() & extDigit().
Step 12:End.

PROGRAM
import java.util.*;
class Convert
{
int n;
Convert()
{
n=0;
}
public void inpNum()throws Exception
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter any number:");
n=ob.nextInt();
}
public void extDigit(int a)
{
if(a>9)
{
NumToWords(a%10);
extDigit(a/10);
}
else
NumToWords(a);
}
public void NumToWords(int x)
{
String s="";
switch(x)
{
case 1:
s="one";
break;
case 0:
s="zero";
break;
case 2:
s="two";
break;
case 3:
s="three";
break;
case 4:
s="four";
break;
case 5:
s="five";
break;
case 6:
s="six";
break;
case 7:
s="seven";
break;
case 8:
s="eight";
break;
case 9:
s="nine";
break;
}
System.out.println(s);
}
public void main()throws Exception
{
inpNum();
extDigit(n);
}
}




VARIABLE DESCRIPTION
Variable
Type
Use
a
Integer
Store Location
n
Integer
Store Number
s
String
Store Number Name




INPUT & OUTPUT

                                                 









PROGRAM-19
Q.To input no of rows & print the spiral matrix as shown-
1
2     5     4
3

ALGORITHM
Step 1:Start.
Step 2:Enter tha no of rows/column 'n' that is odd.
Step 3:Create a 2-D array ar[][] of rows & columns n.
Step 4:Assign the central element as 0.
Step 5:Assign q=n-1,r=0,c=n/2,d=1 & p=0.
Step 6:Generate a loop until central element is zero.
Step 7:Make another loop in it until r<=q.
Step 8:ar[r][c]=d++;
Step 9:Increment r & check whether it is less than or equal to n/2.
Step 10:If true,c-- else c++.
Step 11:End inner loop and r-=2,p++.
Step 12:Make another loop until r>=p.
Step 13:ar[r][c]=d++;
Step 14:Decrement r & check whether it is less than n/2.
Step 15:If true,c-- else c++.
Step 16:End inner loop & r+=1,p-=1.
Step 17:End outer loop.
Step 18:Print the array elements if they are not zero.
Step 19:End.

PROGRAM
class Circular
{
public static void main()
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER");
n=Integer.parseInt(br.readLine());
int ar[][]=new int[n][n];
int r=0,c=n/2,d=1,p=0,q=n-1;
ar[c][c]=0;
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT IS");
while(ar[n/2][n/2]==0)
{
while(r<=q)
{
ar[r][c]=d++;
r++;
if(r<=n/2)
c--;
else
c++;
}
r-=2;
p++;
while(r>=p)
{
ar[r][c]=d++;
r--;
if(r<n/2)
c--;
else
c++;
}
r+=1;
q-=1;
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
if(ar[r][c]!=0)
System.out.print(ar[r][c]+"\t");
else
System.out.print("\t");
}
System.out.println();
}
}}
VARIABLE DESCRIPTION
Variable
Type
Use
ar[][]
Integer
Array to store no.
n
Integer
To store array length
r
Integer
To store row no.
c
Integer
To store column no.
d
Integer
Counter
p
Integer
Temporary
q
Integer
Temporary









INPUT & OUTPUT
                                                                                                                                        
                                           

No comments:

Post a Comment