Thursday 4 August 2011

PROGRAMS 5


TO PRINT THE FOLLOWING PATTERN
S
SHS
SHIHS
SHIVIHS
SHIVEVIHS
SHIVENEVISH
SHIVENDNEVIHS
SHIVENDUDNEVIHS
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

RAJ
R
RAR
RAJAR

RAJAT
R
RAR
RAJAR
RAJAJAR
RAJATAJAR

TO ENTER A MATRIX OF nXn AND PRINT ITS DIAGONALS AS WELL AS THEIR SUM SEPARATELY

ALGORITHM
Step-1 Start
Step-2 Initialization of variables.
Step-3 Accept the limit value for no. of rows and columns.
Step-4 Declaration of a 2-D array with ‘limit’ as its both subscripts.
Step-5 Run the loop for no. of rows.
Step-6 Run the loop for no. of columns.
Step-7 Store the numbers in the declared array.
Step-8 Change the line.
Step-9 Run the loop for no. of rows.
Step-10 Run the loop for no. of columns.
Step-11 If no. of rows are equal to no. of columns then print the left diagonal and add the value of the left diagonal to a counter variable else, proceed to
      Step-12.
Step-12 If no. of rows + no. of columns is equal to subscript limit -1 then print the right diagonal and add the value of the right diagonal to a counter variable else, proceed to Step-13.

Step-13 Print horizontal tab (i.e. ‘\t’)
Step-14 Display the sum of both diagonals separately.
Step-15 Exit

PROGRAM
import java.io.*;
class diagonal
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter No. of rows and columns");
int n=Integer.parseInt(br.readLine());
int a[][]=new int[n][n];
int i,j,ld=0,rd=0;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
System.out.println("Enter a number");
a[i][j]=Integer.parseInt(br.readLine());
    }
   }
   for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
   
}
    System.out.println();
   }
   System.out.println ("Diagonals are as follows");
   for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (i==j)
{
ld=ld+a[i][j];
System.out.print(a[i][j]+"\t");
    }
else if (i+j==n-1)
{
System.out.print(a[i][j]+"\t");
rd=rd+a[i][j];
}
else
{
System.out.print("\t");
    }
    }
System.out.println();

}
System.out.println("Sum of Left Diagonal is "+ld);
System.out.println("Sum of Right Diagonal is "+rd);
    }
}
VARIABLE DESCRIPTION-
VARIABLE USED
DATATYPE
DESCRIPTION

n
Integer type
For storing the value for no. of rows and columns.
a
Integer type
A 2-D array with variable ‘n’ as subscripts.
i
Integer type
For running the loop for no. of rows.
j
Integer type
For running the loop for no. of columns.
ld
Integer type
For storing the sum of the left diagonal.
rd
Integer type
For storing the sum of the right diagonal.

INPUT/OUTPUT-
Enter No. of rows and columns
3
Enter a number
1
Enter a number
2


Enter a number
3
Enter a number
4
Enter a number
5
Enter a number
6
Enter a number
7
Enter a number
8
Enter a number
9
1          2          3         
4          5          6         
7          8          9         
Diagonals are as follows
1                      3         
            5                     
7                      9         
Sum of Left Diagonal is 15
Sum of Right Diagonal is 10
TO PRINT FOLLOWING PATTERN
AAAA1
BBB12
CC123
D1234
12345
ALGORITHM-
Step-1 Start
Step-2 Take the limit value 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 alphabets.
Step-6 Print the alphabets
Step-7 Run the loop to print the numbers.
Step-8 Print the number/s.
Step-9 Change the line.
Step-10 Exit






PROGRAM
   class pattern9
   {
      public static void main(int n)
   {
      int a,b,c;
      char s='A';
      for (a=1;a<=n;a++)
   {
      for (b=1;b<=n-a;b++)
   {
      System.out.print(s);
    }
      for (c=1;c<=a;c++)
    {
        System.out.print (c);
    }
     System.out.println();
      s++;
   }
      }
   }



VARIABLE DESCRIPTION-
VARIABLE USED
DATATYPE
DESCRIPTION

n
Integer type
To store the limit value.
a
Integer type
For running the loop to print the no. of lines.
b
Integer type
For running the loop to print the alphabets.
c
Integer type
For running the loop to print the numbers.
s
Character type
Counter variable for printing alphabets.

INPUT/OUTPUT-

INPUT
OUTPUT

3
AA1
B12
123


6
AAAAA1
BBBB12
CCC123
DD1234
E12345
123456
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-7 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 to enter a number & check whether it is a palindrome or not.If not, then ADD its reverse until it becomes a palindrome.

ALGORITHM
Step-1Start
Step-2 Generate the function to reverse a number.(Step 3 to Step 8)
Step-3Input a number, n.
Step-4 Initialize variables a=0,b.
Step-5 Generate a loop from n to 0 in b(Step 6 & 7)
Step-6 Store a=a*10+b%10
Step-7 b=b/10
Step-8 Return a.
Step-9 Generate the main function & input the number
Step-10 Initialize x=0 & y=n
Step-11 Generate loop until x==0
Step-12 If reverse of y = y, print y is palindrome & x=1else print that reverse of string is not palindrome & y=y+reverse of y.
Step-13 Exit

