Sunday 25 June 2017

Encryption and Decryption

Hi friends,
the code for life we discuss here is,

SQUARE ENCRYPT

A secret code encrypts a message by putting it in an array and reading down the columns (blanks are replaced by asterisks and full stops are added to fill up the array).

Write a program that encrypts an input string. (For extra marks write a program that decrypts a given string using the rules outlined above.)

LETS*G
O*TO*T HE*SAN DWICH* SHOP*T ODAY..

 Input:         “Lets go to the sandwich shop today”
Output:      “Lohdsoe*ewhdtt*ioasoscpy**ah*.gtn*t.”

CODE:

package sanjay;
import java.util.*;
public class encrypt {
   public static void main(String args[])
   {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter a string : ");
       String s1=sc.nextLine();
       int length=s1.length();
       String s2[]=new String[100];         //to store as a char array
       for(int z=0;z<s2.length;z++)
       {
           s2[z]="";                        //2nd array s 1st position
       }
       int count=0;
        int j=0;
       for(int i=0;i<s1.length();i++)
       {
               if(s1.charAt(i)==' ')           //if space then
               {
                   String str="*";            //assign *
                   s2[j]=s2[j]+str;           //to link wid next word
                   count++;
               }
               else
               {
                   s2[j]=s2[j]+s1.charAt(i);    //1&2nd arrays 1st pos +
                   count++;
               }
               if(count!=6&&i==s1.length()-1)
               {
                   for(int x=count;x<6;x++)
                   {
                       s2[j]=s2[j]+".";         //to fill blanks wid .
                   }
               }
               if(count==6)
               {
                   j++;
                   count=0;
               }
        }
       for(int k=0;k<6;k++)
       {
           for(int y=0;y<=j;y++)
           {
                System.out.print(s2[y].charAt(k));
           }
       }
   }

}

Rotating a NXN Mratrix

Hello friends !!
Today's code for life is,
Imagine a square pattern divided into smaller squares as shown below. As you can  see seven of the smaller squares are filled in. If this is rotated clockwise by 90 degrees and placed on top of the original pattern then more squares are filled in.

The total number of filled in squares is now 13. Continuing in this way you can again rotate and add the original pattern to the combination giving 19 black squares and one more rotation + addition gives 25 black squares.


You must write a program that will read in a square pattern and output the original number of black squares and the number that result from each rotation.

Input:

-  an integer N on a single line
-  the next N lines consist of the NxN square,
(where 1 represents a black square and 0 represents a white square)

Output:

-      four lines containing the number of black squares before each rotation



CODE:

package sanjay😊;
import java.util.*;
public class matrixrotate {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the matrix size: ");
        int arr[][]=new int[100][100];          //2d matrix array
        int i,j,k,m,l;
         int old_arr[][]=new int[100][100];     //initial array
          int add_arr[][]=new int[100][100];    // after rotatong to add new array
         int no_rotation=0;
         int rotation;
        int n = input.nextInt();
        System.out.println("Enter the matrix elements as 1 and 0: ");
        for( i=0;i<n;i++)
        {
            for( j=0;j<n;j++)
            {
                arr[i][j]=input.nextInt();
            }
        }
         System.out.println("The Entered matrix is:");
        for(i=0;i<n;i++)
        {
             for( j=0;j<n;j++)
            {
            if(arr[i][j]>0)
            {
                no_rotation++;
            }
            }
             System.out.println("");
        }
        for( k=1;k<5;k++)
        {
            rotation=0;
             System.out.println("Rotation:"+k);
            for ( i = 0; i < n / 2; i++)
            {
            for ( j = i; j < n - i - 1; j++)
            {
            int temp= arr[i][j];
            arr[i][j] = arr[n - j - 1][i];
            arr[n - j - 1][i] = arr[n - i - 1][n - j - 1];
            arr[n - i - 1][n - j - 1] = arr[j][n - i - 1];
            arr[j][n - i - 1] = temp;
            }
            }
            for(m=0;m<n;m++)
            {
                for(l=0;l<n;l++)
                {
                                add_arr[m][l]=old_arr[m][l]+arr[m][l];
                                  if(add_arr[m][l]>0)
                                  {
                                      rotation++;
                                  }
                                  arr[m][l]=add_arr[m][l];
                                  old_arr[m][l]=add_arr[m][l];
                }
             }
            System.out.println("Rotation values grater than 1:"+rotation);
            }  
      }
}

Saturday 24 June 2017

Assigning Wifi password for a new user !

Hello friends,
In today's problem we discuss about assigning a password for a hotel wifi conection based on the inputs by the user based on certain criteria !!

Conditions:

  • Length of password is to be 5 characters
  • 1st char- summation of room number(till it becomes a single digit)
  • 2nd char- 1st char of lastname (if no last name,then first name)
  • 3rd char- summation of mobile number(1 digit)
  • 4th char- random mo generated system
  • 5th char- if person is from locality then no of chars in 1stname ,if it is odd then add 1,else
  • print the last index of firstname

