The Programming Project

Sunday, December 6, 2020

PYTHON PROGRAM FOR BEGINNERS : SCHOOL LEVEL

 Calculating How Old Your Dog is in Human Years

This assignment is designed to give you practice writing code and applying lessons and topics for the current module.


This homework deals with the following topics:


Getting user input

Error checking

Variables & data types

Conditionals

The Assignment


In this assignment, you will write a program in Python that calculates a dog’s age in human years.


The program will prompt the user for an age in dog years and calculate that age in human years.  Allow for int or float values, but check the user’s input to make sure it's valid -- it should be numeric and positive.  Otherwise, let the user know their input is not valid.


You can use the following rules to approximately convert a medium-sized dog’s age to human years:


For the first year, one dog year is equal to 15 human years

For the first 2 years, each dog year is equal to 12 human years

For the first 3 years, each dog year is equal to 9.3 human years

For the first 4 years, each dog year is equal to 8 human years

For the first 5 years, each dog year is equal to 7.2 human years

After that, each dog year is equal to 7 human years.  (Note: This means the first 5 dog years are equal to 36 human years (5 * 7.2) and the remaining dog years are equal to 7 human years each.)

Print the result in the following format, substituting for <dog_age> and <human_age>: "The given dog age <dog_age> is <human_age> in human years."  Round the result to 2 decimal places.  


For example:


If the user enters 2, the program will print: “The given dog age 2 is 24.0 in human years.”

If the user enters 3, the program will print: “The given dog age 3 is 27.9 in human years.”

If the user enters 4.5, the program will print: “The given dog age 4.5 is 32.4 in human years.”

If the user enters 12.1, the program will print: “The given dog age 12.1 is 85.7 in human years.”

Considering invalid inputs:


Your program must ask the user for an age in dog years - hint: use the input() function

We are going to test invalid inputs - make sure that your code can handle negative value inputs and non-numerical inputs!

For invalid inputs, make sure that your printed response adheres to the following:

     - If a text-based input is provided, make sure your response contains the word 'invalid'.  For example, if the user doesn’tinput a number, print “<age> is invalid.”, substituting for <age>.


     - If a negative input is provided, make sure your response contains the word 'negative'.  For example, if the user inputs a negative number, print “Age cannot be a negative number.”

  

  Python CODE:

 dog_age=(input("Enter the dog's age:"))

flag=False
try:
    dog_age = float(dog_age)
    dog_age_in_Human_years=0
    if dog_age <0:
        flag = True
    elif 0 < dog_age <=1:
        dog_age_in_Human_years += dog_age*15.0
    elif 1dog_age <=2:
        dog_age_in_Human_years += dog_age*12.0
    elif 2dog_age <=3:
        dog_age_in_Human_years += dog_age*9.3
    elif 3dog_age <=4:
        dog_age_in_Human_years += dog_age*8.0
    elif 4dog_age <=5:
        dog_age_in_Human_years += dog_age*7.2
    else:
        dog_age_in_Human_years += 5*7.2+(dog_age-5)*7.0 
except ValueError as e:
    print("Your input is invalid")
if flag == False:
    print("The given dog age"dog_age"is"round(dog_age_in_Human_years,2), "in human years.")
else:
    print("Age cannot be a negative number")
    

Wednesday, August 26, 2020

RSA Encryption Course

Here is the best RSA Encryption course on the Internet https://bit.ly/32x1UP9  ( Udemy )

 The RSA Public Key Cryptosystem is one of the best and widely used cryptosystems in the world today. Some people can talk a little about some of what RSA Encryption entails, but very few people can actually encrypt (and decrypt) data from start to finish using the RSA Public Key Cryptosystem. Highly recommended for mathematics students. 

Great example of real life application of Number Theory.



Tuesday, August 25, 2020

ISC COMPUTER SCIENCE THEORY PAPER JAVA PROGRAMS -2019 Question 8

 


import java.util.*;

public class ISC2015Q1 {

       public static void main(String[] args) {

       int M,N;

       System.out.println("Input:");

       Scanner in = new Scanner(System.in);

       do {

             System.out.println("ENTER ROW SIZE:");

             M = in.nextInt();

             System.out.println("ENTER COLUMN SIZE:");

             N = in.nextInt();

             if(M<0 || N < 0)

 

                   System.out.println("OUT OF RANGE, TRY AGAIN:");

             }while(M<0 || N < 0);

       MatRev obj = new MatRev(M,N);

       MatRev P = new MatRev(M,N);

       obj.fillarray();

       System.out.println("Entered Matrix");

       obj.show();

       obj.revMat(P);

       System.out.println("Reversed Matrix");

       P.show();

       in.close();

 

 

     }

}

class MatRev {

      

         public void revMat(MatRev P) {

                  for(int p=0;p<M;p++)

              for(int q=0;q<N;q++)

                    P.mat[p][q]= reverse(mat[p][q]); // storing reverse matrix in the current object P

               

         }

         private int reverse (int x) {

                int rev = 0,length;

                length = Integer.toString(x).length()-1;    // calculates the length of the number x

                do {                                                              // if you don't want to use library function     

                       rev += (x%10)*(int)Math.pow(10,length);    // use a do while loop

                       length--;

                       x /=10;

                       }while(x > 0);

                return rev;

         }

      public void show() {

            for(int p=0;p<M;p++)

                   for(int q=0;q<N;q++)

                         if(MAX_MATRIX < mat[p][q])

                                MAX_MATRIX = mat[p][q];

            String s,element;

            s=Integer.toString(MAX_MATRIX);

            for(int i=0;i<M;i++) {

                   for(int j=0;j<N;j++) {

                          element=Integer.toString(mat[i][j]); // for formatted output

                        int tmp=element.length();

                         while(tmp !=s.length()) {

                                       element +=" ";

                                       tmp++;

                                       }

 

                        System.out.print(element+"  ");

                         }              

                 System.out.println();

            }

     }

     public void fillarray() {

            Scanner in = new Scanner(System.in);

            System.out.println("Enter the values of the matrix:");

            for(int i=0;i<M;i++) {

                    System.out.println("Enter the elements of row :"+(i+1));

                   for(int j=0;j<N;j++) {

                         mat[i][j]=in.nextInt();

                   }

            }

            in.close();

      }

 

     MatRev(int M, int N){

            this.M = M;

            this.N = N;

            mat = new int[M][N];

            mat = new int[M][N];

            MAX_MATRIX =0;

            }

 

       private int[][] mat;

       private int M,N;

       private int MAX_MATRIX;    

}