Wednesday, 3 August 2011

PROGRAMS


PROGRAM TO CHECK WHETHER ENTERED NUMBER IS MAGICAL OR NOT

ALGORITHM

Step-1 Start
Step-2 Generate a function for calculating the sum of digits (Step 3 to 7)
Step-3 Initialize s=0 & a
Step-4 Generate loop till each digit is taken (Step 5 to 6)
Step-5 s=s+a/10
Step-6 a/=10
Step-7 Return s
Step-8 In main function, input any number
Step-9 Generate a loop till counter is not disturbed (Step 10 to 12)
Step-10 Check if sum of digits is less than 10
Step-11 If it is true update counter & if sum is 1, print no. is magical
Step-12 Else execute next loop with the sum obtained
Step-13 Exit










PROGRAM
class magic
{
 public static int sod(int n)
 {
  int s=0,a;
  for(a=n;a>0;a/=10)
  s=s+a%10;
  return s;
 }
 public static void main(int n)
 {
  int x=0,y=n;
  for (;x==0;)
 
{
   if(sod(y)<10)
   {
    x++;
    if (sod(y)==1)
    System.out.println (n+" is magical.");
    else
    System.out.println (n+" is not magical.");
   }
   else
   y=sod(y);
  }
 }
}





VARIABLE DESCRIPTION

VARIABLE USED
DATATYPE
DESCRIPTION

S
Integer type
Stores sum of digits of entered value.
A
Integer type
Loop variable for getting the sum of digits.
N
Integer type
Stores the entered value.
X
Integer type
Counter variable used in loop.
Y
Integer type
Stores the sum temporarily.

INPUT/OUTPUT-



INPUT
OUTPUT
1254
1254 is not magical.
455554
455554 is magical.



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




PROGRAM-7
Program to enter a string & if its Palindrome, print the pattern-
                             M A D A M
                                                              A                A
                                            D                D
   A                A
                                                              M A D A M

ALGORITHM
Step-1 Start
Step-2 Enter any string
Step-3 Take the reverse of the string into any variable
Step-4 Initialize c=length of string -2
Step-5 If string is not equal to its reverse, then print that it is not palindrome & exit.
Step-6 Else print the string
Step-7 Create loop from 1 to length of string -1(Step 8 to 10)
Step-8 Print the character at that index
Step-9 Print c spaces
Step-10 Print the character at that index
Step-11 Print the string
Step-12 Exit










PROGRAM

class palinpat
{
 public static void main(String s)
 {
  String t="";
  int a,b,c=s.length()-2;
  for(a=0;a<s.length();a++)
   t=s.charAt(a)+t;
  if(t.equals(s)==false)
  {
  
System.out.println(s+" is not palindrome.");
   System.exit(0);
  }
  System.out.println(s);
  for(a=1;a<s.length()-1;a++)
  {
   System.out.print(s.charAt(a));
   for(b=0;b<c;b++)
    System.out.print(" ");
   System.out.println(s.charAt(a));
  }
  System.out.println(s);
 }
}









VARIABLE DESCRIPTIONN

VARIABLE USED
DATATYPE
DESCRIPTION
s
String type
Stores the entered string
t
String type
Stores reverse of string
a
Integer type
Loop variable
b
Integer type
Loop variable
c
Integer type
For storing no. of spaces


INPUT/OUTPUT-

Input
Output
AMAR
AMAR is not palindrome.


ARORA
         A R O R A
                   R            R
                   O            O
                   R            R
         A R O R A


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




PROGRAM-8
PROGRAM TO INPUT A NUMBER AND PRINT IT IN WORDS.

ALGORITHM
Step1-Start
Step2-Input the number in amt (Integer Type)
Step3-We have taken the Integer variable i.e. z&g
Step4-We have taken the String array i.e.
          String x1[] = {“,” “ONE”, “TWO”, “THREE”, “FOUR”, “FIVE”,  “SIX”, “SEVEN”, “EIGHT”, “NINE”}.
          String x [] = {“,”, “TEN”, “ELEVEN”, “TWELVE”, “THIRTEEN”,
            “FOURTEEN”, “FIFTEEN”, “SIXTEEN” , “SEVENTEEN” ,                                                                                                                                                                          
            “EIGHTEEN”, “NINTEEN”};
          String x2[] = {“,” “TWENTY”, “THIRTY”, “FOURTY”, “FIFTY”,    
            “SIXTY”, “SEVENTY”, “EIGHTY”, “NINTY”};