CODE:

package sanjay;
import java.util.*;
public class wifiassign {
static  String password="";
static int sum_of_digits(long num)
{
long sum=0;
long rem=0;
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
return (int)sum;
}
static int no_of_digits(long num)
{
int nod=0;
while(num>0)
{
num=num/10;
nod++;
}
return nod;
}
static int getRandomNumberInRange(int min, int max) {

if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String first_name="";
String middle_name="";
String last_name="";
long phno=0;
int room_no=0;
String area="";
String name="";
System.out.println("Enter the name : ");
name=sc.nextLine();
System.out.println("Enter the area(Rural/Urban) : ");
area=sc.nextLine();
System.out.println("Enter the mobile number : ");
phno=sc.nextLong();
System.out.println("Enter the room number : ");
room_no=sc.nextInt();
String names[]=name.split(" ");
int char1=sum_of_digits(room_no);
int dig1=no_of_digits(char1);
while(dig1>1)
{
char1=sum_of_digits(char1);
dig1=no_of_digits(char1);
}
password=password+char1;
password=password+names[1].charAt(0);
int char3=sum_of_digits(phno);
//System.out.println(char3);
int dig3=no_of_digits(char3);
while(dig3>1)
{
char3=sum_of_digits(char3);
dig3=no_of_digits(char3);
}
password=password+char3;
int char4=getRandomNumberInRange(0,9);
password=password+char4;
int char5=0;
int dig5=0,len=0;
if(area.equalsIgnoreCase("Rural"))
{
len=names[0].length();
if(len%2==0)
{
char5=len;
dig5=no_of_digits(char5);
while(dig5>1)
{
char5=sum_of_digits(len);
dig5=no_of_digits(char5);
}
}
else
{
char5=len+1;
dig5=no_of_digits(char5);
while(dig5>1)
{
char5=sum_of_digits(len);
dig5=no_of_digits(char5);
}
}

}
if(area.equalsIgnoreCase("Urban"))
{
len=names[0].length();
char5=len;
dig5=no_of_digits(char5);
while(dig5>1)
{
char5=sum_of_digits(len);
dig5=no_of_digits(char5);
}
}
password=password+char5;
System.out.println("Password is : "+password);
}
}

Monday 19 June 2017

Square Pattern for multiple squares!!

Hello friends,This is my first code!!
-G.Sanjaydeep

Imagine a square pattern divided into smaller squares . Squeven of the smaller squares are filled in. If this is rotated clockwise by 90 degrees and placed on top of the original pattern then more squares are filled in.

The total number of filled in squares is now 13. Continuing in this way you can again rotate and add the original pattern to the combination giving 19 black squares and one more rotation + addition gives 25 black squares.

Program reads in a square pattern and output the original number of black squares and the number that result from each rotation.

Input:

-  an integer N on a single line
-  the next N lines consist of the NxN square,
(where 1 represents a black square and 0 represents a white square)

Output:

-      four lines containing the number of black squares before each rotation

CODE:


package sanjay;
import java.util.*;
public class matrixrotate {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the matrix size: ");
        int arr[][]=new int[100][100];          //2d matrix array
        int i,j,k,m,l;
        int old_arr[][]=new int[100][100];     //initial array
        int add_arr[][]=new int[100][100];    // after rotatong to add new array
        int no_rotation=0;
        int rotation;
        int n = input.nextInt();
        System.out.println("Enter the matrix elements as 1 and 0: ");
        for( i=0;i<n;i++)
        {
            for( j=0;j<n;j++)
            {
                arr[i][j]=input.nextInt();
            }
        }
         System.out.println("The Entered matrix is:");
        for(i=0;i<n;i++)
        {
             for( j=0;j<n;j++)
            {
            if(arr[i][j]>0)
            {
                no_rotation++;
            }
            }
             System.out.println("");
        }
        for( k=1;k<5;k++)
        {
            rotation=0;
             System.out.println("Rotation:"+k);
            for ( i = 0; i < n / 2; i++)
            {
            for ( j = i; j < n - i - 1; j++)
            {
            int temp= arr[i][j];
            arr[i][j] = arr[n - j - 1][i];
            arr[n - j - 1][i] = arr[n - i - 1][n - j - 1];
            arr[n - i - 1][n - j - 1] = arr[j][n - i - 1];
            arr[j][n - i - 1] = temp;
            }
            }
            for(m=0;m<n;m++)
            {
                for(l=0;l<n;l++)
                {
                                add_arr[m][l]=old_arr[m][l]+arr[m][l];
                                  if(add_arr[m][l]>0)
                                  {
                                      rotation++;
                                  }
                                  arr[m][l]=add_arr[m][l];
                                  old_arr[m][l]=add_arr[m][l];
                }
             }
            System.out.println("Rotation values grater than 1:"+rotation);

            }  
}
}


BlockChain Company Infographic

Hello All,      This post talks about the impact of blockchain technology in various standards and industrial uses. Kindly do read to know i...