PROGRAM
class Palin
{
 public static int reverse(int n)
 {
  int a=0,b;
  for(b=n;b>0;b/=10)
  a=a*10+b%10;
  return a;
 }
 public static void main(int n)


{
  int x=0,y=n;
  for (;x==0;)
  {
   If (reverse(y)==y)
   {
    System.out.println (y+" is palindrome.");
    x=1;
   }
   else
   {
    System.out.println (y+" is not palindrome.");
    y=y+reverse(y);
   }}}}

VARIABLE DESCRIPTION

VARIABLE USED
DATATYPE
DESCRIPTION
a
Integer type
Stores reverse of entered value.
b
Integer type
Loop variable for storing reverse of entered number.
n
Integer type
Stores the number entered by user.
x
Integer type
Controls loop.
y
Integer type
Stores the value stored in variable ‘n’.

INPUT/OUTPUT-

INPUT
OUTPUT
65556
65556 is palindrome.

4587
4587 is not palindrome.
12441 is not palindrome.
26862 is palindrome.
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 TO PRINT ALL POSSIBLE COMBINATIOS OF A
FOUR LETTERED STRING

ALGORITHM
Step-1 Start
Step-2  Enter the string and store it in ‘s’
Step-3 Store the length of the string in ‘l’
Step-4 Take three loops
            1st loop- for(a=0;a<l;a++)
            2nd loop-for(b=0;b<l;b++)
            3rd loop -for(c=0;c<l;c++)
Step-5 If a,b and c are not equal then add them ,subtract them from 6 and store the result in ‘d’
Step-6 Add all the characters of ‘s’ at locations a,b,c,d and store it ‘w’
Step-7 Print ‘w’
Step-8 Exit








PROGRAM
import java.io.*;
class combinations
{
public static void main()throws IOException
{
int l,a,b,c,d;
char r,t,u,v;
String w="",s="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A 4 LETTERED STRING");
s=br.readLine();
l=s.length();
System.out.println ("COMBINATIONS ARE");
for(a=0;a<l;a++)
{
for (b=0;b<l;b++)
{
for (c=0;c<l;c++)
{
if (a!=b&&b!=c&&c!=a)
{
d=6-(a+b+c);
r=s.charAt(a);
t=s.charAt(b);
u=s.charAt(c);
v=s.charAt(d);
w=r+""+t+""+u+""+v;
System.out.println (w);
}
w="";}}}}}

VARIABLE DESCRIPTION-

VARIABLE USED
DATATYPE
DESCRIPTION
l
Integer type
For storing the length.
a,b,c
Integer type
Used for running different loops for combinations.
r,t,u,v
Character type
Used for storing different characters.
s
String type
Used for storing the entered string.
w
String type
Used for storing combinations.










INPUT/OUTPUT-

INPUT
OUTPUT







ENTER A 4 LETTERED STRING
COMP
COMBINATIONS ARE
COMP
COPM
CMOP
CMPO
CPOM
CPMO
OCMP
OCPM
OMCP
OMPC
OPCM
OPMC
MCOP
MCPO
MOCP
MOPC
MPCO
MPOC
PCOM
PCMO
POCM
POMC
PMCO
      PMOC
PROGRAM-14
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 TO FIND WHETHER ENTERED DAY IS VALID OR NOT

ALGORITHM
Step-1 Start
Step-2 Store the date , month and year in ‘dd’ , ‘mm’ and ‘yy’ respectively
Step-3 Take an array m[] to store maximum no of days of all months
Step-4 If the year is divisible by 100 and 400 assign k=1 else if it is divisible by 4 assign k=1 else assign k=0 and the value of k to 1st location element of m[]
Step-5 If date is  not between 1 and m[mm-1] assign t=1 
Step-6 If month  is  not between 1 and 13
Step-7 If year is less than 1 assign t=1 
Step-8 If t=0, print day is valid else print day is invalid
Step-9 Exit
PROGRAM
import java.io.*;
class day
{
public static void main()throws IOException
{
int t=0,k=0,dd,mm,yy;
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println ("ENTER THE DATE");
dd=Integer.parseInt(br.readLine());
System.out.println ("ENTER THE MONTH");
mm=Integer.parseInt(br.readLine());
System.out.println("ENTER THE YEAR");
yy=Integer.parseInt(br.readLine());
k=(yy%100==0)?(yy%400==0)?1:0:(yy%4==0)?1:0;
m[k]=m[k]+k;
if(yy<1)
{
t=1;
System.out.println ("YEAR IS INVALID");
}
if (mm<1||mm>12)
{
t=1;
System.out.println ("MONTH IS INVALID");
}
if(dd<0||dd>m[mm-1])
{
t=1;
System.out.println ("DATE IS INVALID");
}
if(t==0)

{
System.out.println ("DAY IS VALID");
}
}
}
VARIABLE DESCRIPTION
VARIABLE USED
DATATYPE
DESCRIPTION
dd
Integer type
Stores the date
mm
Integer type
Stores the month
yy
Integer type
Stores the year
m
Integer type
An array used for storing maximum no. of days in a month.
k
Integer type
For storing the result

INPUT/OUTPUT-

INPUT
OUTPUT
ENTER THE DATE
20
ENTER THE MONTH
25
ENTER THE YEAR
2007


MONTH IS INVALID
ENTER THE DATE
90
ENTER THE MONTH
5
ENTER THE YEAR
16


DATE IS INVALID

No comments:

Post a Comment