Step5-We have taken the remainder of inputted number amt in z and the 
          quotient of the Inputted Step number in g.
Step6- We have checked the condition i.e. (g! =1).If this condition found   
           true then Go to Step7 otherwise Go to Step7-Print the Sting array
           i.e. (x2[g-1] + “”+x1[z])
Step8-Print the String array i.e. (x [amt-9]);
Step9-End

PROGRAM
import java.io.*;
class eng
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String x3;
System.out.println("Enter any Number(less than 99)");
int amt=Integer.parseInt(br.readLine());
int a,b,c,y,z,g;
String[]={"","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen",
"Sixteen","Seventeen","Eighteen","Nineteen"};
String x1[]={" " ,"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
String x2[]={"","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
z=amt%10;
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]);
else
System.out.println(x[amt-9]);
    System.out.println("----------*----------*---------- ");
    System.out.println("----------*----------*---------- ");
}
}
VARIABLE DESCRIPTIONN

VARIABLE USED
DATATYPE
DESCRIPTION
a
Integer type
Used In program for calculation
b
Integer type
Used In program for calculation
c
Integer type
Used In program for calculation
y
Integer type
Loop variable
z
Integer type
Used In program for calculation
g
Integer type
Used In program for calculation


INPUT/OUTPUT-

Input
Output
3
Three


40


Fourty

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


 PROGRAM-9
PROGRAM TO PRINT THE AIRTHMATIC SERIES.


Define a class 'APSeries' with following specification
Class Name : APSeries
Data Member :
a - To store first term
d - To store common difference
Member Function :
APSeries()                  - To initialize value of a and d with 0
APSeries(double a,double d) - To initialize value of a and d with
parametric a and d
nTHTerm(int n)         - To return nTH term of series
Sum(int n)                  - To return sum of series till n terms
showSeries(int n)       - To display n terms of series with sum





ALGORITHM

Step1- START
Step2-We have initialize double type variable i.e. a and d
Step3-We have made non –parameterized constructor by which we have
           assign 0 to ‘a’and‘d’.
Step4- We have made a parameterized constructor .Parameters are double a, double d. We have assign a to this. a i.e. object class and d to this .d
Step5-We have made a return type function i.e. nTHTerm. Parameter is int n.We has return the nth term of A.P series in this function
Step6- We have made another return type function i.e. Sum and int ‘n’ is
         the parameter. In this function we have return the sum of A.P series
Step7- We have made a function i.e. Show Series and parameter is int
          n (Integer Type). In this we have print the nth term of series as well   
          as Sum of AP series.
Step8- We have made main function of this class and print the A.P. series of required term.
Step9-End 
                                               
                                             



  PROGRAM
import java.io.*;
class APSeries
{
private double a,d;
APSeries()
{
a = d = 0;
}
APSeries(double a,double d)
{
this.a = a;
this.d = d;
}
double nTHTerm(int n)
{
return (a+(n-1)*d);
}
double Sum(int n)
{
return (n*(a+nTHTerm(n))/2);
}
void showSeries(int n)
{
 System.out.println("FIRST TERM OF THE SERIES IS  "+a);
 System.out.println("COMMON DIFFERENCE OF THE SERIES IS  "+d);
System.out.println("Hence the series is ");
for(int i=1;i<=n;i++)
{
System.out.println(nTHTerm(i)+" ");
}
System.out.println("It's Sum will be : "+Sum(n));
System.out.println("------------------------*-------------------------*---------------------------");   
}
}
class testap
{
public static void main(String args[]) throws Exception
{
APSeries a = new APSeries(1,2);
a.showSeries(5);
}
}
VARIABLE DESCRIPTIONN

VARIABLE USED
DATATYPE
DESCRIPTION
a
Double Type
Used In program for calculation
i
Integer type
Loop variable
n
Integer type
Loop variable
d
Integer type
Used In program for calculation

No comments:

Post a Comment